Skip to content
vjrj edited this page Oct 7, 2021 · 14 revisions

Introduction

ssh is a basic tool that allows us to connect to a server safely, transfer files and many other things. For example, ansible uses it to connect to our servers, and thus configure them as we indicate in our inventories of services and servers and according to the tasks indicated in ala-install.

Basic Concepts

SSH keys

ssh uses a system of public and private cryptographic keys. Making a simplistic comparison, think of a key (private key) as the key to your house, and one lock (the public key) or more locks for match that key that you can put in different places and thus open them with the same key.

You can have different public and private keys for different uses (work, home, car, bike, etc).

You can share the public key in several places (the ideal thing is to have the same lock on all the doors of your house or your car). But the private key, you only share it with your family members (or with members of your team), although it is advisable that each one use their own different keys and locks to enter the same sites.

That is to say, a door can have several locks, and the door can be opened with different keys and here our metaphor gets a bit mixed up, but let's hope it is understood. Imagine that you have a chain and you add several locks chained (see the image). If any neighbor opens their lock with their key, the chain will open. © Linnaea Mallette, Licence: CC0 Public Domain

The same happens with ssh. You can configure several public keys in a server and account (that is several chained locks) and any one that have one (private) key of that locks can enter to the server.

If the lock is not positioned properly, you will not be able to open that door chain (and access that server).

Password access

Yes, you can use a password to access to a server without using SSH keys, like Abracadabra to open a magic door, but SSH keys are a more secure option and we prefer to use them.

SSH Key Generation

Just typing:

ssh-keygen

will ask for some name and password and it will generate a key pair for your with the default values.

You can also specify more options for that key:

ssh-keygen -q -t rsa -b 2048 -f .ssh/my-key

Adding your public key to some user/server to authorized

You should authorized that public key in your server adding it to /home/your-user/.ssh/authorized_keys

This is like to put the new lock in your chain.

To do that you can :

  1. Use ssh-cp-id https://explainshell.com/explain?cmd=ssh-copy-id+-i+key.pub to copy the public key to your user/server (recommended):
ssh-copy-id -i .ssh/my-key jane@1.2.3.4

(lets say that your user is jane and 1.2.3.4 is the IP of your server. 2) edit manually .ssh/authorized_keys accessing by ssh to your user/server:

ssh jane@1.2.3.4
nano .ssh/authorized_keys2 
# add there the contents or .ssh/my-key.pub and save the file

so the next time you access to your server you can use that key instead of the user/server password:

ssh -i .ssh/my-key jane@1.2.3.4

Public and Private IP Addresses

Not all of our servers are directly connected to the Internet. Our data centers are like an office building in which not all offices have a door to the street, to the outside.

Our servers have public and/or private IP addresses. Using also a metaphor, given a building, we cannot put a postal address with a street number to each office in the building, therefore, a street number is shared, and then the floor/door is used, for example, to indicate an office inside the building.

Gateways

This also affects security. Imagine what a mess if each office gave onto the street, outside. For this reason, many times, it is accessed through a portal, or a security control that then gives you access to the rest of the building. Sometimes you have to go through several security checks until you reach the office you want to enter.

In ssh terminology this is a bastion, gateway or proxyjump. You "jump" to one server to be able to access another typically, an internal server.

SSH Ports

Like the classic web http port is 80, or https 443, the default port for SSH, is 22. If on a server you ring the bell 80 the web will answer you, if you touch 443 the secure web will answer you, and if you ring the bell 22, then ssh answers.

Sometimes another port is used instead of the default port 22. This happens many times when we have internal servers and the external machine uses port 22 for itself. In these cases a different port (for example 22001) is redirected to the internal machine port 22.

Basic ssh commands

The basic one:

ssh jane@1.2.3.4

or specifying the key to use:

ssh -i .ssh/my-key jane@1.2.3.4

as this is more a more long a complicate to remember we can edit the .ssh/config file to set up this connection for future reuse, adding:

Host my_server
  HostName 1.2.3.4
  IdentityFile ~/.ssh/my-key
  User jane

With this configuration now we can just type:

ssh my_server

to connect to that IP address with the user jane and using that ssh key.

SSH using gateways

You can access to an internal server using a bastion/gateway/proxyjump like:

ssh -J gateway.l-a.site jane@1.2.3.4

But as this command is a bit difficult to memorize and also it can be longer if you have to specify keys, etcetera, it's recommended to use the .ssh/config file instead to do the same:

Host my_server
  HostName 1.2.3.4
  IdentityFile ~/.ssh/my-key
  ProxyJump bastion.l-a.site
  User jane

scp, sftp

TODO

SSH Clients

The remote server you want to access runs a SSH server, but to connect to it, you need a SSH client. There are different versions but as we use openssh in the server side, it's recommend to use openssh in the client side too.

Clone this wiki locally