Hey everyone and welcome back. Now previously when we have been authorising our SSH sessions
we have been reliant on using passwords. In this nugget here we're going to see how we
can make our lives a little bit easier and by the way this is indeed the recommended
more secure way to handle your SSH connections. We're going to be relying on SSH keys. So
check this out then what I will do here is I will go onto my server here and if we happen
to look within our exe directory again we have an SSH directory. Within here we can
see all of these different files. Now let me just do a long listing here so you can
see this a little bit better. In fact let me just full screen this right now. So these
files right here the ones that end in dot pub these are the public key files. Now some
of these ones you can see here for example this one is an RSA key. This means it is using
the RSA encryption algorithm. Here we have others like the elliptical curve algorithm
so on and so forth. But the point here to note here is that our dot pubs those are public
we can share these the ones which do not have dot pub those are private please please please
do not be sharing these out these are meant to be private. So what I want to do is I want
to show you how we can actually generate our own keys. So what I will do here is if I just
clear the screen I say man SSH hyphen key gen. So we can see this is part of the open
SSH suite of utilities. What this is going to do is going to allow us to generate an
authentication key. Now if you happen to page right through all of these different options
you're going to see we have a ton of switches which can do a ton of things and this example
here we're going to keep things really quite simple. The only switch which I am going to
be focusing on is this one here the dash T which is going to allow us to specify the
type of key we want to create. So again similar to what we saw before we had those RSA dot
pub files or the elliptical curve pub files we can specify the type of encryption we want
to use. We will just use the RSA type encryption for our example but again you can choose whatever
you wish. So what I will do here is I will just escape out by pressing Q to quit and
I will clear the screen once again. I will generate this key by saying SSH hyphen key
gen and I will use the dash T flag to specify the type of encryption we want. I am going
to say RSA. So if I hit enter it is going to tell me that we are going to generate a
public and private key pair. Where do we want to save the key? Now notice this is actually
a user specific key because check out the location of where it is going to be stored.
I can change this location but I will just hit enter to keep it as it is. I can add a
pass phrase but again I don't have to keeping things simple I will just hit enter and leave
the pass phrase empty. Now we can see here we actually have generated these keys so if
I do an LS in fact I have to go into my dot SSH directory as per the location specified
right here. So I will do CD dot SSH if I do a PWD we can see where we are doing LS. Now
we notice I have this file here which is my public key and I have this one here which
is the private key. Again the public key is the one we want to share. The private is the
one we never share. So what I want to do is I actually want to copy this public key to
the remote server where I would usually authenticate into using my password. So what I now want
to do is I want to actually copy this particular file so I will say SSH hyphen copy hyphen ID
and then I will use the dash I flag and now I will specify the key I want to copy which
is going to be RSA or ID underscore RSA pub. And now I want to specify where do I want
to copy this so I am going to copy it to server 2 so I will use server 2's login credentials
which is IPv0 at 192.1684.28. So now if I hit enter I am going to have to type in the
password for server 2 which I have and now we can see here the number of keys added is
1. Now it says here try logging into this machine. So what I can do here is just say SSH IPv0
and I will do 4.28 and hit enter and as we can see here we automatically log in to the
server without me having to type in my password anymore. I am not prompted for the password
like I was up here. That is not present here. We automatically log in. That is because we
now have this public private key pair ultimately handling the authentication. So what I will
do is I can exit out of the server and back on server 1. Now if I actually want to create
some type of alias so that I don't even have to do IPv0 at 192.1684.28 like this instead
what I can do is within my configuration file. So I will go into my .ssh directory. We can
see here. I am going to create this config file so I will just use the .nano config and
within here I am going to use the keyword host. And now what I want to do is I want
to specify the alias I want to use for that particular host. So let's just maybe just
say I don't know we will call server 2 johnbox or johnbox 2 since it is server 2 whatever.
Now what I am going to do is I am going to go in 4 spaces and I am going to say host
name and be careful of the capitalization here and now I am going to specify the IP address
I want to target so I will say 4.28 and I will also specify the user again be careful of
the capitalization and I will say IPv0. So all I am ultimately doing here is creating
an alias rather than me logging in and saying ssh ipv0 at 192.1684.28. I can just now say
ssh johnbox 2 without having to give this username without having to specify this IP
address and like I say we don't have to supply the password anymore because that is now handled
with our ssh keys. So what I will do here is I will just save this configuration file.
If I clear the screen I do a cde. Now I will just say ssh johnbox 2 and hit enter. Notice
straight away we automatically log in immediately. No need to specify the username, no need to
specify an IP address and no need to specify a password. So clearly this is a very valuable
way to be able to handle your ssh connections especially if you happen to be using some
type of automation whereby you don't want to have to continually be prompted to manually
type in your password. You just want to have ssh keys set up so that the automation system
can automatically log in by virtue of being on your system and like I say if you want
to smooth out the authentication process you can actually use these aliases so that you
can also strip away the IP addresses or the domain names and the user names with these
nice short hands. Ok doc so that is us for our ssh configurations. I hope this has been
informative for you and I'd like to thank you for viewing.