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

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

by liuqiyue

How to Add to Authorized_keys

Adding to the authorized_keys file is a crucial step in setting up secure SSH access to your server. This file is located in the .ssh directory of the user’s home directory and contains a list of public keys that are allowed to connect to the server. In this article, we will guide you through the process of adding a new public key to the authorized_keys file, ensuring that only authorized users can access your server.

Step 1: Generate SSH Key Pair

Before you can add a public key to the authorized_keys file, you need to generate an SSH key pair. You can do this using the ssh-keygen command. Follow these steps:

1. Open a terminal or command prompt.
2. Run the following command to generate a new SSH key pair:
“`
ssh-keygen -t rsa -b 4096
“`
3. You will be prompted to enter a file to save the key pair. Press Enter to use the default location.
4. You will be asked to enter a passphrase for your key. This is optional, but it adds an extra layer of security. If you choose to set a passphrase, you will need to enter it every time you use the key.

Step 2: Copy the Public Key

Once you have generated the SSH key pair, you need to copy the public key to the server. You can do this by using the ssh-copy-id command. Follow these steps:

1. Open a terminal or command prompt.
2. Run the following command, replacing “username” with your username and “server_ip” with the IP address or hostname of your server:
“`
ssh-copy-id username@server_ip
“`
3. You will be prompted to enter your password. Once you do, the public key will be added to the authorized_keys file on the server.

Step 3: Verify SSH Access

After adding the public key to the authorized_keys file, you should verify that SSH access is working correctly. Follow these steps:

1. Open a terminal or command prompt.
2. Run the following command, replacing “username” with your username and “server_ip” with the IP address or hostname of your server:
“`
ssh username@server_ip
“`
3. If you are prompted for a password, it means that SSH access is not working correctly. Check the permissions of the .ssh directory and the authorized_keys file to ensure that they are set correctly.
4. If you are not prompted for a password, you have successfully added your public key to the authorized_keys file, and you can now access your server using SSH.

Conclusion

Adding a public key to the authorized_keys file is an essential step in securing your server. By following the steps outlined in this article, you can ensure that only authorized users can access your server using SSH. Always remember to keep your private key secure and never share it with others.

Related Posts