The ten most effective ways to secure a Linux server are: keep it updated automatically, use a sudo user instead of root, allow SSH with keys only, turn on a firewall, block brute-force attempts with Fail2ban, remove services you don’t need, apply least privilege to users and files, keep AppArmor or SELinux on, watch the logs, and keep tested backups. None of them needs special tools, and together they stop the large majority of attacks a server on the internet will see.

A new server gets automated login attempts within minutes of going online. Most attacks aren’t aimed at you personally; bots scan the whole internet for default passwords, open databases, and unpatched software. This guide explains each practice, why it matters, and the commands to apply it. Commands are for Ubuntu and Debian, with Fedora and RHEL equivalents where they differ. Test changes on a non-production server first, and keep a second SSH session open whenever you change access settings.

1. Keep the System Updated Automatically

Most break-ins use known vulnerabilities that already have a fix. Updating is the single most valuable habit.

Update now:

sudo apt update && sudo apt upgrade     # Fedora/RHEL: sudo dnf upgrade

Then turn on automatic security updates. On Debian and Ubuntu:

sudo apt install unattended-upgrades
sudo dpkg-reconfigure --priority=low unattended-upgrades

On Fedora and RHEL, install dnf-automatic, set apply_updates = yes in /etc/dnf/automatic.conf, and enable its timer:

sudo dnf install dnf-automatic
sudo systemctl enable --now dnf-automatic.timer

Kernel updates only take effect after a reboot. On Ubuntu and Debian, the file /var/run/reboot-required exists when one is pending; schedule a reboot during a quiet hour.

2. Use a Regular User with sudo, Not Root

Root can do anything, so a typo or a stolen root password affects the whole system. Create your own user, give it admin rights, and use sudo for admin tasks. sudo logs every command, so you can see who did what.

sudo adduser jane
sudo usermod -aG sudo jane      # Fedora/RHEL: the group is wheel

Give every admin their own account instead of sharing one, and remove accounts when people leave. The details are in Linux User and Permission Management: Users, Groups, and Files.

3. Lock Down SSH

SSH is the front door, and bots try passwords on it all day. Use keys, turn off password login and root login, and limit who can connect. In /etc/ssh/sshd_config.d/00-hardening.conf:

PasswordAuthentication no
KbdInteractiveAuthentication no
PermitRootLogin no
AllowUsers jane
MaxAuthTries 3

Test and apply, keeping your current session open:

sudo sshd -t
sudo systemctl reload ssh       # Fedora/RHEL: sshd

The full walkthrough, including key setup, why the file name matters, and fixes when you get locked out, is in How to Configure SSH on Linux: Secure Remote Server Access.

4. Turn On a Firewall

A firewall blocks every port you haven’t explicitly opened, so a service you forgot about isn’t exposed to the internet. On Ubuntu with UFW:

sudo ufw default deny incoming
sudo ufw default allow outgoing
sudo ufw allow OpenSSH
sudo ufw allow 'Nginx Full'     # only if this is a web server
sudo ufw enable
sudo ufw status verbose

Allow SSH before running ufw enable, or you’ll cut off your own connection. On Fedora and RHEL, firewalld is on by default; manage it with firewall-cmd.

Databases, admin panels, and caches (MySQL on 3306, PostgreSQL on 5432, Redis on 6379) should never be open to the whole internet. Bind them to 127.0.0.1, or allow only specific addresses. If your cloud provider offers a firewall in front of the server, use that too.

5. Block Brute-Force Attacks with Fail2ban

Fail2ban watches the logs and temporarily bans addresses that fail to log in too many times:

sudo apt install fail2ban

Create /etc/fail2ban/jail.local:

[DEFAULT]
bantime = 1h
findtime = 10m
maxretry = 5

[sshd]
enabled = true
backend = systemd
sudo systemctl enable --now fail2ban
sudo fail2ban-client status sshd

Fail2ban has filters for Nginx, Apache, mail servers, and many applications, so you can protect login pages beyond SSH.

6. Remove Software and Services You Don’t Need

Every running service is something an attacker can try. List what’s listening on the network:

sudo ss -tulpn

Each line should be a service you know and need. List what starts at boot:

systemctl list-unit-files --type=service --state=enabled

Stop and disable what you don’t use, and remove packages you installed for a test and forgot:

sudo systemctl disable --now cups      # example: printing on a server
sudo apt purge <package>
sudo apt autoremove

Fewer packages also means fewer updates and less to monitor.

7. Apply Least Privilege to Users, Files, and Services

Give every person and program only the access it needs:

  • Grant sudo only to people who administer the server; use limited sudo rules for single tasks.
  • Run each application as its own unprivileged system user, never as root.
  • Keep secrets (.env files, private keys, database passwords) at 600 and owned by the user that needs them.
  • Never use chmod 777.
  • Check for unexpected admin users and setuid programs now and then:
