Harden and verify the server
Lock the server down and verify each control from a second machine.
By Invicly Tech, , in Linux Administration (RHCSA-style). About 10 minutes to read.
In short
Hardening means reducing what an attacker can reach and what they can do once inside. Layer controls: a tight firewall, key-only SSH for a limited group, least-privilege sudo, SELinux enforcing, automatic security updates and regular audits. Verify each control from another machine rather than assuming it works.
Explained simply
Your server works. Now you make it hard to attack. Hardening has two goals: give attackers as few doors as possible, and limit what they can do if they get through one.
No single setting does it all, so you stack several: a firewall that only lets in web and SSH traffic, SSH that accepts keys only, sudo rights that are as small as possible, SELinux to fence in each service, and automatic security updates.
The most important part is checking, not assuming. A rule you think is active may never have been saved. So for every control, you try to break it from a second machine: attempt a password login, scan the ports, ask for a page that should be blocked.
Hardening is not done once. New software, new users and new holes appear all the time, so you repeat the audit regularly and fix what drifted.
Press Play on a diagram to walk through it one step at a time.
- 1Network
firewalld allows only ssh and http(s).
- 2Remote access
Key-only SSH, no root, AllowGroups.
- 3Accounts
Password aging, scoped sudo, no unused users.
- 4Confinement
SELinux enforcing with correct labels.
- 5Maintenance
Automatic security updates, logs reviewed.
- 1Baseline
Firewall, SSH, sudo, SELinux, updates
- 2Verify from outside
Try to break each control
- 3Audit regularly
Ports, setuid files, accounts, logs
- 4Fix drift
New services, users or rules
- Back to step 1
In depth
Firewall: allow only what is needed
firewalld groups rules into zones. Each network interface belongs to one zone, usually public. firewall-cmd --list-all shows what that zone allows. On a fresh RHEL install, public allows ssh, dhcpv6-client and cockpit. A web server needs only ssh, http and https, so remove the others with --remove-service.
Remember the two-layer rule: changes without --permanent are lost at the next reload or reboot, and --permanent changes are not active until firewall-cmd --reload. To allow SSH only from an admin network, use a rich rule that accepts ssh from that source and remove the general ssh service, or put the admin network in its own zone with firewall-cmd --zone=trusted --add-source.
- firewall-cmd --get-active-zones shows which interface is in which zone
- firewall-cmd --permanent --remove-service=cockpit
- firewall-cmd --list-all --permanent shows what will survive a reload
- 1Arrives on ens160
Interface is in zone public
- 2Zone rules checked
Services, ports and rich rules
- 3Allowed: http, https
Passed to Apache
- 4ssh from 10.0.0.0/24
Accepted by a rich rule
- 5Anything else
Rejected by default
Shrink the attack surface
Every listening program is a door. sudo ss -tulpn lists all TCP and UDP sockets with the program behind each. For each one, ask: is it needed, and does it need to listen on all addresses? A database used only locally should listen on 127.0.0.1. Services you do not need, such as cups or avahi-daemon on a server, should be disabled with systemctl disable --now and ideally removed.
Also check accounts. awk -F: '$3 == 0' /etc/passwd should print only root. Look for accounts with real shells that nobody uses, and lock them. Review /etc/sudoers and every file in /etc/sudoers.d: broad rules like ALL=(ALL) NOPASSWD: ALL for a service account are a common way attackers become root.
Automatic security updates
Most real break-ins use known holes that already had a fix. Automatic updates close that gap. On RHEL, install dnf-automatic and edit /etc/dnf/automatic.conf: upgrade_type = security limits it to security fixes, and apply_updates = yes installs them instead of only downloading. Then enable dnf-automatic.timer. The dnf-automatic-install.timer variant always applies updates regardless of that setting.
On Ubuntu, the unattended-upgrades package does the same, configured in /etc/apt/apt.conf.d/50unattended-upgrades, and unattended-upgrade --dry-run --debug shows what it would do. Kernel and core library updates need a reboot to take effect; on RHEL, needs-restarting -r from dnf-utils tells you whether one is due. Plan regular reboot windows.
- systemctl list-timers dnf-automatic*
- dnf updateinfo list --security shows pending security fixes
- needs-restarting -r: exit code 1 means reboot needed
Audit with a repeatable checklist
Run the same checks every time, ideally from a script, and compare with the last result. Listening ports: ss -tulpn. Setuid and setgid programs, which run with their owner's power: find / -xdev \( -perm -4000 -o -perm -2000 \) -type f. World-writable files outside temp folders: find / -xdev -type f -perm -0002. Failed logins: journalctl -u sshd | grep 'Failed', and lastb if it is enabled. SELinux: getenforce must say Enforcing, and ausearch -m AVC shows recent denials.
A new setuid file, a new listening port or a new UID 0 account that you did not create is a red flag. Save each audit output with the date, so the diff between two runs tells you what changed.
Verify from the outside
Checks on the server tell you what should happen; tests from another machine prove it. From a second host, scan the ports with nmap -Pn -p- web01; only 22, 80 and 443 should show as open. Try a password login with ssh -o PubkeyAuthentication=no -o PreferredAuthentications=password ana@web01; it must say Permission denied (publickey). Try ssh root@web01; it must fail even with a valid key.
Test from an address outside the admin network too, if SSH is restricted. Finally, keep evidence: save the scan and test output with the date. That is your proof for an auditor, and your baseline for the next review.
- Key login as ana
- curl http://web01/
- sudo for members of admins
- nmap shows 22, 80, 443 open
- Password login
- ssh root@web01
- Port 3306 from outside
- SSH from a non-admin network
Learn it in 5 short steps
About 50m each. Track them with a timer in your plan.
- 1Enforce key-only SSH logins~50m
- 2Open only needed firewall services~50m
- 3Keep SELinux enforcing with correct labels~50m
- 4Enable automatic security updates~50m
- 5Audit with ss, find and logs~50m
Key points
- Remove or disable services and packages you do not need.
- dnf-automatic or unattended-upgrades applies security updates on a schedule.
- Audit regularly: listening ports, SUID files, sudoers and failed logins.
- Test controls from outside: try a password login and a closed port and confirm they fail.
Key terms
Tap a card to flip it. 0/4 known
Examples
sudo ss -tulpn # what is listening?
sudo systemctl disable --now cups avahi-daemon 2>/dev/null
sudo dnf install -y dnf-automatic
sudo sed -i 's/^apply_updates.*/apply_updates = yes/' /etc/dnf/automatic.conf
sudo systemctl enable --now dnf-automatic.timer
getenforce # must be Enforcing
sudo find / -perm -4000 -type f 2>/dev/null
ssh -o PubkeyAuthentication=no ana@web01 # should be refusedTrim services, automate updates and audit.
Restrict SSH to the admin network
The web server should accept web traffic from anyone but SSH only from 10.0.0.0/24. Step 1: add a permanent rich rule accepting ssh from that network. Step 2: remove the general ssh service and the unneeded defaults. Step 3: reload and review the permanent config. Do this from a session that comes from 10.0.0.0/24, or from the console, so you do not cut yourself off. Step 4: test from an admin host and an outside host.
sudo firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="10.0.0.0/24" service name="ssh" accept'
sudo firewall-cmd --permanent --remove-service=ssh
sudo firewall-cmd --permanent --remove-service=cockpit --remove-service=dhcpv6-client
sudo firewall-cmd --permanent --add-service=http --add-service=https
sudo firewall-cmd --reload
sudo firewall-cmd --list-all
# from 10.0.0.5: ssh ana@web01 works; from elsewhere: connection rejectedA dated audit you can compare
Step 1: write a short audit script that collects ports, setuid files, UID 0 accounts, sudo rules and the SELinux mode into one dated file. Step 2: run it today and keep the file. Step 3: next month, run it again and diff the two files. Anything new in the diff, such as a new setuid binary or port, needs an explanation.
#!/usr/bin/env bash
set -euo pipefail
out=/root/audit/$(date +%F).txt; mkdir -p /root/audit
{
echo '## ports'; ss -tulpn | awk 'NR>1 {print $1, $5, $7}' | sort
echo '## setuid'; find / -xdev -perm -4000 -type f 2>/dev/null | sort
echo '## uid0'; awk -F: '$3 == 0 {print $1}' /etc/passwd
echo '## sudo'; grep -rhv '^#' /etc/sudoers /etc/sudoers.d | grep -v '^$'
echo '## selinux'; getenforce
} > "$out"
ls -1 /root/audit | tail -2 | sed 's|^|/root/audit/|' | xargs diff || trueQuick reference
- firewall-cmd --list-all and --list-all --permanent: compare runtime and saved rules
- --permanent changes need firewall-cmd --reload to take effect
- ss -tulpn lists every listening socket and its program
- find / -xdev -perm -4000 -type f lists setuid programs
- awk -F: '$3 == 0' /etc/passwd should print only root
- dnf-automatic: upgrade_type = security, apply_updates = yes, enable dnf-automatic.timer
- needs-restarting -r tells you if a reboot is due after updates
- Test from outside: nmap -Pn -p- host and a forced password SSH login
Quick quiz
Pick an answer to see if you're right and why.
Practice exercises
Try each one yourself first. Use the hint if you get stuck, then compare with the solution.
- Exercise 1easy
List every listening port with its program and decide which ones your web server does not need.
- Exercise 2medium
Set up automatic security-only updates on RHEL and confirm when they will next run.
- Exercise 3hard
From a second machine, produce evidence that only ports 22, 80 and 443 are open, that root and password logins fail, and save it to a dated file.
Check yourself
Answer in your head, reveal, then be honest with yourself.
Feel confident about this topic?
Frequently asked questions
- What is the most important hardening step?
- Keeping the system updated and allowing only key-based SSH. Most attacks use known, already-fixed holes or weak passwords.
- Is a firewall needed if only a few services listen?
- Yes. It is a second layer: if someone later starts a service by mistake, the firewall still blocks it from the network.
- Should automatic updates also reboot the server?
- Many teams let updates install automatically but reboot in a planned window. needs-restarting -r on RHEL tells you when a reboot is needed.
- How often should I audit a server?
- Run a short automated audit at least weekly and review it, and do a full review after major changes. Comparing results over time shows drift quickly.