SSH - QLGQ/learning-python GitHub Wiki
Introduction
为了避免Ansible下发指令时输入目标主机密码,通过证书签名,通过证书签名达到SSH无密码是一个好的方案,推荐使用ssh-keygen与ssh-copy-id来实现快速证书的生成及公钥下发,其中ssh-keygen生成一对密钥,使用ssh-copy-id来下发生成的公钥。
Operation
在主控端主机创建密钥,执行:ssh-keygen -t rsa
,有询问直接按回车键即可,将在/root/.ssh下生成一对密钥,其中id_rsa为私钥,id_rsa.pub为公钥(需要下发到被控主机用户.ssh目录,同时要求重命名成authorized_keys文件)。
ubuntu@et-wuqiang:~$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/ubuntu/.ssh/id_rsa):(回车)
Enter passphrase (empty for no passphrase):(回车)
Enter same passphrase again:(回车)
Your identification has been saved in /home/ubuntu/.ssh/id_rsa.
Your public key has been saved in /home/ubuntu/.ssh/id_rsa.pub.
The key fingerprint is:
92:0d:64:7d:37:b2:86:de:8f:ff:cd:eb:48:25:7b:7f ubuntu@et-wuqiang
The key's randomart image is:
+--[ RSA 2048]----+
| o. |
| o . o o |
| . o + . |
| +. o |
| o.So . . |
| .. . + |
| o o . |
| . o ooE|
| ..ooo*|
+-----------------+
接下来同步公钥文件id_rsa.pub到目标主机,推荐使用ssh-copy-id公钥拷贝工具,命令格式:/usr/bin/ssh-copy-id [-i [identity_file]] [user@]machine
。本示例中我们输入以下命令同步公钥至192.168.1.21和192.168.1.22主机。
#ssh-copy-id -i /root/.ssh/id_rsa.pub [email protected]
#ssh-copy-id -i /root/.ssh/id_rsa.pub [email protected]
校验SSH无密码配置是否成功,运行ssh [email protected]
,如直接进入目标root账号提示符,则说明配置成功。
退出目标主机,运行exit
即可。