Skip to content

step-by-step guide to setting up SSH passwordless login

Step 1: Generate SSH Keys on Client

On the Client Machine

Run the following command to generate SSH keys:

ssh-keygen -t rsa

This will create a pair of files: id_rsa (private key) and id_rsa.pub (public key).

Step 2: Copy Public Key to Server

Using SSH-Copy-ID

Run the following command to copy the public key to the server:

ssh-copy-id username@server_ip

Replace username with your actual username and server_ip with the IP address of your server.

Manual Method

If ssh-copy-id is not available, you can copy the public key manually:

scp ~/.ssh/id_rsa.pub username@server_ip:~/

Then, log in to the server and append the public key to the authorized_keys file:

cat ~/id_rsa.pub >> ~/.ssh/authorized_keys

Alternative way of adding keys

Add GitHub user public keys to authorized_keys

If you want to add all public keys for a GitHub user (convenient for provisioning accounts), GitHub exposes them at https://github.com/<username>.keys. (no login required)

Local (on the server) using curl:

1
2
3
4
mkdir -p ~/.ssh
chmod 700 ~/.ssh
curl -s https://github.com/GITHUB_USERNAME.keys >> ~/.ssh/authorized_keys
chmod 600 ~/.ssh/authorized_keys

Local (on the server) using wget:

1
2
3
4
mkdir -p ~/.ssh
chmod 700 ~/.ssh
wget -qO- https://github.com/GITHUB_USERNAME.keys >> ~/.ssh/authorized_keys
chmod 600 ~/.ssh/authorized_keys

Remote (from your client) using ssh + curl:

ssh username@server_ip 'mkdir -p ~/.ssh && chmod 700 ~/.ssh && curl -s https://github.com/GITHUB_USERNAME.keys >> ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys'

Remote (from your client) using ssh + wget:

ssh username@server_ip 'mkdir -p ~/.ssh && chmod 700 ~/.ssh && wget -qO- https://github.com/GITHUB_USERNAME.keys >> ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys'

Replace GITHUB_USERNAME with the GitHub account name whose public keys you want to add. These commands create the .ssh folder if missing and set safe permissions.

Step 3: Configure SSH Server

Edit SSHD Config

Edit the SSH server configuration file (usually /etc/ssh/sshd_config) and ensure the following lines are uncommented:

PubkeyAuthentication yes
AuthorizedKeysFile  .ssh/authorized_keys

Restart the SSH server to apply changes:

sudo service ssh restart

Step 4: Test Passwordless Login

Connect to Server

Try connecting to the server using SSH:

ssh username@server_ip

You should be logged in without being prompted for a password.

The content provided is generated with the help of artificial intelligence (AI) and may contain inaccuracies or outdated information due to the limitations of AI. While I strive to review and validate the content, some errors or inaccuracies may still be present in the final output. Please use this content as a general guide only and verify any critical information through reputable sources before relying on it. I appreciate your understanding and feedback in helping us improve the accuracy and quality of our AI-generated content."