Skip to content

Instantly share code, notes, and snippets.

@LoPan455
Last active December 16, 2022 14:51
Show Gist options
  • Select an option

  • Save LoPan455/b5dd32cb59f8f862f1d55e5e602f641c to your computer and use it in GitHub Desktop.

Select an option

Save LoPan455/b5dd32cb59f8f862f1d55e5e602f641c to your computer and use it in GitHub Desktop.
Copy A Different User's Public SSH Key to a Remote Server

If a remote server has password access disabled and relies on authorized SSH keys to manage user access, things can get tricky.

The scenario this gist will solve is:

  1. Server A (10.0.1.20) has a user account, ubuntu, that we need to be able to use to login to it from a variety of hosts.
  2. User A has access to a Server A (10.0.1.20) via SSH. Therfore User A's public key is present in Server A's 'authorized_keys' file.
  3. User A's private key is present on a Bastion host that allows password logins.
  4. User B needs to access Server A, and therefore needs her public key copied to Server A. Server A doesn't allow password logins, so we're stuck.

The solution is the following:

  1. Copy the contents of User B's id_rsa.pub Public Key to a file on host to which User A has access AND contains the private key of a user than can access Server A via SSH. In this example, we acheive the copy by using ssh-copy-id, but you could use really any means.

    ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected] (promted for tjohander's password on 10.0.1.15, which is the bastion server in this scenario)

  2. Now login to the remote server. User B's public key will be present in ~/.ssh/authorized_keys on the Bastion

  3. Copy the public key of User B to a new Key file.

    cat ~/.ssh/authorized_keys > ~/.ssh/thomas_shoebox.rsa.pub

  4. Add User B's public key to the desination host (Server A), using the identity of User A

ssh-copy-id -f -i ~/.ssh/thomas_shoebox.rsa.pub -o 'IdentityFile ~/.ssh/id_rsa' [email protected]

  1. Repeat as necessary on any destination host.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment