Linux主機間ssh實現無密碼登陸

來源:互聯網
上載者:User

標籤:ssh   無密碼登陸   

server1    主機名稱:centos6         IP:192.168.2.105        作業系統:centos6.5

server2    主機名稱:rhel6             IP:192.168.2.110        作業系統:rhel6.5


為了實現server1對server2能夠實現無密碼登陸,可以在server1主機上使用ssh-keygen工具產生一對密鑰,server1保留私密金鑰,將公開金鑰上傳至server2主機相應使用者的主目錄下的.ssh/檔案夾下,並重新命名為authorized_keys

步驟如下:

1、在server1上產生金鑰組

[[email protected] .ssh]# ssh-keygen -t rsaGenerating 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:
43:5a:4c:00:e6:93:88:ef:92:87:1b:6b:ed:91:c9:33 [email protected]The key‘s randomart image is:+--[ RSA 2048]----+|    o....        || . + . o         ||. . +   +        || .   . +         ||  .   . S        || +. o    .       ||= +E             || B .+            ||+ ..             |+-----------------+
[[email protected] .ssh]#pwd/root/.ssh[[email protected] .ssh]#lsid_rsa  id_rsa.pub

需要注意的是     Enter passphrase (empty for no passphrase):
                        Enter same passphrase again:

這兩項提示輸入密碼,必須留空,直接按斷行符號鍵跳過,這樣就能避免server1遠程登入server2時所要口令了。

這時候產生了兩個檔案,id_rsa  id_rsa.pub

2、將公開金鑰檔案id_rsa.pub上傳至server2主機上

[[email protected] .ssh]# ssh [email protected] "cat >> /root/.ssh/authorized_keys" < id_rsa.pub[email protected]‘s password:

3、修改server2中sshd的設定檔

[[email protected] .ssh]#vim /etc/ssh/sshd_config

啟用兩項

RSAAuthentication yes

AuthorizedKeysFile .ssh/authorized_keys

在server2上重啟sshd服務

[[email protected] .ssh]/etc/init.d/sshd restart

此時從server1使用ssh登入server2則無需登入密碼,如果還要求輸入密碼或者報錯,則是由於selinux的原因

[[email protected] .ssh]setenforce 0            ###立即生效

強制關閉selinux

或者修改相關設定檔

[[email protected] .ssh]vim /etc/selinux/config

修改:

SELINUX=disabled        ###修改設定檔重啟後才可生效

此時就可以實現server1對server2的無密碼登陸了


註:看見網上說鑒於檔案的安全性應將兩台主機的.ssh/目錄以及目錄下的所有檔案的許可權改為600

chmod -R 600 .ssh/

不過個人認為,由於.ssh/目錄位於使用者的家目錄中,其他普通使用者根本無法cd進來,所以這一步貌似可以略去。

本文出自 “紅樓遺夢” 部落格,請務必保留此出處http://7385845.blog.51cto.com/7375845/1531074

聯繫我們

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