Linux(Centos)配置OpenSSH無密碼登陸

來源:互聯網
上載者:User

標籤:des   style   http   color   os   io   使用   ar   for   

最近在搭建Hadoop環境需要設定無密碼登陸,所謂無密碼登陸其實是指通過認證認證的方式登陸,使用一種被稱為"公私密金鑰"認證的方式來進行ssh登入。

在linux系統中,ssh是遠程登入的預設工具,因為該工具的協議使用了RSA/DSA的密碼編譯演算法.該工具做linux系統的遠端管理是非常安全的。telnet,因為其不安全性,在linux系統中被擱置使用了。

       " 公私密金鑰"認證方式簡單的解釋:首先在用戶端上建立一對公私密金鑰 (公開金鑰檔案:~/.ssh/id_rsa.pub; 私密金鑰檔案:~/.ssh/id_rsa)。然後把公開金鑰放到伺服器上(~/.ssh/authorized_keys), 自己保留好私密金鑰.在使用ssh登入時,ssh程式會發送私密金鑰去和伺服器上的公開金鑰做匹配.如果匹配成功就可以登入了。 


       在Ubuntu和Cygwin 配置都很順利,而在Centos系統中配置時遇到了很多問題。故此文以Centos(Centos5 ) 為例詳細講解如何配置認證驗證登陸,具體操作步驟如下:

 

1. 確認系統已經安裝好OpenSSH的server 和client

    安裝步驟這裡不再講述,不是本文的重點。

 

2. 確認本機sshd的設定檔(需要root許可權)

    $ vi /etc/ssh/sshd_config

    找到以下內容,並去掉注釋符”#“

    RSAAuthentication yes
    PubkeyAuthentication yes
    AuthorizedKeysFile      .ssh/authorized_keys

 

3.  如果修改了設定檔需要重啟sshd服務 (需要root許可權)

   $ vi /sbin/service sshd restart

 

 

4. ssh登陸系統 後執行測試命令:

   $ ssh localhost

   斷行符號會提示你輸入密碼,因為此時我們還沒有產生認證

 

5.產生認證公私密金鑰的步驟:

   $ ssh-keygen -t dsa -P ‘‘ -f ~/.ssh/id_dsa 
   $ cat ~/.ssh/id_dsa.pub >> ~/.ssh/authorized_keys

 

6.測試登陸 ssh localhost:

   $ ssh localhost

 正常情況下會登陸成功,顯示一些成功登陸資訊,如果失敗請看下面的 一般調試步驟

 

7.一般調試步驟

   本人在配置時就失敗了,按照以上步驟依舊提示要輸入密碼。於是用ssh -v 顯示詳細的登陸資訊尋找原因:

   $ ssh -v localhost

   斷行符號顯示了詳細的登陸資訊如下:

 

。。。。。。省略
debug1: Authentications that can continue: publickey,gssapi-with-mic,password
debug1: Next authentication method: gssapi-with-mic
debug1: Unspecified GSS failure. Minor code may provide more information
Unknown code krb5 195 

debug1: Unspecified GSS failure. Minor code may provide more information
Unknown code krb5 195

debug1: Unspecified GSS failure. Minor code may provide more information
Unknown code krb5 195 

debug1: Next authentication method: publickey
debug1: Trying private key: /home/huaxia/.ssh/identity
debug1: Trying private key: /home/huaxia/.ssh/id_rsa
debug1: Offering public key: /home/huaxia/.ssh/id_dsa
debug1: Authentications that can continue: publickey,gssapi-with-mic,password
debug1: Next authentication method: password
huaxia @localhost ‘s password: 

 

同時用root使用者登陸查看系統的記錄檔:

 

   $tail /var/log/secure -n 20 

 

