How to SSH

Cyber basics #3

April 20, 2023 · 4 mins read

SSH

SSH is an abbreviation for secure shell. It’s a protocol you can use to securely and easily connect to a remote machine.

Connect to a remote machine using SSH

For example, you have a backup server with the ip 192.168.178.97 and want to remotely log into it as user john. Just type:

    ssh john@192.168.178.97

You get asked for the password and boom your are logged in. Not more, not less. That’s cool isn’t it?

image ssh example

By default ssh is running on port 22. In case the remote machine listens on a different one you can specify the port using the -p option.

Authentication using a ssh-key

Instead of typing a password to log into a remote machine you can use a ssh-key which increases security and makes your life a lot easier. You need to generate a ssh key pair once. The private key stays on your local machine while the public key is placed on the remote machine. When you type the above ssh command to establish the connection you won’t get asked for a password. Instead the machine gets ahh you are the guy and boom you are logged in without anything else to do. It gets that by creating a challenge which only you as the owner of the private key can solve. But this is all done in the background, you don’t need to do anything.

Generating the ssh-key pair

To generate a ssh-key pair simply type:

    ssh-keygen

ssh-keygen is a tool within OpenSSH wich is installed on linux and macOS systems by default. When executing ssh-keygen a RSA public-private-key pair is generated. At first you get asked for a name of the key files. By default the keys are stored in ~/.ssh/id_rsa (private key) and ~/.ssh/id_rsa.pub (public key).
If you have multiple key pairs on your machine you should specify another name. If you want you can also specify a passphrase for that key. The private key must always stay on your local machine.

image ssh-keygen example

In the next step we copy the public key to our remote server.

NOTE: Never share your private key or copy the private key onto a remote machine! Only the public key is meant to be shared and copied!

Copy the public key to the remote machine

All the public keys for a specific user on the remote machine are managed in the “authorized_keys“-file within the .ssh directory located in the users home directory.
Instead of copying the public key manually use the tool ssh-copy-id:

    ssh-copy-id -i id_rsa.pub john@192.168.178.97

which copies the public key to the remote machine over ssh.
Using the -i parameter you can specify the name of the public key you want to copy. Without the -i the default key “id_rsa.pub” is used. Well, in my example with my key named id_rsa.pub the -i wouldn’t be necessary but anyway.

image ssh-copy-id example

That’s it

Yeah! Now you can easily log into your remote machine by typing the already mentioned command ssh user@ip-adress without typing your password.

Have fun!

In case of problems

In case it does not work somehow get some more information about whats going on by adding -vvv to your ssh command:

    ssh -vvv john@192.168.178.97

If you have multiple keys on your local machine you can try to edit/create the file “config” inside your local .ssh-directory. Add the lines:

    Host 192.168.178.97
    IdentityFile ~/.ssh/id_rsa

with your corresponding ip and name of your key. Note, here you have to take the name of the private key (the one without the .pub at the end).

Hope that helps.

Further information