Setup ssh login with pem file without password on ubuntu/linux server

Login to VPS with account user (not root)

mkdir pem
cd pem
ssh-keygen -b 2048 -f identity -t rsa -m PEM

Enter to leave passphare empty. It will gen 2 files in pem dir (identity and identity.pub)

Copy public key contents to authorized_keys

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

Can check with

nano ~/.ssh/authorized_keys

Disable Password Authentication

sudo nano /etc/ssh/sshd_config

Update PasswordAuthentication from “yes” to “no” as below:

# Change to no to disable tunnelled clear text passwords
PasswordAuthentication no

Restart SSH:

sudo service ssh restart

Download your private key to client side:

Copy the contents of the file private key file identity on vps to a key file identity.pem on your local system, just copy and paste the data into a new file.

cat ~/.ssh/pem/identity

The content look like this:

— — -BEGIN RSA PRIVATE KEY — — -
……
— — -END RSA PRIVATE KEY — — -

Set permission for pem on your local:

sudo chmod 600 identity.pem

Test login:

ssh -i identity.pem user@vps-ip

vinhvd

Hello there! 👋 I'm Vinh.