[英]ssh key generation and copy ssh key to other machines
我正在運行以下腳本來生成 ssh 密鑰並將其復制到集群中的其他計算機,以實現無密碼 ssh。
#!/bin/bash
#create host and password file
echo enter host-name,password in "host pass" format:
read hopa
echo "$hopa"> /root/host1.txt
sed -i 's/,/\n/g' /root/host1.txt
# SSH Key
#echo -en "y" | ssh-keygen -f id_rsa -t rsa -N ''
# Passwordless ssh
echo -en "y" | ssh-keygen -f id_rsa -t rsa -N ''
declare -A hp
while IFS=' ' read -r host pass
do
hp["$host"]="$pass"
done < /root/host1.txt
for host in "${!hp[@]}"
do
pass="${hp["$host"]}"
sshpass -p "${pass[i]}" ssh-copy-id -i id_rsa -o "StrictHostKeyChecking no" -f root@"${host[i]}" -p 22
done
當提示輸入主機名及其密碼時:我的輸入:
cephadmin 1234,ceph2 1234,ceph3 1234,cephclient 1234
Output:
The key fingerprint is:
SHA256://CzhoYLtmVVRWoJTKfTkJV9BQbeKypzGoXBLV62KKw root@cephadmin
The key's randomart image is:
+---[RSA 3072]----+
| o+o+==.o|
| . .oB.*. .|
| + * B .. |
| . . B = . |
| o S + . . |
| . . + . . |
| E o *.=. |
| . =.*o+o |
| . oo .+o |
+----[SHA256]-----+
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "id_rsa.pub"
Number of key(s) added: 1
Now try logging into the machine, with: "ssh -o 'StrictHostKeyChecking no' -p '22' 'root@cephadmin'"
and check to make sure that only the key(s) you wanted were added.
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "id_rsa.pub"
Number of key(s) added: 1
Now try logging into the machine, with: "ssh -o 'StrictHostKeyChecking no' -p '22' 'root@ceph2'"
and check to make sure that only the key(s) you wanted were added.
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "id_rsa.pub"
Number of key(s) added: 1
Now try logging into the machine, with: "ssh -o 'StrictHostKeyChecking no' -p '22' 'root@ceph3'"
and check to make sure that only the key(s) you wanted were added.
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "id_rsa.pub"
Number of key(s) added: 1
Now try logging into the machine, with: "ssh -o 'StrictHostKeyChecking no' -p '22' 'root@cephclient'"
and check to make sure that only the key(s) you wanted were added.
當我嘗試使用 ssh cephclient 登錄 cephclient 機器時,系統提示我輸入密碼
[root@localhost ~]# ssh cephclient
root@cephclient's password:
如何確保我的腳本正常運行並獲得所需的 output,即無需密碼即可登錄其他機器?
第一次它會詢問密碼。 從第二次開始,它不會詢問密碼。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.