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)

FAQs

How do I copy my SSH key to a remote server? ›

Copying Public Keys to Remote Servers
  1. Use the ssh-copy-id command to append the public key in the local ~/.ssh/id_rsa.pub file to the ~/.ssh/authorized_keys file on the remote system, for example: ssh-copy-id remote_user@host.
  2. When prompted, enter the password for the remote system.

How do I add a SSH key to a remote server? ›

Method 2: Manually copy the public ssh key to the server
  1. Step 1: Get the public key. Ask the end user to provide the public key by typing the following command: cat ~/.ssh/id_rsa.pub. ...
  2. Step 2: Create ssh directory in the user's home directory (as a sysadmin) ...
  3. Step 3: Set appropriate permission to the file.
Nov 13, 2023

Can I just copy SSH keys to another machine? ›

Yes, if you change the machine from which you access your servers, you can just copy the ~/. ssh directory from the old machine to the new one. It is unnecessary (and cumbersome) to generate a new key pair for every server. You just need the private keys on your new machine.

What does the ssh-copy-ID command copies to the remote host? ›

ssh-copy-id is a useful tool for SSH connections to a remote host without using a password. Basically, it copies the SSH key into the remote host's authorized_keys file, which is by default in the $HOME/. ssh directory.

How do I SSH into a remote server? ›

In the PuTTY configuration window, do the following:
  1. Go to Connection -> SSH -> Tunnels.
  2. Type in the source port number in the Source port field.
  3. Type the VNC server address and port in the Destination field.
  4. Start the SSH session as you normally would.
  5. Connect to your server with a VNC client of your choice.
Nov 23, 2023

How do you copy and paste remote SSH? ›

To copy FROM the micro editor running on your remote machine over ssh: WORKS (you MUST use Shift + select, AND Ctrl + Shift + C): Hold down Shift + mouse select in the remote micro, then Ctrl + Shift + C; then on the local PC to paste: Ctrl + V or Ctrl + Shift + V or middle-mouse-click.

Can you share SSH keys between machines? ›

You can generate an SSH key pair on your boot node and share that key with the other cluster nodes. To share the key with the cluster nodes, you must have the access to an account with root access for each node in your cluster.

How do I save my SSH key to another computer? ›

Here is the process using an External Drive or USB thumb drive:
  1. Copy your private key (typically ~/. ...
  2. Copy your public key (typically ~/. ...
  3. Carry the USB stick to your new workstation.
  4. Make the ~/. ...
  5. Change the permissions of the ~/. ...
  6. Copy your private key from the USB stick to your ~/.

How to clone via SSH key? ›

To perform a GitHub clone with SSH keys in Git, simply follow these steps:
  1. Create an SSH keypair on your Windows or Linux OS.
  2. Copy the value of the public SSH key to your GitHub account.
  3. Obtain the GitHub SSH URL for the repository to be cloned.
  4. Using Git, clone from GitHub with the SSH URL.
Jan 11, 2022

Which command is used to copy an SSH key? ›

Copy the key to a server

Once an SSH key has been created, the ssh-copy-id command can be used to install it as an authorized key on the server. Once the key has been authorized for SSH, it grants access to the server without a password.

How to copy with ssh command? ›

You can do so using an SSH connection. The commands which you would need to use are mv (short from move) and cp (short from copy). By executing the above command you will move (rename) the file original_file to new_name.

In what file on a remote system would you copy an ssh public key? ›

Before you begin

Conceivably, you can share the public key with anyone without compromising the private key; you store it on the remote system in a .ssh/authorized_keys directory.

How do I export my SSH key? ›

To export your private SSH key:
  1. Select the OpenSSH keys keyring from the left side panel.
  2. Select the Personal SSH key, that has to be exported, from the list.
  3. Right click on the selected key and select Properties ▸ Details.
  4. Press the Export Private Key button to save the exported private key on your computer.

How do I copy to a remote server? ›

Copy a Local File to a Remote System with the scp Command

Where file.txt is the name of the file we want to copy, remote_username is the user on the remote server, 10.10.0.2 is the server IP address. The /remote/directory is the path to the directory you want to copy the file to.

How do I import SSH key to another computer? ›

Answer
  1. Copy your private key (typically ~/. ...
  2. Copy your public key (typically ~/. ...
  3. Carry the USB stick to your new workstation.
  4. Make the ~/. ...
  5. Change the permissions of the ~/. ...
  6. Copy your private key from the USB stick to your ~/. ...
  7. Change the permissions of the private key to be either 600 or 400.

How to SSH into a server using key? ›

The SSH public key authentication has four steps:
  1. Generate a private and public key, known as the key pair. ...
  2. Add the corresponding public key to the server.
  3. The server stores and marks the public key as approved.
  4. The server allows access to anyone who proves the ownership of the corresponding private key.
Aug 10, 2021

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.