Linux ssh配置

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

1、建立在用戶的home目錄下建立  .ssh文件夾apache

mkdir .ssh安全

能夠隱藏文件夾或文件內容服務器

ls -a ssh

2、 生成證書ide

證書分爲:dsa和rsaoop

ssh-keygen -t rsa -P  '' -b 1024測試

ssh-keygen 生成命令spa

-t 表示證書 rsa debug

-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

3、 把公鑰信息寫入 authorized_keys 文檔中

cat  id_rsa.pub  >>  authorized_keys

(將生成的公鑰文件寫入 authorized_keys 文件)

4、設置文件和目錄權限

設置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 

6、重啓sshd 服務 (須要使用root用戶登陸)

service sshd restart

7、本地驗證測試

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
hadoop@localhost'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目錄權限不正確,請從新執行第四步操做;

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

scp   id_rsa  hadoop@IP:/home/hadoop/.ssh  

遠程複製

scp   id_rsa.pub  hadoop@IP:/home/hadoop/.ssh 

遠程複製

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

 

9、將驗證遠程免密碼登陸:

ssh  10.196.20.194(遠程IP)

總結:

一、文件和目錄的權限千萬別設置成chmod 777,這樣權限太大了,存在安全問題;

二、生成的rsa/dsa簽名的公鑰是給對方機器使用的。

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

四、配置出錯狀況:權限或/etc/ssh/sshd_config設置不正確

相關文章
相關標籤/搜索