add ssh key to server ubuntu

admin3 April 2024Last Update :

Mastering SSH: A Comprehensive Guide to Adding SSH Keys on Ubuntu Servers

add ssh key to server ubuntu

Welcome to the world of secure server management! In this extensive guide, we will delve into the intricacies of Secure Shell (SSH) and how it becomes an indispensable tool for administrators and developers alike. By the end of this article, you’ll be equipped with the knowledge to add SSH keys to your Ubuntu server, enhancing security and streamlining your workflow.

Understanding SSH and Its Importance

Before we jump into the practical steps, let’s understand what SSH is and why it’s crucial for server security. SSH, or Secure Shell, is a protocol that provides a secure channel over an unsecured network in a client-server architecture, allowing for encrypted communication between the two.

Why Use SSH?

  • Encryption: All data transferred via SSH is encrypted, safeguarding against eavesdropping and connection hijacking.
  • Authentication: SSH supports various forms of authentication, including password-based and key-based, the latter being more secure and recommended.
  • Integrity: SSH ensures that the data sent and received has not been tampered with, maintaining its integrity throughout transmission.

Setting Up SSH Key Authentication on Ubuntu

Now, let’s get hands-on and set up SSH key authentication on your Ubuntu server. This method replaces the less secure password login with a pair of cryptographic keys: a private key that remains with the user, and a public key that you place on the server.

Generating Your SSH Key Pair

The first step is to generate your SSH key pair on your local machine. Open your terminal and use the following command:

ssh-keygen -t rsa -b 4096

This command creates a new RSA key pair with a length of 4096 bits, offering robust security. You’ll be prompted to enter a file path to save the keys and an optional passphrase for added security.

Copying the Public Key to Your Ubuntu Server

With your key pair ready, the next step is to copy the public key to your Ubuntu server. The simplest way to do this is using the ssh-copy-id utility:

ssh-copy-id username@your_server_ip

Replace “username” with your actual username and “your_server_ip” with your server’s IP address. If you’ve set a passphrase, you’ll be asked to enter it.

Manual Method: Adding the Public Key Manually

If for some reason ssh-copy-id isn’t available, you can manually add the public key to your server by following these steps:

  1. Log into your server with your username and password.
  2. Create a directory named .ssh in your home folder if it doesn’t already exist:
mkdir -p ~/.ssh
  1. Create or edit the authorized_keys file within this directory:
nano ~/.ssh/authorized_keys
  1. Copy your public key content from your local machine and paste it into this file.
  2. Save and close the file, ensuring the correct permissions are set:
chmod 600 ~/.ssh/authorized_keys

Securing Your SSH Configuration

After adding your SSH key, it’s essential to tweak your SSH configuration to enhance security further.

Disabling Password Authentication

To prevent unauthorized access through password guessing, disable password authentication on your server:

sudo nano /etc/ssh/sshd_config

Find the line that reads PasswordAuthentication and change it to no:

PasswordAuthentication no

Restart the SSH service to apply changes:

sudo systemctl restart sshd

Changing the Default SSH Port

Changing the default SSH port (22) to a non-standard port can reduce the risk of automated attacks:

sudo nano /etc/ssh/sshd_config

Locate the line with #Port 22, remove the #, and change the number to a port of your choice:

Port 2222

Remember to update any firewall settings to allow traffic on the new port before restarting the SSH service.

FAQ Section

What is the difference between RSA and ECDSA keys?

RSA keys are traditional and widely supported, while ECDSA keys are newer and provide equivalent security with shorter key lengths. Both are suitable for SSH.

Can I use the same SSH key pair for multiple servers?

Yes, you can use the same SSH key pair to access multiple servers for convenience, but for higher security environments, it’s recommended to use unique key pairs.

How do I troubleshoot issues with SSH key authentication?

Ensure the public key is correctly placed in the authorized_keys file, permissions are set properly, and the SSH service is configured to accept key authentication.


Adding an SSH key to your Ubuntu server is a straightforward process that significantly enhances security. By following the steps outlined in this guide, you can protect your server from unauthorized access and manage it efficiently. Remember to keep your private key safe and regularly review your server’s security configurations to maintain a robust defense against potential threats.


Leave a Comment

Your email address will not be published. Required fields are marked *

Comments Rules :

Breaking News