Skip to content
Visual explainer 4h to learn 5 steps

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.

Topic 41 of 42

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.

LayersDefence in depth on one server
  1. 1
    Network

    firewalld allows only ssh and http(s).

  2. 2
    Remote access

    Key-only SSH, no root, AllowGroups.

  3. 3
    Accounts

    Password aging, scoped sudo, no unused users.

  4. 4
    Confinement

    SELinux enforcing with correct labels.

  5. 5
    Maintenance

    Automatic security updates, logs reviewed.

CycleHardening is a loop, not a one-off
  1. 1
    Baseline

    Firewall, SSH, sudo, SELinux, updates

  2. 2
    Verify from outside

    Try to break each control

  3. 3
    Audit regularly

    Ports, setuid files, accounts, logs

  4. 4
    Fix drift

    New services, users or rules

  5. 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
FlowA packet reaching web01
  1. 1
    Arrives on ens160

    Interface is in zone public

  2. 2
    Zone rules checked

    Services, ports and rich rules

  3. 3
    Allowed: http, https

    Passed to Apache

  4. 4
    ssh from 10.0.0.0/24

    Accepted by a rich rule

  5. 5
    Anything 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.

CompareExpected results from a second machine
Should succeed
  • Key login as ana
  • curl http://web01/
  • sudo for members of admins
  • nmap shows 22, 80, 443 open
Should fail
  • 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.

  1. 1
    Enforce key-only SSH logins~50m
  2. 2
    Open only needed firewall services~50m
  3. 3
    Keep SELinux enforcing with correct labels~50m
  4. 4
    Enable automatic security updates~50m
  5. 5
    Audit 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

bash
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 refused

Trim 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.

bash
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 rejected

A 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.

bash
#!/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 || true

Quick 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.

Question 1

1You ran firewall-cmd --permanent --add-service=https, but port 443 is still closed. Why?

Question 2

2Which audit result is a red flag?

Question 3

3In /etc/dnf/automatic.conf, what does apply_updates = yes change?

Question 4

4How do you best prove that password SSH logins are disabled?

0/4 answered

Practice exercises

Try each one yourself first. Use the hint if you get stuck, then compare with the solution.

  1. Exercise 1easy

    List every listening port with its program and decide which ones your web server does not need.

  2. Exercise 2medium

    Set up automatic security-only updates on RHEL and confirm when they will next run.

  3. 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.

Go deeper

ShareXLinkedInWhatsApp