1. Generate SSH Key Pair
Run the following command on your local machine to create an SSH key pair:
- Do not set a password; just press
Enter
when prompted for a passphrase. - This creates the private key
id_rsa
and the public keyid_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:
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:
4. Set Permissions on the Remote Server
Ensure the correct permissions for the .ssh
directory and authorized_keys
file:
5. Test SSH Passwordless Login
Try logging into the remote server without a password:
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 theauthorized_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 theauthorized_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.