Linux 配置SSH免密登录

Linux 配置SSH免密登录,第1张

【比如:Jenkins构建时都是以jenkins用户进行 *** 作,所以要以jeknins用户来配置免密钥登录。】

su - root

ssh-keygen -t rsa

A

[sjfuser@jenkins ~]$ ll /root/.ssh/

-rw-------. 1 sjfuser sjfuser 1671 4月 19 05:10 id_rsa

-rw-r--r--. 1 sjfuser sjfuser 398 4月 19 05:10 id_rsa.pub

-rw-r--r--. 1 sjfuser sjfuser 417 4月 19 05:08 known_hosts

B

-rw------- 1 root root 1217 Apr 21 10:46 /root/.ssh/authorized_keys

将此公钥写入任何一台远程主机的/root/.ssh/authorized_keys后,便可通过密钥登陆到远程主机。

任何一台主机,拿到该密钥id_rsa后,便可登陆写入了该公钥id_rsa.pub的主机。

如果添加指纹的时候提示添加失败,是因为你以前添加过了这个ip的指纹。

解决办法:将.ssh目录的known_hosts文件删除掉。也可以打开这个文件把对应ip的那条记录删除。

https://blog.csdn.net/qq_30059235/article/details/103890477

https://blog.csdn.net/fireofjava/article/details/40624215

1. 在服务器 S 上执行如下命令来生成配对密钥:

ssh-keygen -t rsa

按照提示 *** 作,注意,不要输入passphrase,因为我的之前已经 *** 作过,所以还提示了是否覆盖,输入 "y"回车就可以。其他提示信息如下

Generating public/private rsa key pair.

Enter file in which to save the key (/root/.ssh/id_rsa):

Enter passphrase (empty for no passphrase):

Enter same passphrase again:

Your identification has been saved in /root/.ssh/id_rsa.

Your public key has been saved in /root/.ssh/id_rsa.pub.

第1步:在本地主机中生成“密钥对”并将公钥传送到远程服务器中:

[root@linuxprobe ~]# ssh-keygen

Generating public/private rsa key pair.

Enter file in which to save the key (/root/.ssh/id_rsa):直接敲击回车或设置密钥的存储路径

Created directory '/root/.ssh'.

Enter passphrase (empty for no passphrase): 直接敲击回车或设置密钥的密码

Enter same passphrase again: 再次敲击回车或设置密钥的密码

Your identification has been saved in /root/.ssh/id_rsa.

Your public key has been saved in /root/.ssh/id_rsa.pub.

The key fingerprint is:

40:32:48:18:e4:ac:c0:c3:c1:ba:7c:6c:3a:a8:b5:22 root@linuxprobe.com

The key's randomart image is:

+--[ RSA 2048]----+

|+*..o . |

|*.o + |

|o*. |

|+ .. |

|o.. S|

|.. + |

|. = |

|E+ . |

|+.o |

+-----------------+

第2步:将生成好的私钥文件传送至远程主机:

[root@linuxprobe ~]# ssh-copy-id 192.168.10.20

The authenticity of host '192.168.10.20 (192.168.10.20)' can't be established.

ECDSA key fingerprint is 4f:a7:91:9e:8d:6f:b9:48:02:32:61:95:48:ed:1e:3f.

Are you sure you want to continue connecting (yes/no)? 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

root@192.168.10.20's password:此处输入远程服务器主机密码

Number of key(s) added: 1

Now try logging into the machine, with: "ssh '192.168.10.20'"

and check to make sure that only the key(s) you wanted were added.

第3步:设置服务器主机只允许密钥验证,拒绝传统口令验证方式,记得修改配置文件后保存并重启sshd服务程序哦~:

[root@linuxprobe ~]# vim /etc/ssh/sshd_config

………………省略部分输出信息………………

74

75 # To disable tunneled clear text passwords, change to no here!

76 #PasswordAuthentication yes

77 #PermitEmptyPasswords no

78 PasswordAuthentication no

79

………………省略部分输出信息………………

[root@linuxprobe ~]# systemctl restart sshd

第4步:在客户端主机尝试登陆到服务端主机,此时无需输入密码口令也可直接验证登陆成功:

[root@linuxprobe ~]# ssh 192.168.10.20

Last login: Mon Apr 13 19:


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/7216752.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-03
下一篇 2023-04-03

发表评论

登录后才能评论

评论列表(0条)

保存