How to Add Public Key to Authorized_keys
In the realm of secure remote access, adding a public key to the authorized_keys file is a crucial step in ensuring that only authorized users can gain access to a system. This process is commonly used in SSH (Secure Shell) to establish secure connections between a client and a server. By adding a public key to the authorized_keys file, you can prevent unauthorized access and streamline the login process for legitimate users. In this article, we will guide you through the steps to add a public key to the authorized_keys file on a Linux system.
Understanding SSH Keys
Before diving into the process of adding a public key to the authorized_keys file, it’s essential to understand the basics of SSH keys. SSH keys consist of a pair of keys: a public key and a private key. The public key is used to authenticate the user, while the private key is kept secret and should never be shared. When a user attempts to connect to a server using SSH, the server sends a challenge to the client’s private key, which is then decrypted using the corresponding public key. If the decryption is successful, the server grants access to the user.
Generating SSH Keys
To add a public key to the authorized_keys file, you first need to generate an SSH key pair. You can do this using the ssh-keygen command. Open a terminal on your client machine and run the following command:
“`
ssh-keygen -t rsa -b 4096
“`
This command generates a new RSA key pair with a key size of 4096 bits. You will be prompted to enter a file path for the private key and a passphrase for additional security. Once you have generated the key pair, you can find the public key in the same directory as the private key, with a filename that ends in `.pub`.
Adding the Public Key to the Authorized_keys File
Now that you have generated the SSH key pair, you need to add the public key to the authorized_keys file on the server. To do this, follow these steps:
1. Log in to the server using SSH.
2. Navigate to the directory where the authorized_keys file is located. This is typically the user’s home directory, which can be accessed using the following command:
“`
cd ~
“`
3. Open the authorized_keys file in a text editor. You can use any text editor you prefer, such as nano, vi, or vim. For example, to open the file using nano, run the following command:
“`
nano .ssh/authorized_keys
“`
4. Copy the contents of your public key and paste it into the authorized_keys file. Ensure that the key is on a new line and that there are no leading or trailing spaces.
5. Save the file and exit the text editor.
Testing the SSH Connection
To ensure that the public key has been added successfully, you can now test the SSH connection from your client machine. Open a terminal and run the following command:
“`
ssh username@server_ip
“`
Replace `username` with your actual username and `server_ip` with the IP address or hostname of the server. If the connection is successful, you should be logged in without being prompted for a password.
Conclusion
Adding a public key to the authorized_keys file is a straightforward process that enhances the security of your SSH connections. By following the steps outlined in this article, you can ensure that only authorized users can access your server. Remember to keep your private key secure and never share it with others.