shell實現多節點ssh密碼登入
下面以:RHEL5 OS為例介紹使用方法
一、安裝sshpass命令工具
1.下載sshpass-1.05-1.el5.rf.x86_64.rpm軟體包:
()可以訪問此網址下載
2.安裝sshpass
#]rpm –ivh sshpass-1.05-1.el5.rf.x86_64.rpm #安裝完成後系統中才有sshpass命令
二、shell使用指令碼說明
#指令碼內容
~]#cat uway-auth.sh
#!/bin/bash
USER_NAME=$1
USER_HOME=/home/$1
PASSWD=$2
Proot=$3
filename=$4
#echo $USER_NAME
#echo $USER_HOME
#在本機的$USER_NAME下生成公鑰、私鑰
if [ -d $USER_HOME/.ssh ]; then
rm -rf $USER_HOME/.ssh
fi
sudo -u $USER_NAME ssh-keygen -t rsa
sudo -u $USER_NAME cat $USER_HOME/.ssh/id_rsa.pub>$USER_HOME/.ssh/authorized_keys
cp $USER_HOME/.ssh/authorized_keys /root/.ssh/
# 在除本機外的各節點生成公鑰、私鑰
for i in $(cat $filename)
do
#echo $i
sshpass -p $PASSWD ssh $USER_NAME@$i -o StrictHostKeyChecking=no <
#建立~/.ssh目錄
if [ -d ~/.ssh ]; then
rm -rf ~/.ssh
fi
#生成公鑰、私鑰
ssh-keygen -t rsa
exit
EOF
#將各個節點上的公鑰追加到本機的
sshpass -p $PASSWD ssh $USER_NAME@$i cat $USER_HOME/.ssh/id_rsa.pub >> $USER_HOME/.ssh/authorized_keys
chown -R $USER_NAME:$USER_NAME $USER_HOME/.ssh
#chmod 600 $USER_HOME/.ssh/authorized_keys
done
#scp authorized_keys 檔案到各臺機器上面。
for i in $(cat $filename)
do
sshpass -p $Proot ssh root@$i <
if [ ! -d /root/.ssh ]; then
mkdir /root/.ssh
fi
exit
EOF
sshpass -p $PASSWD scp $USER_HOME/.ssh/authorized_keys $USER_NAME@$i:$USER_HOME/.ssh/
sshpass -p $Proot scp $USER_HOME/.ssh/authorized_keys root@$i:/root/.ssh/
done
#使用說明
1.上傳指令碼到linux主機上,並且賦給x許可權
chmod +x uway-auth.sh
2.指令碼執行需要的傳給的引數:
USER_NAME=$1 #使用者(在主機的哪個使用者下建立ssh互信)
PASSWD=$2 #密碼(上面的使用者的密碼)
Proot=$3 #root的密碼(需要在USER_NAME無密碼登入其他節點的root使用者)
filename=$4 #IP地址列表檔案(除本機外,所有需要ssh互信的節點IP)
三、實驗:使用uway-auth.sh配置ssh免密碼登入
1.準備工作:
(其中某個結點準備即可)
a.安裝sshpass工具
b.上傳uway-auth.sh指令碼到主機上
為了說明實驗效果,以下是執行指令碼之前的記錄:
#此時,ssh到其他主機的時候都需要密碼
~]# su - test
[test@hadoop1 ~]$ cat /etc/hosts
# Do not remove the following line, or various programs
# that require network functionality will fail.
127.0.0.1 localhost.localdomain localhost
::1 localhost6.localdomain6 localhost6
192.168.8.46 hadoop1
192.168.8.48 hadoop2
192.168.8.49 hadoop3
192.168.8.50 hadoop4 實驗中配置這4結點之間ssh免密碼登入
[test@hadoop1 ~]$ ssh hadoop1 date
The authenticity of host 'hadoop1 (192.168.8.46)' can't be established.
RSA key fingerprint is 41:25:aa:22:c1:fb:c1:52:3c:42:db:8d:55:1b:35:79.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'hadoop1,192.168.8.46' (RSA) to the list of known hosts.
test@hadoop1's password:
Permission denied, please try again.
test@hadoop1's password: 需要輸入密碼
[test@hadoop1 ~]$ ssh hadoop2 date
The authenticity of host 'hadoop2 (192.168.8.48)' can't be established.
RSA key fingerprint is 41:25:aa:22:c1:fb:c1:52:3c:42:db:8d:55:1b:35:79.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'hadoop2,192.168.8.48' (RSA) to the list of known hosts.
test@hadoop2's password:
Permission denied, please try again.
test@hadoop2's password: 需要輸入密碼
[test@hadoop1 ~]$ ssh hadoop3 date
The authenticity of host 'hadoop3 (192.168.8.49)' can't be established.
RSA key fingerprint is 41:25:aa:22:c1:fb:c1:52:3c:42:db:8d:55:1b:35:79.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'hadoop3,192.168.8.49' (RSA) to the list of known hosts.
test@hadoop3's password:
Permission denied, please try again.
test@hadoop3's password: 需要輸入密碼
[test@hadoop1 ~]$ ssh hadoop4 date
The authenticity of host 'hadoop4 (192.168.8.50)' can't be established.
RSA key fingerprint is 41:25:aa:22:c1:fb:c1:52:3c:42:db:8d:55:1b:35:79.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'hadoop4,192.168.8.50' (RSA) to the list of known hosts.
test@hadoop4's password: 需要輸入密碼
[test@hadoop1 ~]$ ssh root@hadoop1 date
root@hadoop1's password: 需要輸入密碼
[test@hadoop1 ~]$ ssh root@hadoop2 date
root@hadoop2's password: 需要輸入密碼
[test@hadoop1 ~]$ ssh root@hadoop3 date
root@hadoop3's password: 需要輸入密碼
[test@hadoop1 ~]$ ssh root@hadoop4 date
root@hadoop4's password: 需要輸入密碼
c.配置IP列表
]# cat ip.txt #將IP地址寫到ip.txt檔案中(除本機外)
192.168.8.48
192.168.8.49
192.168.8.50
2.執行指令碼
]# ./uway-auth.sh test test password ip.txt #引數說明請見第二節的第二點
Generating public/private rsa key pair.
Enter file in which to save the key (/home/test/.ssh/id_rsa): 回車
Created directory '/home/test/.ssh'.
Enter passphrase (empty for no passphrase): 回車
Enter same passphrase again: 回車
Your identification has been saved in /home/test/.ssh/id_rsa.
Your public key has been saved in /home/test/.ssh/id_rsa.pub.
The key fingerprint is:
87:72:d1:cf:9a:9e:66:af:88:ba:97:fe:3a:77:72:64 test@hadoop1
3.檢視結點上的authorized_keys檔案內容是否包含了其他結點
4.測試ssh免密碼登入
如上圖所示,主機hadoop1使用test使用者ssh其他節點的test使用者,使用test使用者ssh其他節點的root使用者都不需要密碼了,證明成功
原文連結:http://blog.itpub.net/27000195/viewspace-1428246/
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/27000195/viewspace-1428246/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- shell+expect建立多個節點無密碼ssh密碼
- 如何實現 SSH 無密碼登入密碼
- SSH 實現免密登入
- SSH配置—Linux下實現免密碼登入Linux密碼
- SSH免密碼登入密碼
- ssh-copy-id三步實現SSH免密登入
- Linux SSH無密碼登入Linux密碼
- Mac 下 SSH 免密碼登入Mac密碼
- ssh公鑰免密碼登入密碼
- SSH認證免密碼登入密碼
- [Linux] SSH配置了免密碼登入,登入時還要輸入密碼Linux密碼
- SSH 遠端登入「記住密碼」密碼
- [RHEL7] ssh免密碼登入密碼
- 配置雙機ssh免密碼登入密碼
- SSH無需密碼金鑰登入密碼
- centos ssh免密碼秘鑰登入CentOS密碼
- centos配置ssh免密碼登入後,仍提示輸入密碼CentOS密碼
- ssh免密登入原理
- shell禁止非法IP ssh登入
- ssh免密碼登入失敗解決密碼
- linux ssh公鑰免密碼登入Linux密碼
- [轉] ssh免密碼登入伺服器密碼伺服器
- SSH無密碼登入到指定機器密碼
- ssh無密碼登入認證失敗密碼
- 批量實現多臺伺服器之間ssh無密碼登入的相互信任關係伺服器密碼
- ssh免密碼登陸密碼
- SSH免密登入詳解
- ssh進階,免密登入
- [SSH服務]——一個SSH無密碼登陸實驗密碼
- Linux 配置 SSH 秘鑰免密碼登入Linux密碼
- Multipass虛擬機器ssh登入(密碼方式)虛擬機密碼
- 使用ssh-keygen,實現免密碼登陸linux密碼Linux
- 自動批次實現linux機器ssh免密shell指令碼Linux指令碼
- ssh直接帶密碼登入Linux,Linux自動密碼登陸利器sshpass密碼Linux
- Hadoop起步之圖解SSH、免密登入原理和實現Hadoop圖解
- 配置SSH免密碼登陸密碼
- RHCE_LAB(2)SSH遠端登入自動驗證(不輸入使用者登入密碼)的實現薦密碼
- ssh免密登入簡單操作