Server hardening
A server on the internet gets visitors you did not invite: within minutes of going online, automated scripts start trying passwords on SSH and probing open ports. The ITR server hardening script takes care of the basics for you, in one run, before you install the ITR. It is a calm and safe start for every new server.
The script is in the folder server-hardening of the ITR-Docker repository. It was developed for Ubuntu Server 26.04 LTS. On any other version it stops, unless you explicitly allow it (see Settings).
Please note: this is a basic hardening. It gives a new server a solid starting point, but it does not cover everything a server on the internet may need, such as monitoring, intrusion detection, regular security updates or the security rules of your own organisation. You remain responsible for your own server. Please check for yourself whether this hardening is sufficient for you or your organisation, and add to it where needed. The scripts are provided as is, without any warranty, as stated in the licence of ITR-Docker and in the general disclaimer of this wiki.
What the script does
harden-ubuntu.sh runs these steps in order:
- Checks. It checks your settings and the Ubuntu version. It stops if ufw is active, if netfilter-persistent is enabled (see Existing servers), or if Docker is installed as a snap.
- Updates. It updates the system (
apt-get updateandapt-get upgrade). - Docker. It installs Docker from Docker's own repository, the same way prepareITR.sh does. If Docker is already installed this way, the step is skipped.
- Admin account. It creates the admin account from your settings, adds it to the sudo group and installs your SSH public key for it. You choose the password when the script asks for it; it is only used for sudo on the server, never to log in over SSH. The password is never stored in a file.
- SSH. It moves SSH to your port, switches off root login over SSH, and allows login with an SSH key only: passwords are refused for every account. It writes these settings in its own file, /etc/ssh/sshd_config.d/00-itr-hardening.conf, so the original sshd_config stays untouched. It checks the settings SSH will really use, for the admin account and for root, and stops if another setting would override them. It then checks that SSH really listens on the new port. If not, it stops before the firewall is switched on, so you cannot lock yourself out.
- fail2ban. It installs fail2ban, which blocks IP addresses that keep trying wrong passwords on SSH. It watches the new SSH port.
- Firewall. It installs itr-firewall and a service that starts it at every boot. PostgreSQL stays closed; itr-postgres-access opens it briefly when you need it. See The firewall.
- History. It empties root's shell history.
Run it once on a fresh server, before prepareITR.sh.
Hardening a new server
You need a fresh Ubuntu Server 26.04 LTS, an account with sudo rights and an SSH key pair.
- On your own computer, create an SSH key pair if you do not have one yet. Keep the private key on your computer; you only need the public key (the .pub file) on the server:
ssh-keygen -t ed25519
- Log in to your server.
- Download the ITR-Docker repository and open the hardening folder:
git clone https://github.com/Quopt/ITR-Docker.git ~/ITR-Docker cd ~/ITR-Docker/server-hardening
- Make your own settings file and fill it in. Set at least ADMIN_USER and ADMIN_SSH_PUBLIC_KEY (paste the contents of your .pub file):
cp harden-ubuntu.conf.example harden-ubuntu.conf nano harden-ubuntu.conf
- Run the script. It asks for the password of the admin account on the way:
sudo ./harden-ubuntu.sh harden-ubuntu.conf
- Keep this session open. Open a second terminal and log in through the new port with the admin account and your key. Check that sudo works:
ssh -p <your SSH port> <your admin account>@<your server> sudo -v
- Check that a password login is refused. The answer must be Permission denied (publickey):
ssh -o PubkeyAuthentication=no -o PreferredAuthentications=password -p <your SSH port> <your admin account>@<your server>
- Only when that works: restart the server with sudo reboot and log in once more.
- Set the network firewall of your hosting provider. At OVH this is the Network Firewall of the server's IP address in the control panel. Allow the same ports: your SSH port, 80 and 443. Allow the PostgreSQL port only while you need it, for your own IP address (see Temporary access to PostgreSQL).
Now continue with the installation of the ITR.
harden-ubuntu.conf is ignored by git, because it may hold your own IP addresses. Passwords never go in it.
The question in prepareITR.sh
prepareITR.sh helps you to remember this step. When you run it in a terminal on a server that has not been hardened yet, it asks:
Do you want to harden this server first? [y/n]
- Answer y and the installation stops before anything is changed. The script shows the steps above. Harden the server, log in with the new admin account and run prepareITR.sh again.
- Answer n and the installation continues as before.
The script knows that a server has been hardened when /etc/itr/firewall.conf exists; then it does not ask. When prepareITR.sh does not run in an interactive terminal, for example from another script, it does not ask either and simply continues.
Settings
All settings are in harden-ubuntu.conf. The example file harden-ubuntu.conf.example explains each one as well.
| Setting | Default | What it does |
|---|---|---|
SSH_PORT |
2243 |
The new SSH port. A port other than 22 keeps away most automated login attempts. |
ADMIN_USER |
(empty, required) | The admin account with sudo rights. Root cannot log in over SSH any more, so this is the account you log in with. Please choose a name that is not easy to guess. |
ADMIN_SSH_PUBLIC_KEY |
(empty, required) | The SSH public key of the admin account: the key itself (ssh-ed25519 AAAA… name) or the path to a .pub file on the server. The script checks that it is a valid key.
|
DISABLE_SSH_PASSWORD_AUTH |
yes |
yes: SSH accepts key login only. Password and keyboard-interactive login are refused for every account (PasswordAuthentication no, KbdInteractiveAuthentication no, AuthenticationMethods publickey). no keeps password login possible; we do not recommend it.
|
PUBLIC_TCP_PORTS |
"80 443" |
Ports that everyone may reach: the website through nginx (or HAProxy). |
ALLOW_PING |
yes |
Answer ping over IPv4. ICMPv6 is always allowed, because IPv6 does not work without it. |
EXT_IFS |
auto |
The network interfaces that face the internet. auto takes the interfaces of the default routes.
|
INSTALL_DOCKER |
yes |
Install Docker from Docker's own repository. |
RUN_DOCKER_HELLO_WORLD |
yes |
Run Docker's hello-world container as a quick check. |
CLEAR_ROOT_HISTORY |
yes |
Empty /root/.bash_history at the end. |
ALLOW_UNTESTED_OS |
no |
yes lets the script run on a version other than Ubuntu 26.04 LTS, at your own risk.
|
The firewall
The firewall is a small script, /usr/local/sbin/itr-firewall. Its settings are in /etc/itr/firewall.conf, which the hardening script writes from your settings. The service itr-firewall applies the rules at every boot and again whenever Docker restarts. The rules are rebuilt from scratch every time, so applying them twice does no harm.
It protects two kinds of traffic, for IPv4 and IPv6 alike:
- Traffic to the server itself (chain ITR-INPUT)
- Allowed: local traffic on the server, replies to connections that already exist, ping, the SSH port and the public ports. Everything else is dropped.
- Traffic to the Docker containers (chain DOCKER-USER)
- Docker sends traffic for its published ports past the normal firewall rules, so a rule for the server alone does not protect a container. itr-firewall therefore also fills Docker's own chain for user rules, DOCKER-USER. From the internet only the public ports reach a container. Every other published port, PostgreSQL included, can only be reached from the server itself.
- Temporary PostgreSQL access (chain ITR-PG)
- Empty after every run of itr-firewall. Only itr-postgres-access adds rules here (see below).
itr-firewall never changes Docker's own rules.
Useful commands:
sudo itr-firewall # apply /etc/itr/firewall.conf again after you changed it sudo itr-firewall status # show the ITR rules
Temporary access to PostgreSQL
The firewall never opens PostgreSQL to the outside. That is on purpose: the database holds all candidate data, and the ITR itself reaches it over the internal Docker network. Now and then you may want to work on the database directly, for example to load a backup with your own tools. For that moment there is a separate script, itr-postgres-access, which opens the PostgreSQL port for one IP address only, and only for a while:
sudo itr-postgres-access open <your IP address>
It closes again by itself after 60 minutes. You can choose another time (1 to 1440 minutes) and other ports. Instance n uses port 25432 + n:
sudo itr-postgres-access open <your IP address> 30 25433
Please close it as soon as you are done:
sudo itr-postgres-access close sudo itr-postgres-access status # show what is open and when it closes
Good to know:
- The access is never saved. A restart of the server or of Docker, and every run of itr-firewall, closes it as well.
- A new open restarts the countdown, and close closes all openings at once.
- Opening to the whole internet (
0.0.0.0/0) is refused. - Your hosting provider's network firewall must allow the port too. Close it there as well when you are done.
Several instances behind HAProxy
When you run several ITR instances on one server, HAProxy runs on the server itself and passes traffic to the nginx containers (see Several instances on one server). PUBLIC_TCP_PORTS="80 443" stays the same. The ports of the instances themselves (81, 82, 444, 445, …) can then only be reached from the server, through HAProxy. If you let HAProxy pass PostgreSQL traffic as well, open the port HAProxy listens on for it with itr-postgres-access. haproxy/HAProxy_iptables.sh is not needed on a hardened server, because local traffic is already allowed.
Existing servers
The script is meant for a fresh server. It stops when netfilter-persistent is enabled, because that service restores saved firewall rules at boot, next to the new ones. This is the case on servers that were hardened by hand with iptables-persistent. To move such a server over:
- Make sure you have console access, for example the KVM or rescue console of your hosting provider.
- Make a copy of /etc/iptables/rules.v4 (and rules.v6 if it exists).
- Switch the service off with sudo systemctl disable netfilter-persistent and restart the server.
- Run the hardening script.
Good to know
- Docker as a snap. The Ubuntu installer offers Docker as a snap. The ITR needs Docker from Docker's own repository: the snap is confined and cannot reach /data/ITR-data, and two Docker installations get in each other's way. The hardening script stops when it finds the snap. Remove it with sudo snap remove --purge docker (this also removes its containers and images) and run the script again.
- A new Ubuntu version. Docker must publish a repository for a new Ubuntu version before it can be installed; the script checks this. If Docker ever uses its nftables firewall instead of iptables, there is no DOCKER-USER chain. itr-firewall then reports that it skipped the container rules, and the containers are not protected by it.
- fail2ban reads the system journal, because not every Ubuntu installation has /var/log/auth.log.
- Shell history. The script empties the saved history of root. The shell you are working in keeps its own history in memory; type history -c before you log out if you want that gone too.
Where to go next
- Install on linux – install the ITR on your hardened server
- Reading guide for system administrators – the wiki in a sensible order for running the ITR
- Deployment – how the containers work together
- Server settings and tools – finishing the installation inside the ITR
