linux配置免密码登录各节点

[root@node1 jdk1.7.0_80]# ssh root@node2 "mkdir .ssh;chmod 0700 .ssh"
The authenticity of host 'node2 (192.168.137.102)' can't be established.
ECDSA key fingerprint is SHA256:pOigsCPjt5BQwCdHruKtW9JVNxY5V80/cIv3fjJjqcs.
ECDSA key fingerprint is MD5:22:b9:63:95:42:77:7d:9e:39:be:36:f7:89:1b:1d:4d.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'node2,192.168.137.102' (ECDSA) to the list of known hosts.
root@node2's password: 
[root@node1 jdk1.7.0_80]#  scp ~/.ssh/id_rsa.pub root@node2:.ssh/id_rsa.pub
root@node2's password: 
id_rsa.pub                                                                           100%  392   205.9KB/s   00:00    
[root@node1 jdk1.7.0_80]# ssh root@node2
Last login: Mon Apr  1 04:36:23 2019 from node1
[root@node2 ~]# exit
登出
Connection to node2 closed.
[root@node1 jdk1.7.0_80]# 

A为本地主机(即用于控制其他主机的机器) ;

B为远程主机(即被控制的机器Server), 假如ip为192.168.137.102 ;

A和B的系统都是Linux

 

在A上的命令:

# ssh-keygen -t rsa (连续三次回车,即在本地生成了公钥和私钥,不设置密码)

# ssh root@node2 "mkdir .ssh;chmod 0700 .ssh" (需要输入密码, 注:必须将.ssh的权限设为700)

# scp ~/.ssh/id_rsa.pub root@node2 :.ssh/id_rsa.pub (需要输入密码)

(使用node2的前提是你在本节点的hosts中配置节点名和ip的对应,比如在node1节点的/etc/hosts下配置了 192.168.137.102  node2)

在B上的命令:

# touch /root/.ssh/authorized_keys (如果已经存在这个文件, 跳过这条)

# chmod 600 ~/.ssh/authorized_keys  (# 注意: 必须将~/.ssh/authorized_keys的权限改为600, 该文件用于保存ssh客户端生成的公钥,可以修改服务器的ssh服务端配置文件/etc/ssh/sshd_config来指定其他文件名)

# cat /root/.ssh/id_rsa.pub  >> /root/.ssh/authorized_keys (将id_rsa.pub的内容追加到 authorized_keys 中, 注意不要用 > ,否则会清空原有的内容,使其他人无法使用原有的密钥登录)

 

回到A机器:

# ssh [email protected] (不需要密码, 登录成功)

你可能感兴趣的:(虚拟机)