天天看點

如何在 CentOS / RHEL 上設定 SSH 免密碼登入如何在 CentOS / RHEL 上設定 SSH 免密碼登入

如何在 CentOS / RHEL 上設定 SSH 免密碼登入如何在 CentOS / RHEL 上設定 SSH 免密碼登入

作為系統管理者,你計劃在 linux 上使用 openssh,完成日常工作的自動化,比如檔案傳輸、備份資料庫轉儲檔案到另一台伺服器等。為實作該目标,你需要從主機 a 能自動登入到主機 b。自動登入也就是說,要在 shell 腳本中使用ssh,而無需要輸入任何密碼。

本文會告訴你怎樣在 centos/rhel 上設定 ssh 免密碼登入。自動登入配置好以後,你可以通過它使用 ssh (secure shell)和安全複制 (scp)來移動檔案。

ssh 是開源的,是用于遠端登入的最為可靠的網絡協定。系統管理者用它來執行指令,以及通過 scp 協定在網絡上向另一台電腦傳輸檔案。

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

用腳本實作日常工作的自動化。

增強 linux 伺服器的安全性。這是防範虛拟專用伺服器(vps)遭受暴力破解攻擊的一個推薦的方法,ssh 密鑰單憑暴力破解是幾乎不可攻破的。

<a></a>

ssh-keygen 是一個用來生成、建立和管理 ssh 認證用的公私鑰的工具。通過 ssh-keygen 指令,使用者可以建立支援ssh1 和 ssh2 兩個協定的密鑰。ssh-keygen 為 ssh1 協定建立 rsa 密鑰,ssh2 則可以是 rsa 或 dsa。

ssh-copy-id 是用來将本地公鑰拷貝到遠端的 authorized_keys 檔案的腳本指令,它還會将身份辨別檔案追加到遠端機器的 ~/.ssh/authorized_keys 檔案中,并給遠端主機的使用者主目錄适當的的權限。

ssh 密鑰為登入 linux 伺服器提供了更好且安全的機制。運作 ssh-keygen 後,将會生成公私密鑰對。你可以将公鑰放置到任意伺服器,從持有私鑰的用戶端連接配接到伺服器的時,會用它來解鎖。兩者比對時,系統無需密碼就能解除鎖定。

以下步驟在 centos 5/6/7、rhel 5/6/7 和 oracle linux 6/7 上測試通過。

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

測試節點1到節點2的連接配接和通路:

<code>[root@node1 ~]# ssh [email protected]</code>

<code>the authenticity of host '192.168.0.10 (192.168.0.10)' can't be established.</code>

<code>rsa key fingerprint is 6d:8f:63:9b:3b:63:e1:72:b3:06:a4:e4:f4:37:21:42.</code>

<code>are you sure you want to continue connecting (yes/no)? yes</code>

<code>warning: permanently added '192.168.0.10' (rsa) to the list of known hosts.</code>

<code>[email protected]'s password:</code>

<code>last login: thu dec 10 22:04:55 2015 from 192.168.0.1</code>

<code>[root@node2 ~]#</code>

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

<code>[root@node1 ~]# ssh-keygen</code>

<code>generating public/private rsa key pair.</code>

<code>enter file in which to save the key (/root/.ssh/id_rsa):</code>

<code>enter passphrase (empty for no passphrase):</code>

<code>enter same passphrase again:</code>

<code>your identification has been saved in /root/.ssh/id_rsa.</code>

<code>your public key has been saved in /root/.ssh/id_rsa.pub.</code>

<code>the key fingerprint is:</code>

<code>b4:51:7e:1e:52:61:cd:fb:b2:98:4b:ad:a1:8b:31:6d [email protected]</code>

<code>the key's randomart image is:</code>

<code>+--[ rsa 2048]----+</code>

<code>| . ++ |</code>

<code>| o o o |</code>

<code>| o o o . |</code>

<code>| . o + .. |</code>

<code>| s . . |</code>

<code>| . .. .|</code>

<code>| o e oo.o |</code>

<code>| = ooo. |</code>

<code>| . o.o. |</code>

<code>+-----------------+</code>

用 ssh-copy-id 指令将公鑰複制或上傳到遠端主機,并将身份辨別檔案追加到節點2的 ~/.ssh/authorized_keys 中:

<code>[root@node1 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub 192.168.0.10</code>

<code>now try logging into the machine, with "ssh '192.168.0.10'", and check in:</code>

<code></code>

<code>.ssh/authorized_keys</code>

<code>to make sure we haven't added extra keys that you weren't expecting.</code>

驗證免密碼 ssh 登入節點2:

<code>last login: sun dec 13 14:03:20 2015 from www.ehowstuff.local</code>

我希望這篇文章能幫助到你,為你提供 ssh 免密碼登入 centos / rhel 的基本認知和快速指南。

本文來自雲栖社群合作夥伴“linux中國”

原文釋出時間為:2013-04-02.

上一篇: 類型轉換
下一篇: 類型轉換

繼續閱讀