Linux ssh配置

來源:互聯網
上載者:User

標籤:des   style   blog   class   c   tar   

A機器ssh登入B機器無需輸入密碼;當應用有伺服器很多的時候輸入密碼很浪費時間 在Hadoop安裝時要求免密碼登入;

一、建立在使用者的home目錄下建立  .ssh檔案夾

mkdir .ssh

可以隱藏檔案夾或檔案內容

ls -a 

二、 產生認證

認證分為:dsa和rsa

ssh-keygen -t rsa -P  ‘‘ -b 1024

ssh-keygen 產生命令

-t 表示認證 rsa 

-p 密碼提示 ‘‘

-b 認證大小 為:1024

執行後 將會產生密鑰檔案和私密金鑰檔案

ll

-rwx------ 1 apch apache 883 May 20 15:13 id_rsa
-rwx------ 1 apch apache 224 May 20 15:13 id_rsa.pub

三、 把公開金鑰資訊寫入 authorized_keys 文檔中

cat  id_rsa.pub  >>  authorized_keys

(將產生的公開金鑰檔案寫入 authorized_keys 檔案)

四、設定檔案和目錄許可權

設定authorized_keys許可權
$ chmod 600 authorized_keys 
設定.ssh目錄許可權
$ chmod 700 -R .ssh

五 修改/etc/ssh/sshd_config  (需要使用root使用者登入)

vi  /etc/ssh/sshd_config

Protocol 2 (僅使用SSH2) 
PermitRootLogin yes (允許root使用者使用SSH登陸,根據登入賬戶設定) 

ServerKeyBits 1024 (將serverkey的強度改為1024) 

PasswordAuthentication no (不允許使用密碼方式登陸)

 PermitEmptyPasswords no   (禁止空密碼進行登陸) 

RSAAuthentication yes  (啟用 RSA 認證) 

PubkeyAuthentication yes (啟用公開金鑰認證)

AuthorizedKeysFile   .ssh/authorized_keys 

六、重啟sshd 服務 (需要使用root使用者登入)

service sshd restart

七、本地驗證測試

ssh -v  localhost (開啟登入偵錯模式)

如果出現輸入密碼說明沒有成功

debug1: Authentications that can continue: publickey,password
debug1: Next authentication method: publickey
debug1: Trying private key: /home/hadoop/.ssh/identity
debug1: Offering public key: /home/hadoop/.ssh/id_rsa
debug1: Server accepts key: pkalg ssh-rsa blen 149
debug1: read PEM private key done: type RSA
debug1: Authentications that can continue: publickey,password
debug1: Offering public key: /home/hadoop/.ssh/id_dsa
debug1: Authentications that can continue: publickey,password
debug1: Next authentication method: password
[email protected]‘s password:

錯誤記錄檔查看

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

tail  -50f  /var/log/secure

May 20 16:35:37 JTMCRM195 sshd[7838]: Authentication refused: bad ownership or modes for directory  /home/hadoop
May 20 16:35:37 JTMCRM195 sshd[7838]: Authentication refused: bad ownership or modes for directory  /home/hadoop
May 20 16:36:05 JTMCRM195 sshd[7839]: Connection closed by 127.0.0.1
May 20 16:36:12 JTMCRM195 sshd[7848]: Authentication refused: bad ownership or modes for directory /home/hadoop
May 20 16:36:12 JTMCRM195 sshd[7848]: Authentication refused: bad ownership or modes for directory /home/hadoop

從日誌上應該.ssh目錄許可權不正確,請重新執行第四步操作;

八、將 id_rsa、 id_rsa.pub複製到其它應用伺服器上:

scp   id_rsa  [email protected]:/home/hadoop/.ssh  

遠程複製

scp   id_rsa.pub  [email protected]:/home/hadoop/.ssh 

遠程複製

登入到應用伺服器(IP),再執行第三步到第七步;

 

九、將驗證遠程免密碼登入:

ssh  10.196.20.194(遠程IP)

總結:

1、檔案和目錄的許可權千萬別設定成chmod 777,這樣許可權太大了,存在安全問題;

2、產生的rsa/dsa簽名的公開金鑰是給對方機器使用的。

3、linux之間的訪問直接 ssh 機器ip

4、配置出錯情況:許可權或/etc/ssh/sshd_config設定不正確

 

 

 

聯繫我們

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