1. Generate Public and Private Keys
  2. Copy Public Key to the target machine
  3. Set appropriate permissions
  4. Create Configuration File for auto login
    1. Example contents of the new file
    2. Login using machine identifier
    3. Save your keys and configuration file in a “safe place” for later
    4. Disable remote login using passwords
      1. Fair warning!

SSH keys allow you to login into your SSH server without having to remember passwords and with much stronger security. It’s like having a super long password that you never have to remember… Just keep your private keys in a safe place.

Generate Public and Private Keys

Generate public keys. The command will produce two keys, one public (~/.ssh/example.com-id.pub) and one private (~/.ssh/example.com-id).

ssh-keygen -f ~/.ssh/example.com-id

Copy Public Key to the target machine

Your remote server needs to know your public key, so you need to send it there.

#copy the public key to the target machine
ssh-copy-id -i ~/.ssh/example.com-id.pub exampleuser@example.com

Set appropriate permissions

Your private key should only be visible to you and not other users, otherwise ssh will complain and not allow you to use the keys.

chmod 0700 ~/.ssh/
chmod 0600 ~/.ssh/example.com-id

Create Configuration File for auto login

This config file will tell ssh where are the configurations for an alias for your now key-authenticated server.

vim ~/.ssh/config

Example contents of the new file

#alias of the machine into which you are logging in
Host example.com

#hostname or ip of the machine
HostName example.com

#the username with which you are logging in
User exampleuser

#the private key, without "-pub"
IdentityFile ~/.ssh/example.com-id

Login using machine identifier

Now you can login using ssh and the alias you choose in the config file above.

ssh example.com

Save your keys and configuration file in a “safe place” for later

You can login from other computers using the same public key without having to configure the server again. All you need is to copy:

  • The ~/.ssh/config file
  • The public (~/.ssh/example.com-id.pub) and private keys (~/.ssh/example.com-id)

into your ~/.ssh directory in the new machine.

Disable remote login using passwords

vim /etc/ssh/sshd_config

Search for the line below and uncomment PasswordAuthentication No

# Change to no to disable tunnelled clear text passwords
#PasswordAuthentication no #change to PasswordAuthentication no without the hash

Restart the sshd service

sudo service ssh restart

Fair warning!

Remember, if others also get these keys and configuration file they can log into your server!