Copy the ssh key into remote servers (2024)

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_servers

username@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 $server

done

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.

Copy the ssh key into remote servers (2024)
Top Articles
Latest Posts
Article information

Author: Zonia Mosciski DO

Last Updated:

Views: 6784

Rating: 4 / 5 (71 voted)

Reviews: 86% of readers found this page helpful

Author information

Name: Zonia Mosciski DO

Birthday: 1996-05-16

Address: Suite 228 919 Deana Ford, Lake Meridithberg, NE 60017-4257

Phone: +2613987384138

Job: Chief Retail Officer

Hobby: Tai chi, Dowsing, Poi, Letterboxing, Watching movies, Video gaming, Singing

Introduction: My name is Zonia Mosciski DO, I am a enchanting, joyous, lovely, successful, hilarious, tender, outstanding person who loves writing and wants to share my knowledge and understanding with you.