Ubuntu和CentOS如何配置SSH使得無密碼登陸

來源:互聯網
上載者:User

在使用Hadoop的時候,一般配置SSH使得我們可以無密碼登入到主機,下面分別以Ubuntu和CentOS兩個平台來舉例說明如何配置SSH使得我們可以無密碼登入到主機,當然,你得先安裝好SSH伺服器,並開啟(關於如何在Linux平台下安裝好SSH請參加本部落格的《Linux平台下安裝SSH》 )Ubuntu配置步驟如下所示:

[bkjia@localhost ~]$ ssh-keygen -t dsa -P ''
Generating public/private dsa key pair.
Enter file in which to save the key (/home/bkjia/.ssh/id_dsa):
Created directory '/home/bkjia/.ssh'.
Your identification has been saved in /home/bkjia/.ssh/id_dsa.
Your public key has been saved in /home/bkjia/.ssh/id_dsa.pub.
The key fingerprint is:
bd:2c:ed:ab:6d:a9:b2:45:88:32:08:5a:d2:d9:ad:cc bkjia@ubuntu
The key's randomart image is:
+--[ DSA 1024]----+
|                |
| . o .          |
|o + . .          |
|o+ o o . .      |
|o o E . S .      |
|  o  . o .    |
|        o +.    |
|      .. +o      |
|      .oo++.    |
+-----------------+
這樣就會在bkjia使用者的/home/bkjia/.ssh/目錄下產生id_dsa和id_dsa.pub兩個檔案,輸出如下:

[bkjia@ubuntu ~]$ cd /home/bkjia/.ssh/
[bkjia@ubuntu:~/.ssh]$ ls -l
total 8
-rw------- 1 bkjia bkjia 668 2013-10-21 02:51 id_dsa
-rw-r--r-- 1 bkjia bkjia 600 2013-10-21 02:51 id_dsa.pub
將id_dsa.pub裡面的內容加到用於認證的公開金鑰檔案中,命令如下(注意:不要複製,一定要用cat去操作,不信你可以去試試。):

[bkjia@ubuntu:~/.ssh]$ cat id_dsa.pub >> authorized_keys
輸入下面命令,如果顯示如下類似的資訊,就說明配置好了!

[bkjia@ubuntu:~/.ssh]$ ssh localhost
The authenticity of host 'localhost (::1)' can't be established.
RSA key fingerprint is 22:9b:94:45:dc:f0:c8:02:03:b2:a1:30:d3:04:92:01.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'localhost' (RSA) to the list of known hosts.
linux ubuntu 2.6.32-38-generic #83-Ubuntu SMP Wed Jan 4 11:12:07 UTC 2012
x86_64 GNU/linux
Ubuntu 10.04.4 LTS

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

242 packages can be updated.
213 updates are security updates.

New release 'precise' available.
Run 'do-release-upgrade' to upgrade to it.

The programs included with the Ubuntu system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Ubuntu comes with ABSOLUTELY NO WARRANTY, to the extent permitted by
applicable law.

[bkjia@localhost ~]$
  但是實際上,很多人都沒有這麼順利的,一般的問題都是檔案使用權限設定的不對,請把.ssh和authorized_keys的存取權限分別設定為755個600(也就是只有自己對上面兩個檔案有寫入權限),命令如下:

[bkjia@localhost ~]$ chmod 755 /home/bkjia/.ssh
[bkjia@localhost ~]$ chmod 600 /home/bkjia/.ssh/authorized_keys
假如這台伺服器上面還有test使用者,如何使得bkjia使用者能夠無密碼登入到test(IP地址為192.168.142.129)呢?執行下面命令

[bkjia@localhost ~]$ cat /home/bkjia/.ssh/id_dsa.pub |
ssh test@192.168.142.129 'cat - >> ~/.ssh/authorized_keys'
之後,在bkjia使用者下執行下面命令

[bkjia@localhost ~]$ ssh test@192.168.142.129
linux ubuntu 2.6.32-38-generic #83-Ubuntu SMP Wed Jan 4 11:12:07
 UTC 2012 x86_64 GNU/linux
Ubuntu 10.04.4 LTS

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

240 packages can be updated.
213 updates are security updates.

New release 'precise' available.
Run 'do-release-upgrade' to upgrade to it.

Last login: Mon Oct 21 02:48:01 2013 from localhost
[test@localhost ~]$
如果出現上述類似的資訊,說明成功了!登陸到test使用者了。注意上面的資訊只有第一次登入的時候才會顯示,以後登陸只會顯示下面類似的資訊:

[bkjia@localhost ~]$ ssh test@192.168.142.129
Last login: Mon Oct 21 02:49:51 2013 from localhost
[test@localhost ~]$
上面是以Ubuntu平台為例子,下面來說說怎麼在CentOS平台上面配置,(CentOS由於許可權比Ubuntu的更加嚴格,所以配置起來比較麻煩),首先,我們需要設定一下/etc/ssh/sshd_config檔案,請將/etc/ssh/sshd_config檔案中下面三行的注釋去掉:

[root@localhost ~]# vim /etc/ssh/sshd_config
RSAAuthentication yes
PubkeyAuthentication yes
AuthorizedKeysFile      .ssh/authorized_keys
其餘的步驟和開始的一樣如下所示:

[bkjia@localhost ~]$ ssh-keygen -t dsa -P ''
Generating public/private dsa key pair.
Created directory '/home/bkjia/.ssh'.
Your identification has been saved in /home/bkjia/.ssh/dsa.
Your public key has been saved in /home/bkjia/.ssh/dsa.pub.
The key fingerprint is:
15:57:37:f1:ee:05:26:3a:9e:e1:aa:b3:11:27:b3:1a bkjia@localhost.localdomain
The key's randomart image is:
+--[ DSA 1024]----+
|          . ...oo|
|          o  .o|
|          . . o .|
|        . . o o |
|      + S +    o|
|      * o +  ..|
|    E o  +    .|
|    o.. .      |
|    . o+.        |
+-----------------+
[bkjia@localhost ~]$ cat ~/.ssh/dsa.pub >> ~/.ssh/authorized_keys
[bkjia@localhost ~]$ ssh localhost
The authenticity of host 'localhost (127.0.0.1)' can't be established.
RSA key fingerprint is f3:f8:0b:26:a1:8e:14:55:34:ec:cb:99:f8:70:2c:d3.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'localhost' (RSA) to the list of known hosts.
Last login: Fri Aug  9 08:39:32 2013 from 192.168.142.1
[bkjia@localhost ~]$

好了,配置完成了。

Ubuntu 下配置 SSH服務全過程及問題解決

Ubuntu 12.04下安裝Git,SSH及出現的Permission denied解決辦法

Ubuntu 12.10下OpenSSH的離線安裝方法

Ubuntu下SSH安裝或設定

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.