Hardening

Server Hardening Checklist for a New Linux Machine

By StrictSecure Editorial Team · · 4 min read

A newly installed Linux server is a blank slate, and that is the best moment to secure it. Default installations favor convenience: they may run services you do not need, allow password logins and leave every port reachable. Automated scanners find freshly exposed machines within hours, so hardening should start before the server does any real work.

Hardening means reducing the ways an attacker can get in and limiting what they can do if they succeed. The checklist below follows a sensible order. The commands assume a Debian or Ubuntu style system, but the principles apply to any distribution.

1. Update everything first

Install pending updates before anything else. Old packages carry known vulnerabilities that attackers know about.

sudo apt update
sudo apt upgrade

Then enable automatic security updates, so the machine does not fall behind when nobody is watching. On Debian and Ubuntu, the unattended-upgrades package handles this. Plan occasional reboots for kernel updates.

2. Create a regular user and stop using root

Logging in as root for daily work means every typo and every compromised session has full power. Create a named user and give it administrative rights through sudo.

sudo adduser alex
sudo usermod -aG sudo alex

Use a strong, unique password for that account, or better, rely on SSH keys as described next. Individual accounts also give you a clear audit trail of who did what.

3. Secure SSH

SSH is usually the main way in, so it gets the most attention.

  1. Use key-based authentication. Generate a key pair on your own computer and copy the public key to the server.
  2. Disable password logins once the key works. This removes the target of password-guessing attacks.
  3. Disable direct root login.
  4. Limit who can connect with an AllowUsers line.

The relevant settings in /etc/ssh/sshd_config look like this:

PermitRootLogin no
PasswordAuthentication no
AllowUsers alex

Test a second connection in a new terminal before closing your current session, so a typo does not lock you out. Changing the default port reduces log noise but is not real security on its own.

4. Configure a firewall

Allow only what the server must serve, and deny everything else. On Ubuntu, ufw provides a simple interface.

sudo ufw default deny incoming
sudo ufw default allow outgoing
sudo ufw allow OpenSSH
sudo ufw allow 443/tcp
sudo ufw enable

Add rules only for services you intentionally run. If a database is used only by an application on the same machine, it should listen on the local address and have no firewall opening at all.

5. Remove or disable unneeded services

Every running service is a possible entry point. List what is listening and ask whether each item is necessary.

sudo ss -tulpn
systemctl list-unit-files --state=enabled

Stop and disable anything you do not recognize or need. Uninstall packages that have no purpose on this machine, such as print services or mail servers on a web host. Fewer components means fewer updates to track and fewer flaws to inherit.

6. Protect against brute-force attempts

Even with password logins disabled, repeated connection attempts fill logs and use resources. A tool such as fail2ban watches log files and temporarily blocks addresses that fail repeatedly. It is a helpful extra layer, but it supports good authentication rather than replacing it.

7. Set sensible file and kernel protections

  • Review permissions on sensitive files such as /etc/shadow and private keys. Keys should be readable only by their owner.
  • Mount temporary directories with restrictive options where it fits your workload.
  • Consider a mandatory access control system, such as AppArmor or SELinux, which most distributions already include. Keep it enabled rather than switching it off when it blocks something. Read the logs and adjust the policy.
  • Apply network hardening settings with sysctl, such as ignoring ICMP redirects, if your environment calls for it.

8. Turn on logging and monitoring

You cannot respond to what you cannot see. Make sure system logs are kept and rotated, and decide who reads them.

  • Check authentication logs for failed and unusual logins.
  • Send logs to a separate machine if possible, so an intruder cannot erase their tracks on the same server.
  • Use a file integrity tool or audit rules for the most sensitive directories.
  • Set up alerts for events that matter, such as a new user being created.

9. Plan backups and recovery

Hardening reduces risk; it does not remove it. Take regular backups of data and configuration, store a copy away from the server, and test a restore at least once. A backup that has never been restored is only a hope.

10. Document and review

Write down what you changed and why. Keep the configuration in version control or a configuration management tool, so you can rebuild the server the same way. Then schedule regular reviews: check for pending updates, inspect user accounts, and rerun the listening-ports check.

Quick summary list

  • Update and enable automatic security patches.
  • Use a non-root user with sudo.
  • SSH keys only, no root login.
  • Firewall with default deny.
  • Remove unnecessary services.
  • Brute-force protection.
  • Mandatory access control enabled.
  • Logs kept and reviewed.
  • Backups tested.

Frequently Asked Questions

Is changing the SSH port worth doing?

It reduces the volume of automated login attempts in your logs, which makes real problems easier to spot. It does not stop a determined attacker, because port scanning finds the new port easily. Treat it as a minor convenience, and rely on key authentication and a firewall for actual protection.

Do I need both a firewall and fail2ban?

They do different jobs. The firewall decides which ports are reachable at all, while fail2ban reacts to repeated failures on services you have deliberately exposed. Most servers benefit from both, though the firewall is the more important of the two.

How often should I repeat the hardening review?

Check updates and logs continuously through automation, and do a full review of users, open ports and configuration every few months. Also repeat the review after any major change, such as installing new software or giving the server a new role.

Related reading