SSH for Beginners - AtlasOfLivingAustralia/documentation Wiki
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
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.
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).
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
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 it
You should authorized that public key in your server adding it to
This is like to put the new lock in your chain.
To do that you can:
ssh-cp-id(more details) to copy the public key to your user/server (recommended)
ssh-copy-id -i .ssh/my-key [email protected](lets say that your user is
188.8.131.52is the IP of your server.
- or edit manually
.ssh/authorized_keysaccessing by ssh to your user/server (more risky if you don't know well what you are doing):
ssh [email protected] 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 [email protected]
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.
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.
ssh terminology this is a bastion, gateway or
proxyjump. You "jump" to one server to be able to access another typically, an internal server.
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
Basic ssh commands
The basic one:
or specifying the key to use:
ssh -i .ssh/my-key [email protected]
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 184.108.40.206 IdentityFile ~/.ssh/my-key User jane
With this configuration now we can just type:
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 [email protected]
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 220.127.116.11 IdentityFile ~/.ssh/my-key ProxyJump bastion.l-a.site User jane
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.
- Mac or GNU/Linux has openssh out of the box in their terminals.
- Windows 10 instructions: https://docs.microsoft.com/es-es/windows-server/administration/openssh/openssh_install_firstuse
- Older Windows versions can use emulators like cygwin or msys2 that includes the openssh package respectively.