Sub topic 3
A safe hardening workflow: baseline, snapshot, rollback
Hardening changes can lock you out of a server, stop a service, or stop the system from booting. A workflow built on three steps keeps those changes reversible: record a baseline, take a snapshot, and prepare a rollback before you change anything.
This chapter assumes you can use the Linux command line, systemd, and sudo on a test server or virtual machine. It also assumes you know what a CIS profile is from the earlier chapters.
Why hardening needs a workflow
Most CIS recommendations restrict something. A restriction that is correct in general can break a particular workload. Four failures come up often:
- A firewall policy of
dropwith no SSH rule ends your remote session. noexecon/tmpbreaks installers and some package scripts.- Disabling a kernel module removes a feature an application relied on.
- A malformed SSH drop-in file stops
sshdfrom starting after a restart.
Working in small, verified steps limits the damage. You always know which change caused a problem, and you can undo it.
The three-part loop
Each pass through the loop covers one group of related changes, such as SSH settings or sysctl parameters. Do not apply a whole benchmark at once.
Step 1: Record a baseline
A baseline tells you how the system looked before you touched it. You use it for two things. It shows which failures your changes fixed. It also gives you a reference when something stops working.
Capture these items:
- A compliance scan with the profile you chose.
- The list of listening ports and running services.
- The effective SSH configuration.
- The active firewall rules.
- Installed packages and the kernel version.
# Create a dated baseline directory
BASE=/root/hardening/baseline-$(date +%Y%m%d)
sudo mkdir -p "$BASE"
# Compliance scan before any change
sudo oscap xccdf eval \
--profile xccdf_org.ssgproject.content_profile_cis_server_l1 \
--results "$BASE/results.xml" \
--report "$BASE/report.html" \
/usr/share/xml/scap/ssg/content/ssg-rhel9-ds.xml
# System state that hardening is likely to change
sudo ss -tulpn | sudo tee "$BASE/listening.txt" > /dev/null
sudo systemctl list-units --type=service --state=running \
| sudo tee "$BASE/services.txt" > /dev/null
sudo sshd -T | sudo tee "$BASE/sshd-effective.txt" > /dev/null
sudo nft list ruleset | sudo tee "$BASE/nft-ruleset.txt" > /dev/null
sudo sysctl -a 2>/dev/null | sudo tee "$BASE/sysctl.txt" > /dev/null
rpm -qa | sort | sudo tee "$BASE/packages.txt" > /dev/null
uname -r | sudo tee "$BASE/kernel.txt" > /dev/nulloscap exits with code 2 when some rules fail. That is normal for a baseline and is not an error in the scan itself.
Also write down what the server is for. List the services it must provide, the ports clients use, and who connects to it. You need this list later to decide whether a failing recommendation is a real problem or a conflict with the workload.
Step 2: Snapshot and back up
A snapshot lets you return the whole system to a known state. A file backup lets you restore single files quickly. Use both when you can.
| Method | Restores | Speed | Typical use |
|---|---|---|---|
| Hypervisor or cloud snapshot | Whole disk and sometimes memory | Fast | Virtual machines and cloud instances |
| LVM snapshot | A logical volume | Fast, but needs free space in the volume group | Physical hosts and virtual machines with LVM |
| Filesystem snapshot (Btrfs, XFS with external tools) | A filesystem | Fast | Systems already using these filesystems |
| Copy of configuration files | Only the files you copied | Instant | Every change, as a minimum |
A snapshot of a running database may not be consistent. Quiesce the application or use its own backup method first.
For configuration files, a timestamped copy is the minimum:
# Back up the files that the next change group will touch
STAMP=$(date +%Y%m%d-%H%M%S)
sudo mkdir -p /root/hardening/backup-$STAMP
sudo cp -a /etc/ssh /etc/fstab /etc/sysctl.d /etc/modprobe.d \
/etc/nftables.conf /etc/audit /root/hardening/backup-$STAMP/The -a option preserves ownership, permissions, and timestamps. A restored file with the wrong owner or mode can cause a new failure.
An LVM snapshot of the root volume looks like this. The names vg0 and root are examples. Check yours with lvs.
sudo lvcreate --size 5G --snapshot --name root_pre_hardening /dev/vg0/root
sudo lvsRemove the snapshot when you finish. A snapshot slows writes to the origin volume and becomes invalid if it fills up.
Step 3: Prepare the rollback
A rollback plan should exist before you apply a change, because a broken system may not let you work out a plan afterward. For each change group, write down three things:
- The command that undoes the change.
- How you will reach the host if network access fails.
- How you will know the change worked.
Keep a way back in
- Open a second SSH session before you change SSH or firewall settings. Keep it open until a new connection succeeds.
- Know your console access: a hypervisor console, cloud serial console, or out-of-band management card. SSH-based recovery does not help when SSH is the problem.
- For firewall changes on a remote host, schedule an automatic revert.
# Schedule a revert in 10 minutes in case the new rules lock you out
echo 'cp /root/hardening/nftables.conf.good /etc/nftables.conf && systemctl restart nftables' \
| sudo at now + 10 minutes
# Check syntax, then load the new rules
sudo nft -c -f /etc/nftables.conf && sudo systemctl restart nftables
# After you confirm access still works, cancel the revert
sudo atq
sudo atrm JOB_NUMBERReplace JOB_NUMBER with the number shown by atq. The at command requires the atd service to be running.
Validate before you apply
Many services can check their configuration without applying it. Use these checks every time:
| Component | Validation command |
|---|---|
| SSH | `sshd -t` |
| nftables | `nft -c -f /etc/nftables.conf` |
| sudoers | `visudo -c` |
| sysctl | `sysctl --system` and read the output for errors |
| auditd rules | `augenrules --check` |
| fstab | `findmnt --verify` |
The fstab check matters because a bad entry can drop the system into emergency mode at the next boot. Run findmnt --verify before every reboot after you edit it.
Verify and rescan
After each group, check that the system still does its job. Then rescan.
- Confirm the services in your inventory are still running:
systemctl --failedandss -tulpn. - Test the real workload from a client, such as a web request, a database connection, or a login.
- Read the journal for new errors:
journalctl -p err -b. - Run the scan again and compare it to the baseline.
sudo oscap xccdf eval \
--profile xccdf_org.ssgproject.content_profile_cis_server_l1 \
--results /root/hardening/after-ssh.xml \
/usr/share/xml/scap/ssg/content/ssg-rhel9-ds.xmlKeep a short change log with the date, the group applied, the rules affected, and the scan result. This log feeds the exception records you write in the last chapter of this tutorial.
Rolling back
Choose the smallest rollback that fixes the problem.
- Revert the single file from your backup, then reload the service.
- Remove the drop-in file you added. Drop-in directories make this easy, since deleting
/etc/ssh/sshd_config.d/60-cis.confremoves only your change. - Restore from the snapshot if the system will not boot or several changes interact. With LVM, merge the snapshot with
lvconvert --merge, which completes at the next activation of the volume. For a root volume, that means a reboot.
After a rollback, find the cause before you try again. A rollback without a diagnosis only repeats the failure.
Common mistakes
- Making many changes in one pass, so a failure has no clear cause.
- Skipping the baseline, so you cannot tell what your changes did.
- Taking a snapshot but never testing that you can restore from it.
- Closing the working SSH session before testing a new one.
- Leaving old snapshots in place, where they slow the disk and eventually fill.
Exercise
You are about to apply a new nftables ruleset with a default-drop input policy to a remote server. List the steps that protect you from a lockout, in order.
Show answer
1) Back up the current working ruleset and /etc/nftables.conf. 2) Open a second SSH session and keep it open. 3) Schedule an automatic revert with at that restores the good file and restarts nftables. 4) Check the new file with nft -c -f. 5) Load it. 6) Open a new SSH connection to confirm access, and test the services. 7) Cancel the scheduled revert with atrm. If step 6 fails, the revert restores access on its own, or you use the console.
Exercise
Why do you scan again after each group of changes instead of once at the end?
Show answer
A scan after each group shows which rules that group fixed and lets you tie any new failure or broken service to a single change. A single scan at the end cannot tell you which change caused a problem, so rollback becomes harder.