How to Add a Public Key to the Server ?

From PheonixSolutions
Jump to navigation Jump to search

Introduction

[edit]

Ensuring secure access to servers is paramount in the realm of server administration, particularly when managing multiple servers across different environments. One of the most secure methods for authentication is through SSH key pairs, which consist of a public key (stored on the server) and a corresponding private key (stored on the client). By adding a public key to a server, administrators can authenticate to that server securely without the need for passwords.

In this guide, we'll walk through the process of adding a public key to a server, enabling seamless and secure access between servers. We'll cover the necessary prerequisites, step-by-step instructions for adding the public key, and verification steps to ensure successful configuration. By following this guide, you'll be equipped to enhance the security of your server infrastructure while streamlining the authentication process for server access.

Prerequisites

[edit]
  • Access to both servers with administrative privileges (e.g., root or sudo access).
  • Have the public key file of the source server ready (usually with a ‘.pub’ extension).

Steps

[edit]

1. Connect to the Source Server:

Use SSH to connect to the source server from which you want to copy the public key.

 ssh username@source_server_ip

2. Open and Copy Public Key:

Open the public key file (usually with a '.pub' extension) using a text editor on the source server. Copy the entire contents of the public key.

 sudo vi ~/.ssh/id_rsa.pub

3. Switch to the Destination Server:

Switch to the destination server where you want to add the public key. Open or create the authorized_keys file in the .ssh directory of the destination server.

 ssh username@destination_server_ip
 vi ~/.ssh/authorized_keys

4. Paste Public Key:

In the authorized_keys file on the destination server, paste the contents of the public key that you copied from the source server. Save the file.

5. Set Correct Permissions:

Ensure that the authorized_keys file on the destination server has the correct permissions.

 chmod 600 ~/.ssh/authorized_keys

6. Test Connection:

Login to the source server and try to connect to the destination server using the corresponding private key of the source server.

 ssh -i /path/to/private_key username@destination_server_ip

If successful, you should be able to login to the destination server.

Conclusion

[edit]

Adding a public key to a server allows you to securely authenticate to the server using SSH key pairs. By following the steps outlined in this guide, you can effectively add a public key to a server and establish secure connections between servers. This enhances security and simplifies the authentication process when accessing remote servers.