Step-by-Step Guide to Creating an SSH Authorized_keys File for Secure Remote Access

by liuqiyue

How to Create ssh authorized_keys File

Creating an ssh authorized_keys file is a crucial step in setting up secure SSH access to your server. This file allows you to specify which public keys are allowed to authenticate against your server, thereby enhancing the security of your system. In this article, we will guide you through the process of creating an ssh authorized_keys file on a Linux server.

Step 1: Generate SSH Keys

Before you can create an ssh authorized_keys file, you need to generate an SSH key pair. You can do this using the ssh-keygen command. Open your 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 your private key and a passphrase for your key. You can leave the default file path and passphrase if you are not prompted.

Step 2: Copy the Public Key to the Server

Once you have generated your SSH key pair, you need to copy the public key to the server. You can do this by using the ssh-copy-id command. Run the following command on your local machine:

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

Replace `username` with your username and `server_ip` with the IP address or hostname of your server. You will be prompted to enter your password to authenticate the copy process.

Step 3: Create the ssh authorized_keys File

After copying the public key to the server, you need to create the ssh authorized_keys file. You can do this by opening the file in a text editor. On the server, navigate to the user’s home directory and run the following command:

“`
nano ~/.ssh/authorized_keys
“`

This command will open the ssh authorized_keys file in the nano text editor. If the file does not exist, nano will create it for you.

Step 4: Add the Public Key to the ssh authorized_keys File

Now, you need to add your public key to the ssh authorized_keys file. Copy the contents of your public key from your local machine and paste it into the nano editor. Make sure to remove any leading or trailing whitespace. Once you have added the public key, save and close the file.

Step 5: Set the Correct Permissions

To ensure that your SSH keys are secure, you need to set the correct permissions on the ssh authorized_keys file. Run the following command on the server:

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

This command will set the file permissions so that only the owner can read and write the file, and no one else can access it.

Conclusion

Creating an ssh authorized_keys file is an essential step in securing your SSH access to a Linux server. By following the steps outlined in this article, you can generate SSH keys, copy them to your server, and add them to the authorized_keys file. This will help you maintain a secure and controlled SSH access to your server.

You may also like