两台服务器实现免密登录
可以用于配置多台(>2)服务器之间的免密登录
使用shell的远程操作命令ssh,通过ssh协议,连接192.168.100.148
服务器,执行echo 1
命令。
ssh 192.168.100.148 'echo 1'
输出如下:
[root@hadoop001 ~]# ssh 192.168.100.148 'echo 1'
root@192.168.100.148's password:
1
需要输入192.168.100.148的登录密码
现在实现免密登录
目标:231访问148不需要输入密码
1、231生成密钥对
# cd /root/.ssh (如果没有.ssh, 请在root目录下mkdir .ssh chmod 755 .ssh)
# ssh-keygen -t rsa
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:06:96:6b:56:0c:33:a4:24:16:8c:06:35:9b:98:8b:e7 root@localhost.localdomain
2、将生成的公钥id_rsa.pub
复制到148机器上
ssh-copy-id -i root/.ssh/id_rsa.pub root@192.168.100.148
说明:将231上生成的公钥文件id_rsa.pub
复制到148的/root/.ssh/authorized_keys
里,ssh-copy-id -i
表示追加写的方式添加到authorized_keys
。可以实现多个机器访问148.
[root@hadoop001 .ssh]# ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.100.148
/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@192.168.100.148's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'root@192.168.100.148'"
and check to make sure that only the key(s) you wanted were added.
3、验证
[root@hadoop001 .ssh]# ssh root@192.168.100.148 'echo 1'
1
不用输入密码,表示配置成功了。
4、总结
为了实现A机器免密登录B机器,需要
1、在A机器上生成密钥对
2、将公钥复制到B机器上