Pages

Thursday, December 21, 2023

Creating a Password-Free Connection Between Two Servers with Proper Permissions

Creating a Password-Free Connection Between Two Servers with Proper Permissions

Setting up a password-free SSH connection between two servers, A and B, enhances security and efficiency. This guide will walk you through establishing a secure, key-based connection, ensuring all permissions are correctly set to 600 for safety.

Step 1: Generate SSH Keys

  • On Both Server A and B:

    1. Run ssh-keygen to create a pair of keys: id_rsa (private) and id_rsa.pub (public).
    2. Ensure the permissions of these keys are set to 600.

Step 2: Exchange Public Keys

  • On Server A:
    1. Copy the id_rsa.pub of Server A to the authorized_keys file of Server B.
  • On Server B:
    1. Similarly, copy the id_rsa.pub of Server B to the authorized_keys file of Server A.

Connecting Using Private Key:

  • To connect to Server A from B (or vice versa), use the private key (id_rsa) with the following command:
    ssh user@xxx.xxx.xxx.xxx -i <path-to-private-key>
    Replace xxx.xxx.xxx.xxx with the actual server IP and specify the correct path to your private key.

Additional Tips:

  • From any other server (say Server Z with IP zzz.zzz.zzz.zzz), you can use the ssh-copy-id command to easily copy your public key to Servers A or B:
    ssh-copy-id root@zzz.zzz.zzz.zzz
  • Reverse the process to allow the opposite connection.

Important Notes:

  • Always ensure your keys' permissions are set to 600 to prevent unauthorized access.
  • Regularly update and rotate your keys for enhanced security.

By following these steps, you'll establish a secure, password-free connection between two servers, ensuring efficient and safe operations.

No comments:

Post a Comment