第一步,生成自己公鑰, 私鑰
1: ssh-keygen -t rsa
2:
3: root@yjlml:~# ssh-keygen -t rsa
4: Generating public/private rsa key pair.
5: Enter file in which to save the key (/root/.ssh/id_rsa):
6: Enter passphrase (empty for no passphrase):
7: Enter same passphrase again:
8: Your identification has been saved in /root/.ssh/id_rsa.
9: Your public key has been saved in /root/.ssh/id_rsa.pub.
10: The key fingerprint is:
11: 3e:6e:d9:12:7c:f8:6f:18:f6:65:cb:6e:16:4f:83:43 root@yjlml
12: The key's randomart image is:
13: +--[ RSA 2048]----+
14: | |
15: | |
16: | |
17: | E |
18: | .S. . . |
19: | .+ + o+..|
20: | oB + +.=.|
21: | .+.+ o = .|
22: | ... o.+. |
23: +-----------------+
中間連續按幾次回車, 使用預設檔案名, 并不輸入密碼。
二, 把你的公鑰,拷貝到遠端機器的 “~/.ssh/authorized_keys” 檔案中。
剛才生成的公鑰在: ~/.ssh/id_rsa.pub, 裡面是文本,直接複制出來,粘貼到遠端的“~/.ssh/authorized_keys” 檔案中即可。 如果沒有這個檔案,自己建立一個。
這個檔案可以儲存多個公鑰,隻需要連續放在裡面就可以了。
拷貝的方法有很多。自己想辦法。
拷貝完成之後,重新開機一下ssh服務。
service ssh restart
然後測試是否成功:
1: root@yjlml:~# ssh root@190.***.***.*** -p 22 ls
這裡故意指定了一個端口22, 其實可以省略的。如果你的ssh端口不是22可以這樣指定。
這句話是在遠端執行一個 指令 ls, 看能不能執行成功。
如果能看到ls的結果。那就是成功了。
三, 錯誤處理
如果通路不成功,請檢查遠端機器的 “/etc/ssh/sshd_config ”檔案:
找到其中的這一段。
6: RSAAuthentication yes
7: PubkeyAuthentication yes
8: #AuthorizedKeysFile %h/.ssh/authorized_keys

注意,必須開啟 公鑰認證, 檢查公鑰檔案。
改完之後,重新開機遠端ssh服務: service ssh restart
歡迎大家通路我的個人獨立部落格: http://byneil.com
來自 Neil的部落格 http://byNeil.com