getent group sudo wheel
sudo find / -xdev -type f -perm -4000 2>/dev/null

The same principle drives Zero Trust security: trust nothing by default, and grant the minimum.

8. Keep AppArmor or SELinux Turned On

These are mandatory access control systems: even if an attacker takes over a program, its profile limits which files and ports it can touch. Ubuntu and Debian use AppArmor; Fedora and RHEL use SELinux.

Check that they’re on:

sudo aa-status          # AppArmor: lists loaded profiles
getenforce              # SELinux: should print Enforcing

When SELinux blocks something legitimate, the common advice online is to turn it off. Don’t. Find the cause in the audit log (sudo ausearch -m avc -ts recent) and allow that one thing, usually with a boolean (setsebool) or a file label (semanage fcontext).

9. Monitor Logs and Audit the Server

You can’t respond to what you don’t see. Check these regularly:

sudo journalctl -p err -b                    # errors since boot
sudo journalctl -u ssh --since today         # SSH logins and failures (sshd on Fedora/RHEL)
sudo journalctl _COMM=sudo --since "7 days ago"   # who used sudo
last -n 20                                   # recent logins

Run a security audit with Lynis, a free tool that checks hundreds of settings and suggests fixes:

sudo apt install lynis
sudo lynis audit system

Start with the warnings at the end of its report. For CPU, memory, disk, and network monitoring (an unexpected spike can be a sign of a compromise, such as a cryptominer), see Linux Server Monitoring: Check CPU, Memory, Disk, and Network. An uptime monitor such as Uptime Kuma tells you when a service goes down.

10. Keep Tested Backups

Backups turn ransomware, a deleted database, or a failed disk from a disaster into an inconvenience. Follow the 3-2-1 rule: three copies of your data, on two different types of storage, with one copy off the server (and ideally one that the server can’t delete or change).

  • Back up data and configuration: /etc, /var/www, /home, and database dumps (mysqldump, pg_dump) rather than raw database files.
  • Use a tool such as restic, borg, or rsync, or your provider’s snapshots, on a schedule.
  • Test restores. A backup you’ve never restored is a guess.

For how ransomware attacks work and how backups fit into defense, see What Is Ransomware?

Bonus: More Ways to Harden a Server

  • Encrypt traffic to every web service with HTTPS; Certbot makes it free and automatic. See How to Install and Configure Nginx on a Linux Server.
  • Turn on two-factor authentication for your hosting and cloud account, which can bypass every server setting. See Two-Factor Authentication (2FA) Explained.
  • Keep applications updated too (WordPress and its plugins, Node.js packages, Docker images), not only the operating system.
  • Use strong, unique passwords for databases and app accounts, stored in a password manager.
  • Document the server: what runs on it, which ports are open, and how to restore it.

Linux Server Security Checklist

#PracticeQuick check
1Automatic security updatessystemctl status unattended-upgrades
2Sudo user, no root loginsgetent group sudo
3SSH keys only, no rootsudo sshd -T | grep -Ei 'passwordauth|permitroot'
4Firewall onsudo ufw status verbose
5Fail2ban runningsudo fail2ban-client status sshd
6Only needed servicessudo ss -tulpn
7Least privilegesudo find / -xdev -type f -perm -0002
8AppArmor/SELinux onsudo aa-status / getenforce
9Logs reviewed, audit runsudo lynis audit system
10Backups testedRestore a file this month

Conclusion

Linux server security isn’t one product or setting; it’s a set of habits that each close a common way in. Patch automatically, use personal accounts with sudo, lock SSH to keys, firewall everything you don’t need, let Fail2ban handle the bots, keep AppArmor or SELinux on, read your logs, and keep backups you’ve actually restored. Start with the first four today, since they block most automated attacks, and work through the rest this week. For the server basics these practices build on, see Linux Server: A Beginner’s Guide to Server Administration.

Frequently Asked Questions

Is Linux secure by default?

Linux is a solid foundation, but a default install isn’t hardened. Password SSH login, open ports, and missing updates are common on new servers. The practices above close those gaps.

Do Linux servers need antivirus?

Usually not in the traditional sense. Updates, a firewall, least privilege, and monitoring matter more. A scanner such as ClamAV is useful on servers that store or pass on files to Windows users, such as mail and file servers.

How often should I update a Linux server?

Apply security updates automatically every day, and review other updates weekly. Reboot for kernel updates during a planned maintenance window.

What is the first thing to do on a new Linux server?

Update it, create a sudo user, set up SSH keys and turn off password and root login, and turn on the firewall. Those four steps block most automated attacks.

Does changing the SSH port make a server secure?

It reduces automated noise in the logs, but it isn’t real protection, since attackers can scan every port. Key-only login and a firewall do the real work.

Want to keep learning? These related guides pair well with this post: