How to Generate an SSH Key Pair and Add it to Your Remote Server on Linux

How to Generate an SSH Key Pair and Add it to Your Remote Server on Linux
Photo by ThisisEngineering RAEng / Unsplash

SSH (Secure Shell) keys provide a secure way to authenticate your identity when connecting to remote servers. They consist of a private key (kept secret) and a public key (shared with remote servers). This guide will walk you through generating an SSH key pair and adding it to your remote server for secure access.

Step 1: Generating SSH Key Pair

On Your Local Machine

  1. Open your terminal.
  2. Generate an SSH key pair using the ssh-keygen command:
ssh-keygen -t rsa -b 4096 -C "[email protected]"

Replace [email protected] with your email address. This command generates an RSA key pair (4096-bit) associated with your email.

  1. You'll be prompted to enter a file to save the key. Press Enter to save it in the default location (~/.ssh/id_rsa), or specify a different path and filename if preferred.
  2. Optionally, you can set a passphrase for added security. It's a password used to protect the private key.

  3. Absolutely! Here's a blog post guide on generating an SSH private-public key pair and adding it to a remote server:

How to Generate an SSH Key Pair and Add it to Your Remote Server

SSH (Secure Shell) keys provide a secure way to authenticate your identity when connecting to remote servers. They consist of a private key (kept secret) and a public key (shared with remote servers). This guide will walk you through generating an SSH key pair and adding it to your remote server for secure access.

Step-by-step guide

Step 1: Generating SSH Key Pair

  1. Open your terminal on your local machine.
  2. Generate an SSH key pair using the ssh-keygen command:bashCopy codessh-keygen -t rsa -b 4096 -C "[email protected]"
    Replace "[email protected]" with your email address. This command generates an RSA key pair (4096-bit) associated with your email.
  3. You'll be prompted to enter a file to save the key. Press Enter to save it in the default location (~/.ssh/id_rsa), or specify a different path and filename if preferred.
  4. Optionally, you can set a passphrase for added security. It's a password used to protect the private key.

Step 2: Adding Public Key to Remote Server

  1. On your local machine, display the public key using:
cat ~/.ssh/id_rsa.pub

This command prints your public key to the terminal.

  1. Copy the entire content of the public key.
  2. Gain access to your remote server. Either using password-based SSH such as ssh username@remote_server_ip or by accessing it on premise (with the public key file at hand).
  3. Create an authorized_keys file if it doesn’t exist:
mkdir -p ~/.ssh && touch ~/.ssh/authorized_keys
  1. Open the authorized_keys file in a text editor, such as nano or vim (advanced users)
nano ~/.ssh/authorized_keys

If the file exists, you can append the content to the end of the file.

  1. Paste the copied public key content into this file.
  2. Set appropriate permissions for .ssh directory and authorized_keys file:
chmod 700 ~/.ssh && chmod 600 ~/.ssh/authorized_keys
  1. Optionally (but highly recommended), disable password authentication to enforce key-based authentication (after confirming the key-based login works):
sudo nano /etc/ssh/sshd_config
    1. Find the line PasswordAuthentication and set it to no.
    2. Save the file and restart SSH: sudo systemctl restart sshd

Step 3: Testing the Connection

  1. Open a new terminal window on your local machine.
  2. Attempt to connect to the remote server using SSH:
ssh username@remote_server_ip

You should be logged in without being prompted for a password, confirming successful key-based authentication.

⚠️
It's recommended to come back to step 8 after ensuring you can remote to your machine using the private-key pair.

Conclusion

Generating and adding an SSH key pair to your remote server enhances security by providing a secure method of authentication. Key-based authentication eliminates the need for passwords, offering a more secure and convenient way to access your server remotely.

With these steps, you can confidently generate an SSH key pair and securely add it to your remote server for seamless and secure access.

😍
If you have access to multiple remove servers and want to make your life easier, consider this post for an easy way to manage your SSH keys.