How to Set Up SSH Keys on Ubuntu?

How to Set Up SSH Keys on Ubuntu?

Setting up SSH keys on Ubuntu 20.04 involves a few steps. SSH keys allow for secure authentication between two parties.
Here’s a step-by-step guide:

  1. Generate SSH Key Pair:Open a terminal on your Ubuntu 20.04 system and run the following command to generate a new SSH key pair:
    ssh-keygen -t rsa -b 4096 -C "[email protected]"

    Replace "[email protected]" with your email address. This command will prompt you to choose a location to save the keys and to optionally enter a passphrase for added security.

  2. Copy the Public Key to the Server:After generating the SSH key pair, you’ll need to copy the public key to the server you want to connect to.
    You can do this with the following command, replacing username and server_ip with your server’s username and IP address:

    ssh-copy-id username@server_ip

    You’ll need to enter your server password for this step.

    If ssh-copy-id is not available on your system, you can manually copy the public key:

    cat ~/.ssh/id_rsa.pub | ssh username@server_ip 'cat >> ~/.ssh/authorized_keys'
  3. Configure SSH:Ensure that SSH on the server is configured to accept key-based authentication.
    Open the SSH configuration file:

    sudo nano /etc/ssh/sshd_config

    Find the line that says PasswordAuthentication and change its value to no.
    This ensures that password authentication is disabled (optional, but recommended for security).

    PasswordAuthentication no

    Save the file and exit the editor. Then, restart the SSH service:

    sudo systemctl restart sshd
  4. Test SSH Connection:Try to SSH into your server again:
    ssh username@server_ip

    If everything is configured properly, you should be able to access the site without needing to enter a password.

  5. Optional: Disable Password Authentication:Once you’ve confirmed that SSH key authentication is working, you can disable password authentication altogether to enhance security.
    To do this, open the SSH configuration file again:

    sudo nano /etc/ssh/sshd_config

    Find the line that says PasswordAuthentication and change its value to no.

    PasswordAuthentication no

    Save the file and exit the editor. Then, restart the SSH service:

    sudo systemctl restart sshd

And that’s it! On Ubuntu 20.04, you have successfully configured SSH key authentication.
You can now safely access your server without entering a password each time.

 

Post Your Comment

Free Migration Assistance

If you need assistance with migrating your current data from another provider, we would be more than happy to assist.