For Multiple remote server
#!/bin/bash# Set the username and the path to the SSH key on the local machine
username=<your_username>
ssh_key_path=<path_to_ssh_key>
# Set the list of remote servers
servers=(
server1.example.com
server2.example.com
server3.example.com
)
# Loop through the servers and copy the SSH key
for server in "${servers[@]}"
do
echo "Copying SSH key to $server..."
ssh-copy-id -i $ssh_key_path $username@$server
done
This script defines a list of remote servers, and then loops through each one, copying the SSH key to the server using the ssh-copy-id
Command. Replace <your_username>
and <path_to_ssh_key>
with your own values.
Make sure to save this script to a file (e.g. copy_ssh_key.sh
) and make it executable by running chmod +x copy_ssh_key.sh
. Then, simply run the script with ./copy_ssh_key.sh
to copy the SSH key to all of the servers in the list.
Another way to copy the ssh key to remote servers.
Create list_of_servers file.
vi list_of_serversusername@servername1
username@servername2
username@servername3
Create a file (e.g. copy_ssh_key.sh
)
#!/bin/bash
for server in `cat /home/list_of_servers`;
do
echo "Copying SSH key to $server..."
ssh-copy-id -i ~/.ssh/id_rsa.pub $serverdone
Make it executable by running chmod +x copy_ssh_key.sh
. Then, simply run the script with ./copy_ssh_key.sh
to copy the SSH key to all of the servers in the list.