两台服务器实现免密登录

两台服务器实现免密登录

可以用于配置多台(>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访问248不需要输入密码

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

img

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

不用输入密码,表示配置成功了。

参考

  1. 多台服务器的免密登录
posted @ 2023-02-25 20:56  永恒&  阅读(362)  评论(0编辑  收藏  举报