# 免输入生成密钥对 [root@node1 ~] ssh-keygen -t rsa -P '' -f ~/.ssh/id_rsa Generating public/private rsa key pair. 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:sKNCpX38EMmqUAXxvW3eBInQTqPzpGwkb96WfHWPfzk root@node1 The key randomart image is: +---[RSA 2048]----+ | ooo. | | o ++o . | | . o+B.o | | ..++oo* . | |. o=o=B S . | | o .*o.* o. . | | o+.o .o... o .| | .. = . . .E.| | . . ....| +----[SHA256]-----+ # 拷贝node1公钥到node2 [root@node1 ~] ssh-copy-id node2 /usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub" /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@node2 password: Number of key(s) added: 1 Now try logging into the machine, with: "ssh 'node2'" and check to make sure that only the key(s) you wanted were added.
解决免密后第一次访问确认连接提示
1 2 3 4
# vim /etc/ssh/ssh_config 把# StrictHostKeyChecking ask前面注释去掉改成 StrictHostKeyChecking no # 或者直接输入命令 sed -i /Checking/a'StrictHostKeyChecking no' /etc/ssh/ssh_config
loop_auto_keygen(){ for ip in $@ do echo "========生成$ip的密钥========" auto_keygen $ip done }
loop_copy_hosts(){ for ip in $@ do echo "========拷贝$ip的hosts========" copy_host $ip done }
loop_copy_key(){ for ip in $@ do echo "========拷贝$ip的密钥========" copy_key $ip done } # add_host loop_auto_keygen $ips loop_copy_hosts $ips loop_copy_key $ips