在CentOS/RHEL上設置SSH免密碼登陸

導讀 做爲系統管理員,你計劃在 Linux 上使用 OpenSSH,完成平常工做的自動化,好比文件傳輸、備份數據庫轉儲文件到另外一臺服務器等。爲實現該目標,你須要從主機 A 能自動登陸到主機 B。自動登陸也就是說,要在 shell 腳本中使用ssh,而無須要輸入任何密碼。

在CentOS/RHEL上設置SSH免密碼登陸在CentOS/RHEL上設置SSH免密碼登陸

本文會告訴你怎樣在 CentOS/RHEL 上設置 SSH 免密碼登陸。自動登陸配置好之後,你能夠經過它使用 SSH (Secure Shell)和安全複製 (SCP)來移動文件。node

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

經過配置 SSH 免密碼登陸,你能夠享受到以下的便利:shell

◆用腳本實現平常工做的自動化。數據庫

◆加強 Linux 服務器的安全性。這是防範虛擬專用服務器(VPS)遭受暴力破解攻擊的一個推薦的方法,SSH 密鑰單憑暴力破解是幾乎不可攻破的。安全

什麼是 Ssh-Keygen服務器

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

什麼是 Ssh-Copy-Iddom

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

SSH 密鑰ide

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

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

如下步驟在 CentOS 5/6/七、RHEL 5/6/7 和 Oracle Linux 6/7 上測試經過。

節點1:192.168.0.9 節點2 : 192.168.0.10

步驟一 :

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

[root@node1 ~]# ssh root@192.168.0.10
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.
root@192.168.0.10's password:
Last login: Thu Dec 10 22:04:55 2015 from 192.168.0.1
[root@node2 ~]#

步驟二:

使用 ssh-key-gen 命令生成公鑰和私鑰,這裏要注意的是能夠對私鑰進行加密保護以加強安全性。

[root@node1 ~]# ssh-keygen
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:
b4:51:7e:1e:52:61:cd:fb:b2:98:4b:ad:a1:8b:31:6d root@node1.ehowstuff.local
The key's randomart image is:
+--[ RSA 2048]----+
| . ++ |
| o o o |
| o o o . |
| . o + .. |
| S . . |
| . .. .|
| o E oo.o |
| = ooo. |
| . o.o. |
+-----------------+

步驟三:

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

[root@node1 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub 192.168.0.10
root@192.168.0.10's password:
Now try logging into the machine, with "ssh '192.168.0.10'", and check in:
.ssh/authorized_keys
to make sure we haven't added extra keys that you weren't expecting.

步驟四:

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

[root@node1 ~]# ssh root@192.168.0.10
Last login: Sun Dec 13 14:03:20 2015 from www.ehowstuff.local

免費提供最新Linux技術教程書籍,爲開源技術愛好者努力作得更多更好:http://www.linuxprobe.com/

相關文章
相關標籤/搜索