How to Add Public Key to authorized_keys in Linux
Adding a public key to the authorized_keys file in Linux is a crucial step in setting up SSH key-based authentication. This method provides a secure way to log in to a remote server without entering a password. In this article, we will guide you through the process of adding a public key to the authorized_keys file on a Linux system.
Step 1: Generate SSH Key Pair
Before adding your 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 on your local machine.
2. Run the following command to generate an SSH key pair:
“`
ssh-keygen -t rsa -b 4096
“`
3. You will be prompted to enter a file to save the key. Press Enter to accept the default location.
4. Next, you will be asked to enter a passphrase for your key. You can leave this blank if you don’t want to use a passphrase.
Step 2: Copy the Public Key to the Remote Server
Once you have generated the SSH key pair, you need to copy the public key to the remote server. You can do this by using the ssh-copy-id command. Follow these steps:
1. Open a terminal on your local machine.
2. Run the following command, replacing
“`
ssh-copy-id -i ~/.ssh/id_rsa.pub user@ip_address
“`
3. You will be prompted to enter the password for the user on the remote server. Once you enter the password, the public key will be added to the authorized_keys file on the remote server.
Step 3: Verify SSH Key-Based Authentication
After adding the public key to the authorized_keys file, you should verify that SSH key-based authentication is working correctly. Follow these steps:
1. Open a terminal on your local machine.
2. Run the following command, replacing
“`
ssh user@ip_address
“`
3. If everything is set up correctly, you should be logged in to the remote server without being prompted for a password.
Conclusion
Adding a public key to the authorized_keys file in Linux is a straightforward process that enhances the security of your SSH connections. By following the steps outlined in this article, you can set up SSH key-based authentication and enjoy a more secure remote login experience.