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配置—Linux下實現免密碼登入Linux密碼
- ssh-copy-id三步實現SSH免密登入
- ssh免密登入
- SSH 遠端登入「記住密碼」密碼
- ssh免密登入原理
- 自動批次實現linux機器ssh免密shell指令碼Linux指令碼
- ssh直接帶密碼登入Linux,Linux自動密碼登陸利器sshpass密碼Linux
- ssh免密碼登入失敗解決密碼
- Linux 配置 SSH 秘鑰免密碼登入Linux密碼
- 批量實現多臺伺服器之間ssh無密碼登入的相互信任關係伺服器密碼
- Hadoop起步之圖解SSH、免密登入原理和實現Hadoop圖解
- ssh進階,免密登入
- SSH免密登入詳解
- Multipass虛擬機器ssh登入(密碼方式)虛擬機密碼
- AWS EC2 Linux | ssh 使用密碼登入Linux密碼
- 配置SSH免密碼登陸密碼
- 在CentOS/RHEL上設定SSH免密碼登入CentOS密碼
- Linux配置SSH免密碼登入(非root賬號)Linux密碼
- ssh免密登入簡單操作
- linux 設定ssh 免密登入Linux
- CentOS監控ssh免密登入CentOS
- hadoop-SSH免密登入配置Hadoop
- flask框架如何實現修改密碼和免密登入功能Flask框架密碼
- Windows配置ssh免密登入linux+VSCode Remote-SSH免密編寫linux程式碼WindowsLinuxVSCodeREM
- centos7 設定ssh免密碼登入配置方法CentOS密碼
- Ubuntu ssh伺服器拒絕密碼(使用root登入)Ubuntu伺服器密碼
- 密碼登入密碼
- SharedPreferences實現記住密碼----自動登入功能密碼
- Linux - 配置SSH免密登入 - “ssh-keygen”的基本用法Linux
- ssh-copy-id登陸無需重複輸入密碼密碼
- 介紹Linux小技巧,如何實現免密碼登入Linux密碼
- SSH免密登陸
- SSH實踐生成密碼密碼
- GitHub不再支援密碼驗證解決方案:SSH免密與Token登入配置Github密碼
- SSH安全登陸原理:密碼登陸與公鑰登陸密碼
- 1-Linux叢集搭建,分發指令碼,ssh免密登入Linux指令碼
- SpringBoot 整合 Shiro 密碼登入與郵件驗證碼登入(多 Realm 認證)Spring Boot密碼
- SSH免密登入與SCP遠端複製