ssh免密碼登錄,公鑰登錄

SSH 是開源的,是用於遠程登錄的最爲可靠的網絡協議。系統管理員用它來執行命令,以及通過 SCP 協議在網絡上向另一臺電腦傳輸文件。

通過配置 SSH 免密碼登錄,你可以享受到如下的便利:

  • 用腳本實現日常工作的自動化。
  • 增強 Linux 服務器的安全性。這是防範虛擬專用服務器(VPS)遭受暴力破解攻擊的一個推薦的方法,SSH 密鑰單憑暴力破解是幾乎不可攻破的。

什麼是 ssh-keygen

ssh-keygen 是一個用來生成、創建和管理 SSH 認證用的公私鑰的工具。通過 ssh-keygen 命令,用戶可以創建支持SSH1 和 SSH2 兩個協議的密鑰。ssh-keygen 爲 SSH1 協議創建 RSA 密鑰,SSH2 則可以是 RSA 或 DSA。

什麼是 ssh-copy-id

ssh-copy-id 是用來將本地公鑰拷貝到遠程的 authorized_keys 文件的腳本命令,它還會將身份標識文件追加到遠程機器的 ~/.ssh/authorized_keys 文件中,並給遠程主機的用戶主目錄適當的的權限。

SSH 密鑰

SSH 密鑰爲登錄 Linux 服務器提供了更好且安全的機制。運行 ssh-keygen 後,將會生成公私密鑰對。你可以將公鑰放置到任意服務器,從持有私鑰的客戶端連接到服務器的時,會用它來解鎖。兩者匹配時,系統無需密碼就能解除鎖定。

在 CentOS 和 RHEL 上設置免密碼登錄 SSH

以下步驟在 CentOS 5/6/7、RHEL 5/6/7 和 Oracle Linux 6/7 上測試通過。

節點1 : 192.168.0.9 節點2 : 192.168.l.10

簡單示例:

在hdp1 172.16.1.124(Monitor)上用root用戶執行:


ssh-keygen -t rsa
ssh-copy-id -i /root/.ssh/id_rsa.pub [email protected]
ssh-copy-id -i /root/.ssh/id_rsa.pub [email protected]
ssh-copy-id -i /root/.ssh/id_rsa.pub [email protected]

 

詳細講解:

步驟1 :

測試節點1到節點2的連接和訪問:

1.  `[root@node1 ~]#  ssh [email protected]

 `The authenticity of host '192.168.0.10 (192.168.0.10)' can't be established.`
 `RSA key fingerprint is 6d:8f:63:9b:3b:63:e1:72:b3:06:a4:e4:f4:37:21:42.`
 `Are you sure you want to continue connecting (yes/no)? yes`
 `Warning: Permanently added '192.168.0.10' (RSA) to the list of known hosts.`
 `[email protected]'s password:`
 `Last  login:  Thu  Dec  10  22:04:55  2015  from  192.168.0.1`
 `[root@node2 ~]#`

步驟二:

使用 ssh-keygen -t rsa 命令生成公鑰和私鑰,這裏要注意的是可以對私鑰進行加密保護以增強安全性。 

1.  `[root@node1 ~]#  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:
SHA256:16BxFzBiqvKoFyELPpl2pSL1udamvOuckW5yd7WDntc [email protected]
The key's randomart image is:
+---[RSA 2048]----+
|        o o..    |
|       o . . .   |
|      . . o .    |
|o o  o   + +     |
|o++o+.  S . .    |
|oB.*o.   ..      |
|o =.+o   o o     |
| .o+=o+ o.+ E    |
|.. *X* oo. .     |
+----[SHA256]-----+


步驟三:

用 ssh-copy-id 命令將公鑰複製或上傳到遠程主機,並將身份標識文件追加到節點2的 ~/.ssh/authorized_keys 中:

在192.168.0.9 上用root用戶執行

[root@node1 ~]#  ssh-copy-id -i /root/.ssh/id_rsa.pub [email protected]

/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
[email protected]'s password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh '[email protected]'"
and check to make sure that only the key(s) you wanted were added.


然後在需要添加信任的機器重複上述操作

步驟四:

驗證免密碼 SSH 登錄節點2:

1.  `[root@node1 ~]#  ssh [email protected]

 `Last  login:  Sun  Dec  13  14:03:20  2015  from www.ehowstuff.local`

我希望這篇文章能幫助到你,爲你提供 SSH 免密碼登錄 CentOS / RHEL 的基本認知和快速指南。



 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章