兩臺伺服器實現免密登入

永恆&發表於2023-02-25

兩臺伺服器實現免密登入

可以用於配置多臺(>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. 多臺伺服器的免密登入

相關文章