Adding SSH Key to Terminal for Easy Authentication

When connecting to your remote devices via SSH, it is important to make sure that you are authenticated. Otherwise just anyone would be able to log into the device and do as they wish. Which is generally a bad thing.

Up until recently I have always (which was infrequently, if I'm honest) just typed the command as and when I needed it;

Which works. After initiating this command I am prompted for the password, and all is good in the world.

BUT SIMPLICITY

A way to save a few key strokes (because who doesn't want to save their fingers from touching a few more keys) would be to configure your ~/.ssh/config file to include an entry for the hostname which would then allow you to initiate the connection with just that.

This is trivial to setup. And oh so satisfying to use. Win-Win.

First create a config file within ~/.ssh (the .ssh folder within your user path).

Secondly, add an entry for the host you want to configure;

HOST rpi
    HostName 192.168.1.104
    User pi

This allows me to then initiate the connection with the simple command ssh rpi. Doing so causes a lookup in the config to occur, and on finding a match we attempt to connect using its configuration.

BUT COMPLEXITY

What about if we want to log in within something like a private key? Something like AWS would issue as part of the creation of an EC2 instance?

Well we can handle that as well, by specifying an IndentifyFile.

HOST rpi
    HostName 192.168.1.104
    User pi
    IdentityFile ~/.ssh/ec2-private.key

Now when we run the command ssh rpi the identify file will be used in the authentication process and substantially streamline our login process. This really comes into its own when the hostnames are rather long (like EC2 names are).

Additionally it allows for multiple hosts with multiple passwords (or private keys) to be configured and without the need to remember them all.

Further, because the keys are stored within your local user .ssh folder they are protected by the OS which prevents others from accessing them.