How To Copy SSH Key to a Remote Computer

We can log into a remote Linux server either by a username / password combination or by using an SSH key. SSH keys provide a more secure way for the authentication to happen. Better yet, we can protect the key with a passphrase so that if the local computer (the one with the private key stored) is stolen, the person who has access to that computer cannot connect to the remote computer with just the key.

We essentially need to perform the following steps:

Create the RSA key pair

Store the keys (with or without passphrase)

Copy the public key to the remote computer

Remove root login.

This lab uses two computers running Ubuntu Linux 15.04.01

LocalCom: 192.168.1.7

RemoteCom: 192.168.1.8

Prepare LocalCom:

Elevate account privilage: sudo su

Rename the server: hostnamectl set-hostname localcom

Logoff: exit twice

Log back in

Change IP address: vi /etc/network/interfaces

Enable root login

vi /etc/ssh/sshd_config

Comment the line: PermitRootLogin without-password

Enter the line: PermitRootLogin yes

Restart SSHD: service ssh restart

Change Root Password: passwd root

Prepare Remotecom: do the same steps for RemoteCom but use IP address 192.168.1.8 and host name remotecom.

At this point, you can SSH to any of the two computers as root.

Step 1: Generate the RSA key Pair

ssh-keygen –t rsa

Step 2: Store the keys (and passphrase if there is any)

When asked for file name, keep the default one. Press Enter to leave the passphrase empty for the purpose of this lab. If you want to enter a passphrase, go ahead and do so but remember that every time you login using the keys, you will be prompted to enter the passphrase.