背景:机器A免密码登录机器B,需要用bash实现;之后服务器C也可以直接用bash代码免密登录机器B;
机器A上配置: (1)root登录;生成公钥私钥
$ ssh-keygen
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.
The key fingerprint is:
SHA256:
root@localhost.localdomain
The key's randomart image is:
+---[RSA 2048]----+
| .*BX+oB.o|
| .*B+B =.|
| . . o o.* B o|
|E. . . o . o.o.*.|
|... S .oo.o..|
|. .. .+ o |
| . . o |
| o |
| . |
+----[SHA256]-----+
(2)拷贝公钥到服务器B
ssh-copy-id -i ~/.ssh/id_rsa.pub -p port 'username@ip'
执行结果如下, 可以到服务器B上去看下是否有文件生成/root/.ssh/authorized_keys /root/.ssh/authorized_keys文件内容如下图: (3)服务器A免提示,免密登录服务器B;
ssh-keyscan -p port ip > ~/.ssh/known_hosts //替换为远程服务器的地址
chmod 644 ~/.ssh/known_hosts
尝试免密登录服务器B,免提示登录服务器B
ssh -p port ip
结果如下,不需要登录提示
(4)服务器C免密免提示登录服务器B; 前提:自行拷贝服务器A生成的文件id_rsa文件到服务器C(/root/.ssh/id_rsa)
eval $(ssh-agent -s)
cat id_rsa | ssh-add -
mkdir -p ~/.ssh
chmod 700 ~/.ssh
ssh-keyscan -p port ip > ~/.ssh/known_hosts //替换为远程服务器的地址
chmod 644 ~/.ssh/known_hosts
执行结果如下
参考:
https://blog.csdn.net/zt15732625878/article/details/119558733 https://blog.csdn.net/weixin_47656385/article/details/123043020
|