linux 使用SSH密钥配置免密登录

需求:多台主机SSH免密登录,需要使用同一个密钥对

操作:

在Linux中,使用SSH密钥对来在多台主机之间配置免密登录。以下是配置步骤:

在你的本地机器上生成一个SSH密钥对。如果你已经有一个,你可以跳过这一步。在终端中运行以下命令:

bash 复制代码
ssh-keygen -t rsa

这将在你的~/.ssh目录下生成两个文件:id_rsa(私钥)和id_rsa.pub(公钥)。

将你的公钥复制到远程主机。你可以使用ssh-copy-id命令来完成这个任务。在终端中运行以下命令,

c 复制代码
ssh-copy-id user@host

将user和host替换为你的远程主机的用户名和主机名:

这将会把你的公钥添加到远程主机~/.ssh/authorized_keys文件中。

现在你应该可以免密登录到远程主机了。在终端中运行以下命令,将user和host替换为你的远程主机的用户名和主机名:

bash 复制代码
ssh user@host

如果你需要在多台主机之间配置免密登录,你可以重复第2步和第3步,为每台远程主机添加你的公钥。

注意:在进行这些操作时,请确保你有对远程主机的适当访问权限,并且你的SSH服务已经启动。

示例

先使用一台主机生成密钥对,如果root权限被禁止,请使用其他用户

c 复制代码
jupiter@installerdev04:/home/hw$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/jupiter/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /home/jupiter/.ssh/id_rsa
Your public key has been saved in /home/jupiter/.ssh/id_rsa.pub
The key fingerprint is:
SHA256:Y19G23GqT+Q/y957zuZPBtvkWirLxk6q0NJW/m9UJeg jupiter@installerdev04
The key's randomart image is:
+---[RSA 3072]----+
|             .   |
|            . . .|
|           .. ..o|
|           .Eo +.|
|        S . + =..|
|       + = o +.* |
|      o + o.o.+ *|
|       +   =+ooX+|
|        ...o==*O&|
+----[SHA256]-----+

## 生成的在此用户的家目录
jupiter@installerdev04:/home/hw$ cd ~/.ssh/
jupiter@installerdev04:~/.ssh$ ls
authorized_keys  id_rsa  id_rsa.pub


##  这一步是为了同样可以连接这台主机
jupiter@installerdev04:~/.ssh$ cat authorized_keys 
jupiter@installerdev04:~/.ssh$ cat id_rsa.pub > authorized_keys 

将公钥进行分发,如果没有这个命令,可以手动进行操作,多台主机分别操作

bash 复制代码
jupiter@installerdev04:~/.ssh$ ssh-copy-id jupiter@10.119.108.42
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/home/jupiter/.ssh/id_rsa.pub"
The authenticity of host '10.119.108.42 (10.119.108.42)' can't be established.
ED25519 key fingerprint is SHA256:1bu3kHrujNlvFXx2QEHvHSgMJeInfV3f9AFUBRP+iyE.
This key is not known by any other names
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
jupiter@10.119.108.42's password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'jupiter@10.119.108.42'"
and check to make sure that only the key(s) you wanted were added.

以上是基本操作,如果想要指定主机名还需要配置hosts

这样免密就配置好了,可以使用同一套密钥对进行连接

注意

如果需要授权

bash 复制代码
chmod 700 /home/earth/.ssh/

chmod 600 /home/earth/.ssh/authorized_keys 

如果配置sshd_config

bash 复制代码
sudo vi /etc/ssh/sshd_config

约45行,取消注释,删除#
 PubkeyAuthentication yes

重启ssh

bash 复制代码
sudo systemctl restart sshd

如果用户为earth,scp目的地的所有组是其他或者root

bash 复制代码
chown earth /data/cqap -R

scp -i config/id_rsa -o stricthostkeychecking=no ./cqap.tar  earth@10.122.177.83:/data/cqap/code

指定私钥进行授权

bash 复制代码
scp -i id_rsa -o stricthostkeychecking=no ./cqap.tar  qatest@10.122.168.25:/data/cloudqaplatform/code

ssh qatest@10.122.168.25 -i id_rsa "cd /data/cloudqaplatform && sudo sh ./deploy.sh"
相关推荐
树℡独4 小时前
ns-3仿真之应用层(五)
服务器·网络·tcp/ip·ns3
嵩山小老虎4 小时前
Windows 10/11 安装 WSL2 并配置 VSCode 开发环境(C 语言 / Linux API 适用)
linux·windows·vscode
Fleshy数模5 小时前
CentOS7 安装配置 MySQL5.7 完整教程(本地虚拟机学习版)
linux·mysql·centos
a41324475 小时前
ubuntu 25 安装vllm
linux·服务器·ubuntu·vllm
Configure-Handler5 小时前
buildroot System configuration
java·服务器·数据库
津津有味道5 小时前
易语言TCP服务端接收刷卡数据并向客户端读卡器发送指令
服务器·网络协议·tcp·易语言
Fᴏʀ ʏ꯭ᴏ꯭ᴜ꯭.6 小时前
Keepalived VIP迁移邮件告警配置指南
运维·服务器·笔记
Genie cloud6 小时前
1Panel SSL证书申请完整教程
服务器·网络协议·云计算·ssl
一只自律的鸡7 小时前
【Linux驱动】bug处理 ens33找不到IP
linux·运维·bug
17(无规则自律)7 小时前
【CSAPP 读书笔记】第二章:信息的表示和处理
linux·嵌入式硬件·考研·高考