Step-by-Step Guide to Adding an Authorized SSH Key for Secure Remote Access

by liuqiyue

How to Add Authorized Key in SSH

Adding an authorized key in SSH is a crucial step in securing your server and ensuring that only authorized users can access it. SSH, or Secure Shell, is a network protocol that allows data to be exchanged over a secure channel between two networked computers. By adding authorized keys, you can control who has access to your server and what actions they can perform. In this article, we will guide you through the process of adding an authorized key in SSH.

Understanding SSH Keys

Before we dive into the steps of adding an authorized key, it is essential to understand what SSH keys are. SSH keys are a pair of cryptographic keys: 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. When a user wants to connect to the server, they use their private key to authenticate themselves. The server then verifies the authenticity of the private key by comparing it with the public key stored on the server.

Generating SSH Keys

To add an authorized key, you first need to generate an SSH key pair on your client machine. You can do this by using the `ssh-keygen` command. Open a terminal and run the following command:

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

This command will generate a new RSA key pair with a key length of 4096 bits. You will be prompted to enter a file path for the private key and a passphrase for additional security. Make sure to remember the passphrase, as you will need it to unlock the private key when connecting to the server.

Adding the Public Key to the Server

Once you have generated the SSH key pair, you need to add the public key to the server. You can do this by using the `ssh-copy-id` command. Open a terminal and run the following command:

“`
ssh-copy-id username@server_ip
“`

Replace `username` with your username on the server and `server_ip` with the IP address or hostname of the server. You will be prompted to enter your password to authenticate the connection. The public key will then be added to the server’s `~/.ssh/authorized_keys` file.

Verifying the Connection

After adding the authorized key, you can verify the connection by trying to log in to the server using SSH. Open a terminal and run the following command:

“`
ssh username@server_ip
“`

You should now be connected to the server without being prompted for a password. If you are still prompted for a password, double-check that the public key was added correctly to the server’s `authorized_keys` file.

Conclusion

Adding an authorized key in SSH is a simple yet effective way to secure your server and control access to it. By following the steps outlined in this article, you can generate an SSH key pair, add the public key to the server, and verify the connection. Remember to keep your private key secure and use strong passphrases for added security.

Related Posts