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
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目錄權限不正確,請重新執行第四步操作;
八、將 id_rsa、 id_rsa.pub復制到其它應用服務器上:
scp id_rsa hadoop@IP:/home/hadoop/.ssh
遠程復制
scp id_rsa.pub hadoop@IP:/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設置不正確