- You have already seen how to connect
to an SSH server before.
So I just want to add a couple of facts
that we haven't discussed before and demonstrate.
So when you first log into an SSH server,
the SSH server host key is cached in a file,
dot ssh slash known hosts.
And while connecting to those, the key is checked
and if it doesn't match, then connection is refused.
Also, you should know
that if you want to connect to a non default port,
you need to tell the SSH client about it.
And something funny is going on here.
The SSH client has lower case p,
followed by the port number.
And if you use SCP, you have upper case p,
followed by the port number.
Which is needed if SSH is listening on a non default port.
Let me show you.
let's start by using SSH.
I want to be user student on 192, 168, 29, 142.
And uh-oh, what do we get?
Well, we get, in fact, exactly what I wanted to see.
A remote host identification has changed.
I know what happened.
I'm using 142 now,
and I messed around with the IP addresses a little bit.
So the first time I connected to 142,
it has stored the SSH host key.
Now another host is using IP address 142.
This other host has an other SSH host key fingerprint.
And the other host is sending the host key fingerprint
to the client.
And the client is saying, "Dude, this is not good.
Somebody might be doing something nasty."
This might be a man-in-the-middle attack.
So if something like this happens,
you should always verify what is happening.
In our case, it's also possible
that the host key has just been changed.
That is exactly the reason why.
And we can see a message
and the message right here is about the offending host key.
Now, the offending host key
is in the fellow users,
Alexander van Vugt dot SSH,
known host colon 84.
And oh, by the way, we are on macOS
because I haven't shown you macOS
SSH connections before.
They are very, very much like Linux.
In fact, you won't even notice the difference
between the mac SSH client and the Linux SSH client.
The thing here is
that I'm getting complaints about line number 84
in the known hosts file.
And this suggests that my known hosts file
has a lot of lines.
So I could remove it,
that's not really what I want to do.
I want to do a sed, minus i, minus e,
84 d on tilde,
slash, dot SSH, slash known host.
You remember this command?
This deletes line 84 from the known hosts file
and now I can try it again.
And oh, we also get an error on line 83.
Well, okay.
Then we are going to do the same for line 83.
That's possible.
And we are going to try it again.
All right.
So after cleaning up this host key information,
we can actually store it.
We can see the host key is known already
by the following other names and addresses.
This is something we have used
in the past, connected to the same host key.
And I know that
because before I was running it on 2022,
now I'm running it on 22.
So I want to continue connecting
and I'm adding my password and there we go.
I'm on the Ubuntu machine right here.
Now I can actually try
and see if we get connection to the CentOS machine.
So from the Ubuntu environment, SSH minus p,
minus p 2022,
to 192, 168, 29, 101.
101?
Yes.
Because in the lesson about networking,
we changed the IP address for the CentOS machine.
And, uh-oh, I am getting a connection refused
on the CentOS machine for port 2022.
That means we need to do some troubleshooting.
Right, troubleshooting, here we go.
First part of the troubleshooting is IP, a.
What is IP, a showing?
29, 101.
So 29, 101.
That seems to be correct.
Second part of troubleshooting, sudo, systemctl,
status, SSHD.
And there we can see,
oh, we can see that that's not good.
Server listening on port 22.
You know what happened?
Well, you probably don't know
but history will tell us.
History is telling us that in command 780,
I applied my change.
But after applying the change
I never restarted SSHD again.
So blind,
I'm pretty convinced that command 779
or restarting the SSH process will fix the problem.
Back to status,
listening on port 2022, looking better.
So let's try it again.
And there we go.
And this is looking so much better.
Are you sure you want to connect?
Continue?
Yes, I'm sure.
Entering the password.
And here we go.
I'm logged in on the CentOS machine on port 2022.
That's all.