Step-by-Step Guide- How to Add a Key to the Authorized Keys List for Enhanced SSH Access Security

by liuqiyue

How to Add Key to Authorized_keys

In the realm of SSH (Secure Shell) authentication, the authorized_keys file plays a crucial role in ensuring secure access to remote systems. This file contains public keys that grant users permission to log in without the need for a password. If you are looking to add a new key to the authorized_keys file, follow these steps to do so efficiently.

Step 1: Generate SSH Key Pair

Before adding a key to the authorized_keys file, you need to generate an SSH key pair. This pair consists of a public key and a private key. The public key is stored on the server, while the private key is kept on the client machine.

To generate a key pair, open a terminal on your client machine and run the following command:

“`
ssh-keygen -t rsa -b 4096
“`

This command creates a new RSA key pair with a key size of 4096 bits. You will be prompted to enter a file path for the key pair and a passphrase to protect the private key. Once you have generated the key pair, you can proceed to the next step.

Step 2: Copy Public Key to Server

Now that you have the public key, you need to copy it to the server where you want to add it to the authorized_keys file. Use the `ssh-copy-id` command to achieve this:

“`
ssh-copy-id -i ~/.ssh/id_rsa.pub username@server_ip
“`

Replace `username` with your actual username on the server, and `server_ip` with the server’s IP address. This command will prompt you for the password of the specified user on the server. Once the key is copied, you can move on to the next step.

Step 3: Add Key to Authorized_keys File

To add the public key to the authorized_keys file on the server, you need to edit the file. You can use a text editor like `nano`, `vim`, or `vi` for this purpose. Run the following command on the server:

“`
nano ~/.ssh/authorized_keys
“`

In the editor, paste the contents of the public key that you copied to the server in the previous step. Make sure that the key is on a new line and that there are no leading or trailing spaces.

Step 4: Save and Exit

After adding the key to the authorized_keys file, save and exit the editor. If you used `nano`, press `Ctrl + X`, then `Y`, and finally `Enter`. If you used `vim` or `vi`, press `Esc`, type `:wq`, and press `Enter`.

Step 5: Set Proper Permissions

To ensure that the authorized_keys file is secure, set the correct permissions. Run the following command on the server:

“`
chmod 600 ~/.ssh/authorized_keys
“`

This command restricts access to the file, allowing only the owner to read and write it.

Step 6: Test SSH Connection

Finally, test the SSH connection to ensure that the new key is working correctly. Open a terminal on your client machine and run the following command:

“`
ssh username@server_ip
“`

If everything is set up correctly, you should be able to log in without entering a password.

By following these steps, you can successfully add a key to the authorized_keys file and enjoy secure SSH access to your remote server.

You may also like