Linux server security: what to configure right after installation
The minimum set of measures I apply to every new Linux server: key-only SSH login, a firewall, automatic security updates, brute-force protection and a check of open ports.
Alexei Sergeev3 min read
A server with a public address starts getting scanned within minutes of going online. Bots brute-force SSH passwords and look for open databases and admin panels. Most compromises of small servers are not targeted attacks but the result of a server left with default settings. Below is the basic set I do first, before installing any applications. The examples are for Debian and Ubuntu.
A user and key-based login
Working directly as root is a bad idea: create a user with sudo rights and add your public SSH key to it. Then disable password login and root login. It is convenient to keep these settings in a separate file so that a package update does not overwrite them.
bash
adduser admin
usermod -aG sudo admin
# from your computer:
ssh-copy-id admin@203.0.113.20
/etc/ssh/sshd_config.d/10-hardening.conf
PermitRootLogin no
PasswordAuthentication no
KbdInteractiveAuthentication no
PubkeyAuthentication yes
AllowUsers admin
bash
sudo sshd -t && sudo systemctl reload ssh
Firewall: close everything unnecessary
The principle is simple: incoming connections are denied except those that are really needed. For a web server that is usually SSH, HTTP and HTTPS. The database, Redis and monitoring dashboards should not be reachable from the internet; they are accessed locally or over a VPN.
Vulnerabilities in OpenSSH, the kernel and system libraries are found regularly. Installing security updates by hand once a quarter means running with known holes for months. Debian and Ubuntu have the unattended-upgrades package, which does it automatically.
Only security updates are installed automatically; major system upgrades stay manual. Some updates, such as kernel updates, require a reboot, which is worth planning for a maintenance window.
Brute-force protection
With key-only login, SSH password brute-forcing is not dangerous, but it clutters the logs. Fail2ban blocks addresses that produce failed login attempts. It is also useful for web applications: you can configure it to block password guessing on an admin panel.
The last step is to see which ports are open and compare them with the list of what should be reachable. An unexpected service on the public address is a reason to find out where it came from.
bash
sudo ss -tulpn
It is worth checking from the outside too: scan the server's address from another computer to see it the way bots see it.
What comes next
This is a minimum, not complete protection. The next steps are off-site backups with restore testing, monitoring and outage alerts, separate users and keys for each administrator, and an audit log. On the servers I maintain on an ongoing basis, these items are part of the maintenance schedule and are checked regularly.
Want to know how well your server is protected?
Describe what runs on it and who has access. I will review the settings, close what is unnecessary and document what was done.
Why a copy on the disk next door is not yet a backup, how to apply the 3-2-1 rule in a small company, and why a backup only counts as existing once a restore has been tested. With a restic example.
If you learn about server problems from your employees, you effectively have no monitoring. I cover what a small company should track, how to set up alerts that people do not ignore, and how to choose between Zabbix and…
Why a flat network where every device sees every other one is the main source of trouble in a small office, and how to split it into VLANs on MikroTik with RouterOS 7: the layout, bridge configuration, firewall rules an…
Read →
This site can use Yandex Metrica for anonymised visit statistics. Allow it?
Learn more