Wednesday, May 4, 2016

Setting Up SSH Passwordless Login

To configure SSH for passwordless login, follow these steps:


1. Generate SSH Key Pair

Run the following command on your local machine to create an SSH key pair:

ssh-keygen -t rsa
  • Do not set a password; just press Enter when prompted for a passphrase.
  • This creates the private key id_rsa and the public key id_rsa.pub in the ~/.ssh directory.

2. Create .ssh Directory on the Remote Server

Log in to the remote server and create the .ssh directory if it does not exist:

ssh {userId}@{ip} mkdir -p ~/.ssh exit

3. Copy Public Key to the Remote Server

Use the following command to append your public key (id_rsa.pub) to the remote server's authorized_keys file:

cat ~/.ssh/id_rsa.pub | ssh {userId}@{ip} 'cat >> ~/.ssh/authorized_keys'

4. Set Permissions on the Remote Server

Ensure the correct permissions for the .ssh directory and authorized_keys file:

ssh {userId}@{ip} chmod 700 ~/.ssh chmod 640 ~/.ssh/authorized_keys exit

5. Test SSH Passwordless Login

Try logging into the remote server without a password:

ssh {userId}@{ip}

Explanation of Commands

  • ssh-keygen -t rsa: Generates an RSA key pair for SSH authentication.
  • cat ~/.ssh/id_rsa.pub | ssh {userId}@{ip} 'cat >> ~/.ssh/authorized_keys': Appends your public key to the authorized_keys file on the remote server.
  • chmod 700 ~/.ssh: Sets the .ssh directory permissions to allow access only to the user.
  • chmod 640 ~/.ssh/authorized_keys: Sets the authorized_keys file permissions to ensure it's readable only by the user and owner.

Once complete, you can securely log into the remote server without needing to enter your password.