Install / Append SSH Key In A Remote Linux / UNIX Servers Authorized_keys

To install or append an SSH key to the authorized_keys file on a remote Linux/Unix server, you can use the ssh-copy-id command. Here are the steps to do so:

  1. Open a terminal window on your local machine.
  2. Generate an SSH key pair (if you haven’t already):
    • Use the following command to generate a new SSH key pair:
      ssh-keygen -t rsa -b 4096
    • Follow the prompts to create the key pair. The default location for the key pair is ~/.ssh/id_rsa for the private key and ~/.ssh/id_rsa.pub for the public key.
  3. Copy the public key to the remote server:
    • Use the ssh-copy-id command to copy the public key to the remote server. For example, to copy the key to a user account named “remoteuser” on a server with IP address “192.0.2.1”, use the following command:
      ssh-copy-id remoteuser@192.0.2.1
    • This will prompt you for the password for the remote user account. Enter the password to log in to the remote server and copy the public key to the authorized_keys file for the user.
  4. Test the SSH connection:
    • Try to log in to the remote server using SSH. If the key was installed correctly, you should be able to log in without entering a password.

Alternatively, you can manually append the contents of your local public key to the authorized_keys file on the remote server using a text editor. Here are the steps to do so:

  1. Open a terminal window on your local machine.
  2. Display the contents of your public key:
    • Use the following command to display the contents of your public key:
      cat ~/.ssh/id_rsa.pub
    • This will display the key in the terminal window.
  3. Connect to the remote server using SSH:
    • Use the following command to connect to the remote server using SSH:
      ssh remoteuser@192.0.2.1
    • This will prompt you for the password for the remote user account. Enter the password to log in to the remote server.
  4. Edit the authorized_keys file:
    • Use a text editor (such as nano or vim) to edit the authorized_keys file for the remote user account. For example, to edit the file for the user “remoteuser”, you can use the following command:
      nano ~/.ssh/authorized_keys
    • Append the contents of your public key to the end of the file.
  5. Save and exit the file:
    • Save the changes you made to the file and exit the text editor.
  6. Test the SSH connection:
    • Try to log in to the remote server using SSH. If the key was installed correctly, you should be able to log in without entering a password.

Leave a Comment