網上該類教程很多,但還是記錄下,方便以後查詢的時候使用。
兩台伺服器,內網ip分別是:
172.16.3.91 (本地機)
172.16.3.92 (遠程機)
現在想在本地機上通過ssh 172.16.3.92登陸到遠程機而不要求輸入密碼。
1.在本地機上用ssh-keygen 建立公開金鑰私密金鑰。
[root@rs-1 ~]# 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:
dc:fc:ea:b8:bb:74:d7:75:4b:6a:27:82:28:27:be:d6 root@rs-1
這個過程會提示輸入密碼,記住直接按斷行符號就行,不要輸入密碼!要是輸入了密碼,一會訪問的時候就需要輸入密碼了。
建立之後能看到如下兩個檔案
[root@rs-1 .ssh]# ll
total 24
-rw------- 1 root root 1675 May 8 07:18 id_rsa
-rw-r--r-- 1 root root 391 May 8 07:18 id_rsa.pub
id_rsa是私密金鑰
id_rsa.pub是公開金鑰, 主要給遠程機使用的
2.把公開金鑰傳到遠程機上去。
[root@rs-1 .ssh]# scp id_rsa.pub root@172.16.3.92:~/.ssh/authorized_keys
Address 172.16.3.92 maps to localhost, but this does not map back to the address - POSSIBLE BREAK-IN ATTEMPT!
root@172.16.3.92's password:
id_rsa.pub 100% 391 0.4KB/s 00:00
3.ssh連結時候就不要求輸入密碼了,直接登陸到遠程機
[root@rs-1 .ssh]# ssh 172.16.3.92
Address 172.16.3.92 maps to localhost, but this does not map back to the address - POSSIBLE BREAK-IN ATTEMPT!
Last login: Thu May 8 07:19:28 2014 from 172.16.3.91
[root@rs-2 ~]#