配置多台主机免密登录

基本流程

[root@freedom yum]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
/root/.ssh/id_rsa already exists.
Overwrite (y/n)? y
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:+3D3s97BFqm01s6EapZ09e3PgGphXVjnn8WVfUeNxnI root@freedom
The key's randomart image is:
+---[RSA 2048]----+
|             . o=|
|            . E.B|
|             * ++|
|            . ..=|
|        S  . o.o*|
|         .o.oo*.=|
|        o.o.==.B |
|         +.*o.*+o|
|         .=. .+==|
+----[SHA256]-----+
[root@freedom yum]# vim /etc/ssh/sshd_config
#关闭密码登录认证
###
PasswordAuthentication no
###
[root@freedom yum]# cd ~/.ssh
[root@freedom .ssh]# cp id_rsa.pub authorized_keys
[root@freedom .ssh]# ls
authorized_keys  id_rsa  id_rsa.pub  known_hosts  rsa
[root@freedom .ssh]# sz id_rsa
[root@freedom .ssh]# systemctl restart sshd
[root@S1 .ssh]# scp -r /root/.ssh 192.168.59.102:/root
The authenticity of host '192.168.59.102 (192.168.59.102)' can't be established.
ECDSA key fingerprint is SHA256:HEBSwDAQicap4rafB7tJ5BX2kUyukLB8TQ3VPFg9QsU.
ECDSA key fingerprint is MD5:d3:89:00:c5:c5:85:5a:02:8f:bd:c2:0a:bd:4d:80:94.
Are you sure you want to continue connecting (yes/no)? ^[[A^H^H^H^C[root@S1 .ssh]# 
[root@S1 .ssh]# scp -r /root/.ssh 192.168.59.102:/root
The authenticity of host '192.168.59.102 (192.168.59.102)' can't be established.
ECDSA key fingerprint is SHA256:HEBSwDAQicap4rafB7tJ5BX2kUyukLB8TQ3VPFg9QsU.
ECDSA key fingerprint is MD5:d3:89:00:c5:c5:85:5a:02:8f:bd:c2:0a:bd:4d:80:94.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.59.102' (ECDSA) to the list of known hosts.
[email protected]'s password: 
id_rsa                                                                                                                                                  100% 1675     2.4MB/s   00:00    
id_rsa.pub                                                                                                                                              100%  389   508.2KB/s   00:00    
authorized_keys                                                                                                                                         100%  389   547.4KB/s   00:00    
known_hosts                                                                                                                                             100%  176   258.9KB/s   00:00    
[root@S1 .ssh]# ssh 192.168.59.102
Last failed login: Sat Jul 27 18:25:43 CST 2019 on tty1
There was 1 failed login attempt since the last successful login.
Last login: Sat Jul 27 14:19:50 2019 from 192.168.59.1
[root@S2 ~]# vim /etc/ssh/sshd_config 

shell

虽然流程也就十个左右的命令就能搞定,但是我这么懒当然是写成脚本用一辈子了,所以恭喜大家。

#!/bin/bash
#本脚本用于配置多台主机之间自动完成免密钥登录

ssh-keygen -t rsa -f /root/.ssh/id_rsa -P ''
sed -i '65s/yes/no/g' /etc/ssh/sshd_config
cp /root/.ssh/id_rsa.pub /root/.ssh/authorized_keys
yum install sshpass -y -q
sed -i 35a'StrictHostKeyChecking no' /etc/ssh/ssh_config
systemctl restart sshd

while :
do
read -p "请输入您要登陆的主机IP(回车退出):" a
	if [ -z $a ]
	then
	break
	fi
read -p "请输入密码:" b
sshpass -p "$b" scp -r /root/.ssh "$a":/root
ssh "$a" "sed -i '65s/yes/no/g' /etc/ssh/sshd_config && systemctl restart sshd"
done

计算机英语

keygen 注册机
authentication 认证

你可能感兴趣的:(自动化运维)