。。。。。。省略
Jul 13 11:21:05 shnap sshd[3955]: Accepted password for huaxia from 192.168.8.253 port 51837 ssh2
Jul 13 11:21:05 shnap sshd[3955]: pam_unix(sshd:session): session opened for user huaxia by (uid=0)
Jul 13 11:21:47 shnap sshd[4024]: Connection closed by 127.0.0.1
Jul 13 11:25:28 shnap sshd[4150]:  Authentication refused: bad ownership or modes for file /home/huaxia/.ssh/authorized_keys 
Jul 13 11:25:28 shnap sshd[4150]:  Authentication refused: bad ownership or modes for file /home/huaxia/.ssh/authorized_keys 
Jul 13 11:26:30 shnap sshd[4151]: Connection closed by 127.0.0.1
。。。。。。省略

 

   從上面的日誌資訊中可知檔案/home/huaxia/.ssh/authorized_keys 的許可權有問題。

   查看/home/huaxia/.ssh/ 下檔案的詳細資料如下:

 

$ ls -lh ~/.ssh/
總計 16K
-rw-rw-r-- 1 huaxia huaxia 602 07-13 11:22 authorized_keys
-rw------- 1 huaxia huaxia 672 07-13 11:22 id_dsa
-rw-r--r-- 1 huaxia huaxia 602 07-13 11:22 id_dsa.pub
-rw-r--r-- 1 huaxia huaxia 391 07-13 11:21 known_hosts

   修改檔案authorized_keys的許可權(許可權的設定非常重要,因為不安全的設定安全設定,會讓你不能使用RSA功能 ):

 

$ chmod 600 ~/.ssh/authorized_keys

 

   再次測試登陸如下:

 

$ ssh localhost
Last login: Wed Jul 13 14:04:06 2011 from 192.168.8.253

 

   看到這樣的資訊表示已經成功實現了原生無密碼登陸。

 

8.認證登陸遠程伺服器(遠程伺服器OpenSSH的服務當然要啟動)

 

    拷貝本地生產的key到遠程伺服器端(兩種方法)

    方法一:

$cat ~/.ssh/id_rsa.pub | ssh 遠端使用者名@遠程伺服器ip ‘cat - >> ~/.ssh/authorized_keys‘

 

    方法二:

    在本機上執行:

$ scp ~/.ssh/id_dsa.pub [email protected]:/home/michael/

    登陸遠程伺服器[email protected] 後執行:

 

$ cat id_dsa.pub >> ~/.ssh/authorized_keys

 

   本機遠程登陸192.168.8.148的測試:

 

$ssh [email protected]
Linux michael-VirtualBox 2.6.35-22-generic #33-Ubuntu SMP Sun Sep 19 20:34:50 UTC 2010 i686 GNU/Linux
Ubuntu 10.10

Welcome to Ubuntu!
* Documentation: https://help.ubuntu.com/

216 packages can be updated.
71 updates are security updates.

New release ‘natty‘ available.
Run ‘do-release-upgrade‘ to upgrade to it.

Last login: Wed Jul 13 14:46:37 2011 from michael-virtualbox
[email protected]:~$ 

    可見已經成功登陸。

 

     如果登陸測試不成功,需要修改遠程伺服器192.168.8.148上的檔案authorized_keys的許可權(許可權的設定非常重要,因為不安全的設定安全設定,會讓你不能使用RSA功能 )

 

chmod 600 ~/.ssh/authorized_keys

 記得關閉selinux

查看SELinux狀態:

1、/usr/sbin/sestatus -v      ##如果SELinux status參數為enabled即為開啟狀態

SELinux status:                 enabled

2、getenforce                 ##也可以用這個命令檢查

關閉SELinux:

1、臨時關閉(不用重啟機器):

setenforce 0                  ##設定SELinux 成為permissive模式

                              ##setenforce 1 設定SELinux 成為enforcing模式

2、修改設定檔需要重啟機器:

修改/etc/selinux/config 檔案

將SELINUX=enforcing改為SELINUX=disabled

重啟機器即可

 

Linux(Centos)配置OpenSSH無密碼登陸

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.