SSH is a communication protocol used for establishing secure, encrypted connection tunnels between hosts. It is a very common tool for sysadmins, IT workers, developers and any person that needs to maintain access to a server from afar.
SSH is used to connect and administer remote servers as explained earlier.
The common commands are ssh (to connect and establish a remote shell session), scp (to securely copy files over an SSH tunnel),
sftp (to transfer files interactively), rsync (to sync files, kind of smart copying).
The basic common SSH command usage forms are:
** note that a command is considered to be on the same line until the comment hash appears **
Most linux distributions have the SSH client and server services installed by default.
If, for some reason, the distribution doesn't include the SSH packages (maybe in some minimal installations),
use the following commands to install the packages on Debian/Ubuntu:
For security reasons, hardening your SSH configuration is a must-have skill for any sysadmin.
The most basic configuration changes to be done are: change the default connection port (default is 22, and many bots are spam connecting to try and bruteforce machines),
Setting login attempts limitations to avoid bruteforce (for regular password login),
setting up private/public key authentication (preferrable over password auth) and creating known connection blocks.
It is important to understand there are two different configuration files at /etc/ssh, one for the SSH client, and one for the SSH server.
The SSH client includes config for your machine trying to connect to a remote host (config file is at /etc/ssh/ssh_config).
The SSH server includes config for your machine accepting connections from other hosts to itself (config file is at /etc/ssh/sshd_config).
We will describe these configuration steps one by one.
Changing the default connection port:
Password based authentication is frowned upon (since it is prone to bruteforce attacks and requires special system hardening as we've just seen), The better and more comfortable approach to SSH into remote hosts is by establishing a connection via shared keys. The connecting machine shares a private key it holds to "unlock" a public key the server holds. This is never done over an insecure channel, and is done only once a secure communication channel was created. This way, no entity without the exact key value that unlocks this public key can login to the server. This rids the user from bruteforce attacks, while at the same time makes SSH more comfortable to use, since the connection is faster, without repeated prompts asking for password credentials. Note, that if by some manner, your private key file is compromised a malicious actor can now SSH as if it is you! However, there is an optional fail-safe mechanism in place to avoid it, by entering a passphrase which is used to unlock the passed key value on login attempt. Additional measures to maximally secure your remote server from malicious unauthorized login is to also establish IP filtering rules (since then, the malicious actor needs to use your machine, which reduces risk), as well as setup hardware authentication options (e.g ubikeys). This guide will not dive into hardware authentication and IP filtering, but will finish with instructions on how to setup key-based authentication.
Connection blocks are configuration we add to the connecting side
configuration file (/etc/ssh/ssh_config).
They are useful for making the connection command less verbose and tiring to type.
For example, without a defined and applied connection block we need to type:
HOST remote_host_name HostName remote_host_IP User remote_host_username Port port_num IdentityFile ~/.ssh/id_remotehost