Blog

SSH Explained Simply: How You Control a Server From Anywhere

What the Fuck is SSH?

Alright, we all have heard that bullshit about SSH, which says "SSH is a secure shell protocol used to connect…" and so much nonsense. Yes, that's right. SSH is a secure shell protocol standard, but why do we need it? We all know that SSH is really secure; you should use it. So let's cut the bull shit and dive straight into it with a simple explanation.

It's just a protocol that makes connections between two systems much secure( Oh shit, I am getting back to those old bullshit). Let's just use a simple example to see what that is. Let's say Edward wants to communicate with John, whose house is 2km away from Edward's, and Edward needs to send his message.

The main story

Alright, let's bring this simple example much closer to the computer world. There is a pipe between Edward's house and John's. Edward needs to write his message and send that message through the pipe. Back in those days, Edward would send that message easily using that pipe, and that pipe was transparent and weak, which means everyone who could find the pipe could also break that pipe and see Edward's message that contained "Oh John, Help me, my girlfriend is pregnant. What the hell should I do?" Now everyone would know that Edward's girl is pregnant, and that's not good. If someone could find the pipe, they could also grab Edward's message and send their own message to John, like "Hey John, FUCK YOU!" and that's not good either.

But Edward somehow figured out that the whole pregnancy story was made up by her girlfriend to blackmail him. So he broke up with her, and when his brain found peace, he came up with an idea that make sure no one can access his messages through the pipe. He created a machine that could draw a specific shape in a specific way. He also created a tool that can identify that specific shape (like Stencil) and can be used publicly. With that stencil, you can figure out if the shape is drawn by the machine or not, but you can not create the machine using that tool (I mean in the computer world, cause they are algorithms, not a physical thing). He kept the machine private, and no one could have that, but anyone could have the tool. So he gave John the tool, and after that, no one could understand their message, BUT HOW?

First, Edward sends a message to John: "Hello John, we need to talk". John doesn't trust the message sender, so he sends back some random words (a challenge) to Edward. Then Edward signs each word in specific places that, if somebody looks at it, they think it's just a scribble. Then Edward sends the signed message to John. John receives the message. Because John knows the random words that he sent to Edward, he knows where to look for those signs, he checks those signs with the tool that Edward gave him, and figures out that the message is valid and the signs were drawn by Edward's machine.

Now that they have validated the message, they are sure that they are talking to each other. They have a secret algorithm that, when they make sure they are talking to each other, they're gonna use it. So after authentication, they start to communicate in the gibberish they made for themselves.

This video can help:

That's how SSH works. First of all, you create your private machine and your stencil. Actually, in the computer world, they have names:

  • Private machine = Private key

  • Public stencil = Public key

Yeah! You have to generate those keys and pass the Public key to your server. You must not share the Private key with anyone. Keep the Private key to yourself, as its name shouts out.

All good, so how to generate the keys? Simple, type these in your command line:

ssh-keygen

You may be prompted to set a passphrase for your key. If you set that passphrase every time you try to use your SSH, you will have to enter that passphrase, but if you don't, you won't, and that's not a big deal. You can just press Enter to pass.

if you activate the ssh-agent you won't have to enter your passphrase every time.

I'll tell you how to do that.
After entering the ssh-keygen A directory is getting created in ~/.ssh If you do ls -al ~/.ssh You will see the list of files.

There will be a file like this: id_ed25519.pub Or if you chose another encryption type, like RSA, you would see the id_rsa.pub . Every file that has pub extension is your public key.

So to give your public key to your server, you have to type this command:

ssh-copy-id <user>@<ip_of_your_server>

When you connect to your server for the first time, SSH says:

I've nerver seen this server before. here its fingerprint do you trust it?

You simply type yes, and the fingerprint gets saved in this directory: ~/.ssh/known_hosts .

Now it's time to connect to your server:

ssh <user>@<ip_of_your_server>

You may be asked for your SSH passphrase and server password. Enter them, and you are ready to go.

But if you want to prevent entering your passphrase 50 times, you may activate ssh-agent:

eval "$(ssh-agent -s)"
Why use eval? Doesn't "ssh-agent -s" gonna work? Simple answer: No, that's just gonna return some shell command that those commands need to be run. This is why we activate the ssh-agen like that. But maybe I will blog about it in the future.

Then you need to add your private key to the ssh-agent using this command:

ssh-add ~/.ssh/id_ed25519

Or if you used RSA

ssh-add ~/.ssh/id_rsa

What does this ssh-agent do?:

  • Holds your private keys in memory

  • Signs things on your behalf

  • Prevents you from typing your passphrase 50 times a day