Generate a SSH key to log in via SSH

A SSH key removes the need for a physical password to log in to a remote machine via SSH. To generate an SSH key on the machine you are using to log in, (not the remote machine you wish to log into), use the following code in Terminal:

 ssh-keygen -t rsa 

You will have an option to select where to save the SSH key, and select a passphrase.

Once the SSH key has been generated it will need to be copied to the remote machine you want to log into using SSH. Replacing USERNAME with the correct username, HOSTNAME with the correct hostname, and PORT with the correct port number, (-p PORT can be omitted if using the standard port 22 for SSH), use the following code:

 ssh-copy-id USERNAME@HOSTNAME -p PORT 

Optionally the remote machine can be set up to only allow access using a SSH key and not via a password. On the remote machine enter the following code in Terminal to edit the OpenSSH configuration file:

 sudo nano /etc/ssh/sshd_config 

Make sure that the line that includes PermitRootLogin is set to either without-password or prohibit-password. If any changes are made to the configuration file then OpenSSH will need to be restarted using:

 sudo service ssh restart 

If you have encrypted the disk on the remote machine, you may still be prompted for the user password on first login.

Source:
https://www.ssh.com/ssh/copy-id

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s