How to add the public SSH key to the authorized_keys file on a Linux server?

LinuxLinuxBeginner
Practice Now

Introduction

This tutorial will guide you through the process of adding your public SSH key to the authorized_keys file on a Linux server. By completing this task, you can establish a secure and efficient remote access to your Linux system, simplifying your administration workflow.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/UserandGroupManagementGroup(["`User and Group Management`"]) linux(("`Linux`")) -.-> linux/RemoteAccessandNetworkingGroup(["`Remote Access and Networking`"]) linux(("`Linux`")) -.-> linux/PackagesandSoftwaresGroup(["`Packages and Softwares`"]) linux/UserandGroupManagementGroup -.-> linux/env("`Environment Managing`") linux/RemoteAccessandNetworkingGroup -.-> linux/ssh("`Secure Connecting`") linux/RemoteAccessandNetworkingGroup -.-> linux/scp("`Secure Copying`") linux/RemoteAccessandNetworkingGroup -.-> linux/sftp("`Secure File Transferring`") linux/PackagesandSoftwaresGroup -.-> linux/openssl("`OpenSSL`") subgraph Lab Skills linux/env -.-> lab-409800{{"`How to add the public SSH key to the authorized_keys file on a Linux server?`"}} linux/ssh -.-> lab-409800{{"`How to add the public SSH key to the authorized_keys file on a Linux server?`"}} linux/scp -.-> lab-409800{{"`How to add the public SSH key to the authorized_keys file on a Linux server?`"}} linux/sftp -.-> lab-409800{{"`How to add the public SSH key to the authorized_keys file on a Linux server?`"}} linux/openssl -.-> lab-409800{{"`How to add the public SSH key to the authorized_keys file on a Linux server?`"}} end

Understanding SSH and SSH Keys

Secure Shell (SSH) is a cryptographic network protocol used for secure communication and remote access to computers and servers over an unsecured network, such as the internet. SSH provides a secure way to connect to a remote system, execute commands, and transfer files, ensuring the confidentiality and integrity of the data exchanged.

One of the key features of SSH is the use of SSH keys, which are a pair of cryptographic keys (a public key and a private key) used for authentication. The public key is shared with the remote server, while the private key is kept securely on the client's machine. When the client connects to the server, the server verifies the client's identity by checking the public key.

Using SSH keys has several advantages over traditional password-based authentication:

  1. Increased Security: SSH keys are much more secure than passwords, as they are based on cryptographic algorithms that are difficult to crack. This makes it much harder for unauthorized users to gain access to the remote system.

  2. Automated Authentication: SSH keys allow for automated authentication, which is particularly useful for scripts and automated tasks that need to connect to remote systems without user intervention.

  3. Improved Scalability: SSH keys can be easily managed and distributed to multiple users or systems, making it easier to scale secure remote access.

  4. Reduced Risk of Password Compromise: With SSH keys, there is no need to remember or store passwords, reducing the risk of password compromise.

To use SSH keys, you first need to generate a public-private key pair. The public key is then added to the authorized_keys file on the remote server, which allows the server to authenticate the client's identity during the SSH connection.

graph LR A[Client] -- Public Key --> B[Remote Server] B -- Private Key --> A

By understanding the basics of SSH and SSH keys, you can enhance the security and efficiency of your remote access and administration tasks on Linux servers.

Generating a Public SSH Key

To generate a public SSH key, you can use the ssh-keygen command, which is a standard tool provided by the SSH package on Linux systems.

Steps to Generate a Public SSH Key

  1. Open a terminal on your Linux system.

  2. Run the following command to generate a new SSH key pair:

    ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
    • -t rsa: Specifies the type of encryption algorithm to use, in this case, RSA.
    • -b 4096: Specifies the bit length of the RSA key, which is set to 4096 bits for increased security.
    • -C "your_email@example.com": Adds a comment to the key, typically your email address, for identification purposes.
  3. The command will prompt you to enter a file location to save the key pair. You can press Enter to accept the default location (~/.ssh/id_rsa).

  4. Next, you will be prompted to enter a passphrase for the private key. It is recommended to use a strong passphrase to protect your private key. However, if you don't want to use a passphrase, you can simply press Enter twice to skip this step.

After completing these steps, you will have generated a new public-private SSH key pair. The public key will be saved in the file ~/.ssh/id_rsa.pub, and the private key will be saved in the file ~/.ssh/id_rsa.

To view the contents of the public key, you can use the following command:

cat ~/.ssh/id_rsa.pub

This will display the public key, which you can then copy and use to configure the authorized_keys file on the remote Linux server.

Configuring the authorized_keys File

The authorized_keys file is a special file located in the .ssh directory of the user's home directory on the remote Linux server. This file contains the public keys of users who are authorized to access the server using SSH.

Steps to Add the Public SSH Key to the authorized_keys File

  1. Connect to the remote Linux server using an SSH client, such as PuTTY or the built-in SSH client on your Linux system.

  2. Navigate to the user's home directory on the remote server. Typically, this can be done using the following command:

    cd ~
  3. Check if the .ssh directory exists. If not, create it using the following command:

    mkdir .ssh
  4. Open the authorized_keys file in a text editor. If the file does not exist, you can create it:

    nano .ssh/authorized_keys
  5. Copy the contents of the public SSH key you generated earlier (the output of the cat ~/.ssh/id_rsa.pub command) and paste it into the authorized_keys file.

  6. Save the file and exit the text editor.

After completing these steps, the public SSH key has been added to the authorized_keys file on the remote Linux server. Now, when you try to connect to the server using SSH, the server will authenticate your identity using the public key, and you will be able to access the server without having to enter a password.

Here's an example of what the authorized_keys file might look like:

ssh-rsa AAAAB3NzaC1yc2EAA...your_public_key_content...== user@example.com

By configuring the authorized_keys file, you have set up SSH key-based authentication, which provides a more secure and efficient way to access your Linux server remotely.

Summary

In this Linux tutorial, you have learned how to generate a public SSH key, configure the authorized_keys file, and enable secure remote access to your Linux server. By following these steps, you can enhance the security and convenience of your Linux administration tasks.

Other Linux Tutorials you may like