How To Set Up SSH Keys

About SSH Keys

SSH keys provide a more secure way of logging into a virtual private server with SSH than using a password alone. While a password can eventually be cracked with a brute force attack, SSH keys are nearly impossible to decipher by brute force alone. Generating a key pair provides you with two long string of characters: a public and a private key. You can place the public key on any server, and then unlock it by connecting to it with a client that already has the private key. When the two match up, the system unlocks without the need for a password. You can increase security even more by protecting the private key with a passphrase.

 

 

 

First Step: Create the RSA Key Pair

The first step is to create the key pair on the client machine (there is a good chance that this will just be your computer):

Actually, You can do this either on Client or Server, both is fair enough.

> ssh-keygen

 

 

Second Step: Store the Keys and Passphrase

Once you have entered the Gen Key command, you will get a few more questions:

Enter file in which to save the key (/home/ubuntu/.ssh/id_rsa):

You can press enter here, saving the file to the user home (in this case, my example user is called ubuntu).

Enter passphrase (empty for no passphrase):

It’s up to you whether you want to use a passphrase. Entering a passphrase does have its benefits: the security of a key, no matter how encrypted, still depends on the fact that it is not visible to anyone else. Should a passphrase-protected private key fall into an unauthorized users possession, they will be unable to log in to its associated accounts until they figure out the passphrase, buying the hacked user some extra time. The only downside, of course, to having a passphrase, is then having to type it in each time you use the Key Pair.

The entire key generation process looks like this:

 

> ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/demo/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/ubuntu/.ssh/id_rsa.
Your public key has been saved in /home/ubuntu/.ssh/id_rsa.pub.
The key fingerprint is:
4a:dd:0a:c6:35:4e:3f:ed:27:38:8c:74:44:4d:93:67 ubuntu@<your ssh-server hostname>
The key’s randomart image is:
+–[ RSA 2048]—-+
|          .oo.   |
|         .  o.E  |
|        + .  o   |
|     . = = .     |
|      = S = .    |
|     o + = +     |
|      . o + o .  |
|           . o   |
|                 |
+—————–+

The public key is now located in /home/ubuntu/.ssh/id_rsa.pub The private key (identification) is now located in /home/ubuntu/.ssh/id_rsa

 

Third Step: Copy the Public Key

Once the key pair is generated, it’s time to place the public key on the virtual server that we want to use.

You can copy the public key into the new machine’s authorized_keys file with the ssh-copy-id command. Make sure to replace the example username and IP address below.

 

If you did ssh-kegen generation on Server, then just execute the ssh-copy-id, below is some examples to excute ssh-copy-id

> ssh-copy-id localhost

> ssh-copy-id <your ssh-server ip/hostname>

 

Alternatively, you can paste in the keys using SSH:

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

 

 

If you did ssh-kegen generation on Client, then you must follow this step.

> ssh-copy-id user@<your ssh-server ip/domain>

or

> cat ~/.ssh/id_rsa.pub | ssh user@<your ssh-server ip/domain> “mkdir -p ~/.ssh && cat >>  ~/.ssh/authorized_keys”

Optional Step – Disable the Password for Root Login

Once you have copied your SSH keys unto your server and ensured that you can log in with the SSH keys alone, you can go ahead and restrict the root login to only be permitted via SSH keys.

 

In order to do this, open up the SSH config file:

> vim /etc/ssh/sshd_config

 

Within that file, find the line that includes PermitRootLogin and modify it to ensure that users can only connect with their SSH key:

PermitRootLogin without-password

 

Put the changes into effect:

> reload ssh

 

 

Tips:

1. SSH Login Without Password:

Prefer to do the ssh-copy-id command, it will properly solve the problem

2. Run sudo command without prompt for password:

Question: “How does the ubuntu user on the AWS images for Ubuntu Server 12.04 have passwordless sudo for all commands when there is no configuration for it in /etc/sudoers?

 

Solution:

I have discovered the answer so may as well put it here for completeness. At the end of /etc/sudoers there is what I thought was just a comment:

#includedir /etc/sudoers.d

 

However this actually includes the contents of that directory. Inside of which is the file /etc/sudoers.d/90-cloudimg-ubuntu. It has the expected contents

# ubuntu user is default user in cloud-images.

# It needs passwordless sudo functionality.

ubuntu ALL=(ALL) NOPASSWD:ALL

So that is where the sudo configuration for the default ubuntu user lives.

You should edit this file using visudo. The following command will let you edit the correct file with visudo.

> visudo -f /etc/sudoers.d/90-cloudimg-ubuntu

 

And add a line like this below, at the end.

nghiale ALL=(ALL) NOPASSWD:ALL

 

RELATED POSTS
Share the Post: