Set up SSH with public-key authentication

SSH’ing into your machine using a password is both a hassle and insecure. The proper way to remotely access your machine is with public-key authentication. To set this up:

On remote server you’re SSHing into

$ sudo apt-get install openssh-server 

On local machine you’re SSHing from

# Generate a file that will store your RSA private key. You will be 
# prompted to provide a name for this file and a password to 
# protect it. This file will be added to your ~/.ssh directory.
$ ssh-keygen -t rsa
# Add your public key to the ~/.ssh/authorized_keys file of your user on
# the remote server.
$ ssh-copy-id -i <your key name> <remote username>@<remote IP address>

To SSH into remote machine using the newly generated keys

$ ssh -i <path to your key> <remote username>@<remote IP address>

If you don’t want to be prompted for your SSH key password everytime

# Add to the bottom of your ~/.bashrc
eval `ssh-agent -s`
ssh-add
1 Like

Similar solution without using ssh-copy-id:

cat ~/.ssh/id_rsa.pub | (ssh user@host "cat >> ~/.ssh/authorized_keys")