Skip to content

Instantly share code, notes, and snippets.

@otkrsk
Last active November 13, 2023 08:40
Show Gist options
  • Star 40 You must be signed in to star a gist
  • Fork 17 You must be signed in to fork a gist
  • Save otkrsk/b0ffd4018e8a79b9010c461af298471e to your computer and use it in GitHub Desktop.
Save otkrsk/b0ffd4018e8a79b9010c461af298471e to your computer and use it in GitHub Desktop.
Add multiple SSH keys to the authorized_keys file to enable SSH authentication when connecting to a server.

Step 1: Generate first ssh key Type the following command to generate your first public and private key on a local workstation. Next provide the required input or accept the defaults. Please do not change the filename and directory location.

workstation 1 $ ssh-keygen -t rsa

Finally, copy your public key to your remote server using scp

workstation 1 $ scp ~/.ssh/id_rsa.pub user@remote.server.com:.ssh/authorized_keys

Step 2: Generate next/multiple ssh key

  1. Login to 2nd workstation
  2. Download original the authorized_keys file from remote server using scp:
workstation 2 $ scp user@remote.server.com:.ssh/authorized_keys ~/.ssh
  1. Now create the new pub/private key:
workstation 2 $ ssh-keygen -t rsa
  1. Now you have new public key. APPEND this key to the downloaded authorized_keys file using cat command:
workstation 2 $ cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
  1. Finally upload authorized_keys to remote server again:
workstation 2 $ scp ~/.ssh/authorized_keys user@remote.server.com:.ssh/

You can repeat step 2 for each user or workstations for remote server.

Step 3: Test your setup Now try to login from Workstation 1, 2 and so on to remote server. You should not be asked for a password:

workstation 1 $ ssh user@remote.server.com
workstation 2 $ ssh user@remote.server.com
@vadorvatsal
Copy link

Thanks for sharing. Clear and precise answer.

@vadorvatsal
Copy link

How does scp work without ssh key(public key) added to .ssh/authorized_keys ? You are doing scp before the key got apppended to the server

@madhusudan12 visit this link to understand how scp works https://linuxize.com/post/how-to-use-scp-command-to-securely-transfer-files/

@manyget
Copy link

manyget commented Sep 16, 2021

Public keys on my Ubuntu have 644 permissions, and authoerized_keys 600 (without my having done anything to change them). Does scp in Step 1 result in authorized_keys having 644? Is that OK? I guess OK could mean two things. Will the machine not object? And OK from a security perspective. (I don't know anything. Just asking.)

@omarsaifuddin
Copy link

Amazing! Thank you so much :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment