我配置了一组虚拟机,并在此过程中选择创建跳转服务器。现在我在跳转服务器上尝试通过 SSH 连接到其他机器,但我一直收到一条错误消息,提示“权限被拒绝(公钥)”
这些机器运行的是 Ubuntu,它位于 Microsoft Azure 上。
在创建过程中,我确实指定了一个公钥,该公钥是我从 ppk 文件中获得的,并加载到 puttygen 中以复制它。我假设其他虚拟机也有这个。不确定如何进入它们。
答案1
Azure文档提供了一个github 链接它提供了在节点之间设置无密码 ssh 的有用代码。如果您的虚拟机尚未提供该选项,您可能需要重新创建启用密码 ssh 的虚拟机。
他们的示例是针对 CentOS 的,但我认为可以轻松修改以在 ubuntu 上运行。
#!/bin/bash
#for centos user must first install epel-release, sshpass, and nmap (sshpass and nmap are available from epel-release for CENTOS)
#usage ./authMe2.sh [username] [password] [internalIP prefix]
# ./authMe2.sh azureuser Azure@123 10.2.1
USER=$1
PASS=$2
IPPRE=$3
HEADNODE=`hostname`
mkdir -p .ssh
echo -e 'y\n' | ssh-keygen -f .ssh/id_rsa -t rsa -N ''
echo 'Host *' >> .ssh/config
echo 'StrictHostKeyChecking no' >> .ssh/config
chmod 400 .ssh/config
chown azureuser:azureuser /home/azureuser/.ssh/config
nmap -sn $IPPRE.* | grep $IPPRE. | awk '{print $5}' > nodeips.txt
for NAME in `cat nodeips.txt`; do sshpass -p $PASS ssh -o ConnectTimeout=2 $USER@$NAME 'hostname' >> nodenames.txt;done
NAMES=`cat nodenames.txt` #names from names.txt file
for NAME in $NAMES; do
sshpass -p $PASS scp -o "StrictHostKeyChecking no" -o ConnectTimeout=2 /home/$USER/nodenames.txt $USER@$NAME:/home/$USER/
sshpass -p $PASS ssh -o ConnectTimeout=2 $USER@$NAME "mkdir .ssh && chmod 700 .ssh"
sshpass -p $PASS ssh -o ConnectTimeout=2 $USER@$NAME "echo -e 'y\n' | ssh-keygen -f .ssh/id_rsa -t rsa -N ''"
sshpass -p $PASS ssh -o ConnectTimeout=2 $USER@$NAME 'touch /home/'$USER'/.ssh/config'
sshpass -p $PASS ssh -o ConnectTimeout=2 $USER@$NAME 'echo "Host *" > /home/'$USER'/.ssh/config'
sshpass -p $PASS ssh -o ConnectTimeout=2 $USER@$NAME 'echo StrictHostKeyChecking no >> /home/'$USER'/.ssh/config'
sshpass -p $PASS ssh -o ConnectTimeout=2 $USER@$NAME 'chmod 400 /home/'$USER'/.ssh/config'
cat .ssh/id_rsa.pub | sshpass -p $PASS ssh -o ConnectTimeout=2 $USER@$NAME 'cat >> .ssh/authorized_keys'
sshpass -p $PASS scp -o "StrictHostKeyChecking no" -o ConnectTimeout=2 $USER@$NAME:/home/$USER/.ssh/id_rsa.pub .ssh/sub_node.pub
for SUBNODE in `cat nodeips.txt`; do
sshpass -p $PASS ssh -o ConnectTimeout=2 $USER@$SUBNODE 'mkdir -p .ssh'
cat .ssh/sub_node.pub | sshpass -p $PASS ssh -o ConnectTimeout=2 $USER@$SUBNODE 'cat >> .ssh/authorized_keys'
done
sshpass -p $PASS ssh -o ConnectTimeout=2 $USER@$NAME 'chmod 700 .ssh/'
sshpass -p $PASS ssh -o ConnectTimeout=2 $USER@$NAME 'chmod 640 .ssh/authorized_keys'
done