To harden a Linux server, start from a minimal install, apply a CIS Benchmark Level 1 baseline, disable root SSH login, require SSH keys, set a default-deny firewall, patch promptly, use named accounts with sudo, send logs centrally, and verify with a baseline scan you repeat regularly.
Key takeaways
- Audit with a CIS Benchmark scan before you change SSH, PAM, or the firewall.
- Disable root SSH login, require keys, and limit logins by user or group.
- Hardening is a cycle: patch, log, re-scan, and document exceptions.
A default Linux install is built to work, not to resist attack. Hardening is the work of removing what you do not need, locking down what remains, and proving the result with a scan you can repeat.
What does it mean to harden a Linux server?
Hardening a Linux server means reducing its attack surface and applying a documented, repeatable baseline so only the services, users, and network access you need are present. You start from a minimal install, then lock down accounts, SSH, the firewall, and logging.
NIST SP 800-123, the Guide to General Server Security, frames this as a lifecycle: plan the server role, install and configure it securely, then maintain that configuration through patching, logging, and periodic reassessment. CIS Benchmarks give you the concrete settings for your exact distribution and version.
What should you do before you change anything?
Audit first, change second. Run a baseline scan, save the result, and make a backup and a rollback plan before you touch SSH, PAM, or the firewall. Those three areas cause most hardening lockouts.
- Pick a baseline: CIS Benchmark Level 1 Server for your exact distribution and version. Level 2 is stricter and can break workloads.
- Audit without changing: on Ubuntu use the Ubuntu Security Guide, on RHEL use OpenSCAP, or run Lynis for a general review.
- Keep console access: make sure you can reach the server outside SSH before you change SSH settings.
- Test on one server: never roll a hardening change across a fleet on a Friday afternoon.
Sort scan failures into three piles: safe to apply, needs testing against your workload, and will not apply, with a documented exception. That list becomes your hardening plan.
How do you harden a Linux server step by step?
Work in this order: minimal install, updates, accounts, SSH, firewall, filesystem, logging, and then verification. Each step below is safe to test on one server first, and each one can be rolled back.
1. Install only what the role needs
Choose a server variant, not a desktop install, and remove unused services, sample applications, and compilers on production systems. Review enabled services with systemctl and disable anything the role does not need.
A minimal install is not about disk space. Every extra service is another thing that can go wrong, another patch to track, and another port an attacker can probe.
2. Patch the system and set a patch routine
Update the system before it serves traffic, then set a routine: security updates promptly, full updates on a schedule, and a reboot plan for kernel updates. Enable automatic security updates where your change process allows it, and test them first on a staging server.
Record what you patched and when. Auditors and incident responders both need that history, and it proves your routine works.
3. Lock down accounts and sudo
Remove or disable unused local accounts and any default credentials shipped with pre-installed software. Give each administrator a named account, use sudo for privilege, and review sudo rights regularly.
Never share a root password, and never let several people log in as the same user. If you cannot tell who did what, you cannot investigate an incident.
4. Harden SSH
SSH is the front door to most Linux servers, so lock it first. Disable direct root login, require key-based authentication, and limit who can log in by user or group.
PermitRootLogin noin /etc/ssh/sshd_config, then test withsshd -tbefore you reload.PasswordAuthentication noonce every admin has a working SSH key.AllowUsersorAllowGroupsto list only the accounts that need SSH.- Consider changing the port only as noise reduction, never as your main defense.
Confirm a non-root admin can log in before you change the policy, and keep a console session open while you test. If you lock yourself out, the console is your way back in.
5. Set a default-deny firewall
Allow only the ports your role needs, and deny everything else by default. On Ubuntu that is usually UFW, on RHEL it is firewalld, and on many servers it is nftables or iptables underneath.
Open port 22 only to the addresses that need it, if you can. Open web ports only on servers that serve the web. Check the result from outside with a port scan of your own server, and compare it with the list you intended.
6. Protect the filesystem and kernel
Set safe permissions on sensitive files such as /etc/passwd and /etc/shadow, and mount temporary filesystems with noexec, nodev, and nosuid where your workload allows it. Be careful with /tmp: noexec there can break installers and container tools, so test it first.
Apply kernel settings through sysctl, including disabling IP forwarding unless the host routes traffic, and enabling SYN cookies. Document every setting you change, so the next admin knows why it is there.
7. Turn on logging and monitoring
Enable auditd for important events, send logs to a central server, and watch for failed logins, sudo use, and changes to key files. Logs that stay only on the server are lost when the server is compromised.
Set up alerts for repeated SSH failures, new sudo users, and unexpected services. A log you never read is not a control, so check alerts weekly and after every change.
8. Verify with a scan and repeat
Run your baseline scanner again and compare the result with the first scan. Fix the remaining failures, document the exceptions, and schedule the next audit. Hardening drifts as software is installed and configs change.
Use a second tool as a cross-check, such as Lynis, and test a restore from backup before you call the server production-ready. A server you cannot restore is not a hardened server, it is a fragile one.
Which hardening controls matter most?
The controls below are the ones that stop the most common attacks. If you only have an afternoon, do the first four rows first, then work down the table.
| Control | What it stops | Risk if you skip it | Lockout risk |
|---|---|---|---|
| SSH keys, no root login | Password guessing | High | High if you test badly |
| Patching | Known exploits | High | Low |
| Default-deny firewall | Exposed services | High | Medium |
| Named accounts and sudo | Shared credentials | Medium | Low |
| Central logging | Missed intrusions | Medium | Low |
| Filesystem mounts | Malware execution | Medium | Medium |
CISA's Cross-Sector Cybersecurity Performance Goals put account security first for the same reason: weak or default credentials remain one of the most common ways servers are compromised. Pair this guide with our Wi-Fi security guide if the server sits on a home or small-office network.
What are the most common hardening mistakes?
The most common mistake is changing SSH, PAM, or the firewall without a rollback plan, then losing access. The second is hardening once and never scanning again.
- Changing the SSH port and calling it done: port changes reduce noise, they do not stop an attacker who scans all ports.
- Disabling the firewall to fix an app: fix the rule, do not turn the firewall off.
- Copying a script from the internet: a script built for another distribution can break your services. Use your distribution's CIS Benchmark instead.
- Forgetting backups: hardening without a tested backup leaves you with a secure server you cannot restore.
Document every exception. An exception you wrote down is a decision; one you forgot is a hole. See our small business cybersecurity checklist for the wider controls around a server. NIST also documents OpenSCAP scanning against CIS and STIG profiles in its hardening guidance, and the DamonSec hardening guide explains which controls cause lockouts.
How often should you re-harden a Linux server?
Re-audit after every major change, and on a schedule even when nothing changed. Monthly is a good default for a production server, with a full baseline scan at least quarterly.
Patch security updates as they arrive, review users and sudo rights monthly, and re-run your CIS or OpenSCAP scan after upgrades. If a scan shows drift, fix it the same week, not the same quarter.
The bottom line
Start with a CIS Level 1 baseline, audit before you change anything, and lock down SSH, accounts, patching, and the firewall first. Those controls stop most real attacks on Linux servers.
Then keep the cycle going: log centrally, scan again, and document exceptions. A hardened server is a maintained server, not a server you hardened once. Check your public services with our security headers checker when the server goes live.
Frequently asked questions
What is the first step to harden a Linux server?
Audit first. Pick a CIS Benchmark Level 1 baseline for your exact distribution and version, run a scan, save the result, and make a backup before you change anything.
Should I disable root login over SSH?
Yes, in most cases. Disable direct root login with PermitRootLogin no, give admins named accounts, and use sudo for privilege. Confirm a non-root admin can log in before you change the setting.
What is a CIS Benchmark?
A CIS Benchmark is a set of concrete, testable security settings published by the Center for Internet Security for a specific operating system and version. Level 1 is practical with limited impact, while Level 2 is stricter.
Is changing the SSH port enough to secure a server?
No. Changing the port reduces automated scanning noise, but an attacker who scans all ports will still find SSH. Use SSH keys, disable root login, and limit logins by user as your main defenses.
How often should I patch a Linux server?
Apply security updates promptly and full updates on a schedule. Test updates on a staging server first where you can, and have a reboot plan for kernel updates.
What firewall should I use on Linux?
Use the firewall your distribution supports, such as UFW on Ubuntu or firewalld on RHEL. Set a default-deny policy and open only the ports your server role needs.
Do I need to harden a server in the cloud?
Yes. Cloud servers are reachable from the internet, often within minutes of creation. Apply the same baseline, limit SSH by source address where you can, and use your cloud provider's security groups as well.
What is OpenSCAP?
OpenSCAP is a free tool that scans a Linux system against a security profile, such as a CIS Benchmark or a DISA STIG, and reports which settings pass or fail. It can also remediate some findings.
Sources
The engineers and analysts who build Cysoro's domain scanner, email-authentication checks and free security tools. We test what we write about every day — SPF, DKIM, DMARC, certificates, look-alike domains and phishing — and check each guide against the relevant standards, government guidance and vendor documentation.
Checked against NIST SP 800-123, CIS Benchmark Level 1 guidance, and CISA performance goals.
How we write and fact-check →