我正在尝试将 .ssh/id_rsa.pub 从我们的中央服务器复制到多个服务器。我有以下脚本,我通常使用它来将更改推送到不同的服务器。
#!/bin/bash
for ip in $(<IPs); do
# Tell the remote server to start bash, but since its
# standard input is not a TTY it will start bash in
# noninteractive mode.
ssh -q "$ip" bash <<-'EOF'
EOF
done
Run Code Online (Sandbox Code Playgroud)
但在这种情况下,我需要在本地服务器上 cat 公钥,然后将其添加到多个服务器。有没有办法通过使用上面的 here 文档脚本来执行以下操作。
cat .ssh/id_rsa.pub |ssh tony@0.0.0.0 'cat > .ssh/authorized_keys'
Run Code Online (Sandbox Code Playgroud)
kle*_*erk 23
通过这个简单的循环,您可以将其自动化并传播到所有远程服务器。
#!/bin/bash
for ip in `cat /home/list_of_servers`; do
ssh-copy-id -i ~/.ssh/id_rsa.pub $ip
done
Run Code Online (Sandbox Code Playgroud)
小智 8
这是我的简单脚本,用于将 ssh-keygen 复制到多个服务器,而无需每次都询问密码。
for server in `cat server.txt`;
do
sshpass -p "password" ssh-copy-id -i ~/.ssh/id_rsa.pub user@$server
done
Run Code Online (Sandbox Code Playgroud)
这需要sshpass,它可能需要通过包或从源代码单独安装。
小智 5
如果需要将其他人的公钥复制到多台机器,则接受的答案将不起作用。所以,我想出了以下解决方案:
\ncat add-vassal-tc-agents.sh
#!/bin/bash\nset -x # enable bash debug mode\nif [ -s vassal-public-key.pub ]; then # if file exists and not empty\n for ip in `cat tc-agents-list.txt`; do # for each line from the file\n # add EOL to the end of the file (i.e., after the last line)\n # and echo it into ssh, where it is added to the authorized_keys\n sed -e '$s/$/\\n/' -s vassal-public-key.pub | ssh "$ip" 'cat >> ~/.ssh/authorized_keys'\n done\nelse\n echo "Put new vassal public key into ./vassal-public-key.pub to add it to tc-agents-list.txt hosts"\nfi\nRun Code Online (Sandbox Code Playgroud)\n此脚本将新密钥添加到计算机列表上的用户,前提是它运行的环境具有访问权限。
\n示例tc-agents-list.txt:
root@10.10.0.1\nroot@10.10.0.2\nroot@10.10.0.3\nroot@10.10.0.4\nRun Code Online (Sandbox Code Playgroud)\n注意:这需要使用 GNU sed。\xc2\xa0\n由于问题说“Linux”,因此可能存在 GNU\xc2\xa0sed。
\n