Security and hardening · Intermediate · 16 min
Act on the top Lynis suggestions on a new Ubuntu server
By Padding · Updated 18 Nov 2026
Ubuntu 26.04 LTS · Lynis 3.1.6
You will work through the suggestions Lynis 3.1.6 gives a fresh Ubuntu 26.04 server, apply the ones that fit, record the ones that do not in a custom profile, and compare the hardening index before and after.
On this page
A first Lynis audit on a new Ubuntu server ends with a long list of suggestions. Most come from the same handful of tests on every fresh install, and they line up closely with the CIS benchmark for Ubuntu: password ageing, a default umask, a login banner, compilers, core dumps, kernel modules for things a server never uses, kernel settings, and process accounting.
This tutorial takes those suggestions one at a time. For each one you see what Lynis checks, decide whether it fits your server, apply it with a file you can read later, and confirm it. Anything you decide not to fix goes into a Lynis custom profile with the reason written next to it. At the end you run the audit again and compare the two reports.
Treat Lynis as advice, not orders. A suggestion that suits a single-purpose web server can break a build box or a Docker host. Each step says when to skip it.
Step 1 · Take a baseline
Install Lynis from the Ubuntu archive, which carries 3.1.6 (the project's own release is 3.1.7). procps provides sysctl, which Lynis uses to read kernel settings; Ubuntu Server already has it.
sudo apt-get update
sudo apt-get install -y lynis procps
lynis show versionRun the audit and keep a copy of the report, because the next run overwrites /var/log/lynis-report.dat. The working folder ~/lynis-demo is only for this tutorial.
mkdir -p ~/lynis-demo
sudo lynis audit system --quick --no-colors > ~/lynis-demo/audit-before.txt
sudo cp /var/log/lynis-report.dat ~/lynis-demo/before.dat
sudo chown "$(id -un)" ~/lynis-demo/before.dat
grep '^hardening_index=' ~/lynis-demo/before.dat
grep '^suggestion\[\]=' ~/lynis-demo/before.dat | cut -d'|' -f1 | cut -d= -f2 | sort | uniq -cCheck it worked
You see one hardening_index= line with a number from 0 to 100, then a list of test IDs with a count beside each, something like 4 NETW-3200 and 2 AUTH-9286. Write the index down. Each step below names the IDs it deals with; skip a step if none of its IDs is in your list.
Step 2 · Set password ageing and the default umask in login.defs
Two tests read /etc/login.defs. AUTH-9286 wants PASS_MIN_DAYS above 0 and PASS_MAX_DAYS below 99999. AUTH-9328 wants UMASK set to 027 or 077 instead of Ubuntu's 022.
Weigh both before you apply them:
- Password expiry has fallen out of favour. NIST SP 800-63B says services should not force periodic password changes. If your users log in with SSH keys and only use a password for
sudo, a long maximum such as 365 days is a reasonable compromise. If you would rather not expire passwords at all, skip AUTH-9286 in the last step instead. - These values apply to accounts created from now on. Existing accounts keep their settings until you change them with
chage. - A umask of 027 means new files are not readable by other users. If a web server reads files that a deploy user creates, check that the web server's user shares a group with them, or keep 022.
Back up the file first, then set each key. The small function replaces the line if it exists and adds it if it does not:
sudo cp /etc/login.defs /etc/login.defs.pre-lynis
set_def() {
if sudo grep -q "^$1[[:space:]]" /etc/login.defs; then
sudo sed -i "s/^$1[[:space:]].*/$1\t$2/" /etc/login.defs
else
printf '%s\t%s\n' "$1" "$2" | sudo tee -a /etc/login.defs > /dev/null
fi
}
set_def PASS_MAX_DAYS 365
set_def PASS_MIN_DAYS 1
set_def PASS_WARN_AGE 14
set_def UMASK 027Check it worked
grep -E '^(PASS_MAX_DAYS|PASS_MIN_DAYS|PASS_WARN_AGE|UMASK)[[:space:]]' /etc/login.defs
sudo chage -l "$(id -un)"The first command prints four lines with your new values. The second shows the ageing of your own account, which has not changed: new values only reach new accounts.
Step 3 · Add a legal banner
BANN-7126 and BANN-7130 look for a warning in /etc/issue (shown before a console login) and /etc/issue.net (shown before an SSH login once sshd is told to use it). Lynis counts how many words from its list appear, such as "authori", "monitor", "prohibited" and "law", and wants at least five. The banner has no technical effect. Its value is legal: it tells anyone connecting that access is restricted and recorded.
The command below replaces both files, which on Ubuntu hold only the release name. It backs them up first.
sudo cp /etc/issue /etc/issue.pre-lynis
sudo cp /etc/issue.net /etc/issue.net.pre-lynis
printf '%s\n' \
'Authorised access only.' \
'This system is private. Activity on it is monitored and recorded.' \
'Unauthorised use is prohibited and may be reported to law enforcement.' \
| sudo tee /etc/issue /etc/issue.net > /dev/nullKeep backslashes out of /etc/issue: the console treats them as escape codes. If your staff or users read another language, such as French, Swahili or Hausa, add a translated line below the English. Keep the English text, because Lynis only counts its English keywords. Ask whoever handles legal matters for your organisation to approve the wording.
Ubuntu's sshd does not show /etc/issue.net until you point it there. Add a drop-in file, test the configuration, then reload. Keep your second SSH session open in case the reload fails:
printf 'Banner /etc/issue.net\n' | sudo tee /etc/ssh/sshd_config.d/60-banner.conf
sudo sshd -t && sudo systemctl reload sshsshd -t prints nothing when the configuration is valid, and the reload only runs if the test passed.
Check it worked
cat /etc/issue.netYou see the three lines of the banner. Then open a new SSH session from your own machine: the same three lines appear before the password or key prompt.
Step 4 · Deal with compilers
HRDN-7222 fires when a compiler or assembler (as, cc, gcc, clang) is installed and any user can run it. An attacker with a foothold can use it to build tools on the server. Lynis suggests restricting it to root. On a production server the cleaner fix is to not have a compiler at all: build elsewhere and deploy the result.
See which ones you have:
for c in as cc gcc clang; do command -v "$c" || true; doneNo output means none is installed and HRDN-7222 will not be in your list. If paths are printed and nothing on the server builds code (no pip packages compiled on install, no DKMS kernel modules), remove the toolchain. Read the list apt shows before you answer, because removing binutils (which provides as) can take other packages with it.
sudo apt-get purge --autoremove gcc g++ clangSkip this on a build server or a machine that compiles DKMS drivers.
Check it worked
Run the for loop again. It prints nothing, or only the tools you chose to keep.
Step 5 · Turn off core dumps
When a program crashes it can write a core dump: a copy of its memory, which may hold passwords, keys and customer data. KRNL-5820 looks for a hard limit of 0 in /etc/security/limits.conf or a file in /etc/security/limits.d/. A separate file is easier to find and remove later than an edit to the main one.
printf '%s\n' '# No core dumps for login sessions (Lynis KRNL-5820)' '* hard core 0' \
| sudo tee /etc/security/limits.d/60-no-core-dumps.confThese limits apply to login sessions through PAM. Services started by systemd take their limit from the unit (LimitCORE=), and setuid programs are covered by fs.suid_dumpable, which you set in the kernel settings step. Skip this step if you debug crashes on this server.
Check it worked
cat /etc/security/limits.d/60-no-core-dumps.confThe file holds the comment and the * hard core 0 line. Log in again in a new SSH session and run ulimit -Hc; it prints 0.
Step 6 · Block kernel modules the server does not use
Four tests look in /etc/modprobe.d/ for modules that are disabled:
- NETW-3200: uncommon network protocols (
dccp,sctp,rds,tipc). - USB-1000 and STRG-1846: USB storage and FireWire, which a cloud server has no use for.
- FILE-6430: rarely used filesystems (
cramfs,freevxfs,hfs,hfsplus,jffs2,udf). Lynis only raises this when one of them is loaded.
An install <module> /bin/false line makes any attempt to load the module fail. That is what Lynis and the CIS benchmark look for. A plain blacklist line only stops automatic loading.
Weigh it first. Leave out usb-storage on a physical server you recover with a USB drive, and sctp if you run telecom software that speaks it. This file does not touch squashfs, which Lynis also lists, because Ubuntu's snap packages are squashfs images and blocking it breaks them. Run snap list; if it shows any snaps, keep squashfs working.
sudo tee /etc/modprobe.d/60-lynis-unused.conf > /dev/null <<'EOF'
# Modules this server does not use (Lynis NETW-3200, USB-1000, STRG-1846, FILE-6430)
install dccp /bin/false
install sctp /bin/false
install rds /bin/false
install tipc /bin/false
install usb-storage /bin/false
install firewire-core /bin/false
install cramfs /bin/false
install freevxfs /bin/false
install hfs /bin/false
install hfsplus /bin/false
install jffs2 /bin/false
install udf /bin/false
EOFA module that is already loaded stays loaded until a reboot. Rebuild the initramfs so the rule also applies early in boot, then confirm one module is blocked:
sudo update-initramfs -u
modprobe -n -v sctpThe modprobe line prints install /bin/false instead of a path to a .ko file.
Check it worked
grep -c '^install' /etc/modprobe.d/60-lynis-unused.confThe output is 12, one line per module.
Step 7 · Tighten kernel settings with sysctl
KRNL-6000 compares about 50 Linux kernel settings (only those present on the running kernel) with the values in Lynis's profile and raises one suggestion if any differ. Put the ones you accept in a file under /etc/sysctl.d/, so they survive a reboot and you can see in one place what you changed.
Weigh it first:
- Lynis wants
net.ipv4.conf.all.forwardingat 0. Docker, Kubernetes, WireGuard and any router or VPN need it at 1. This file leaves it alone. - Lynis wants
kernel.modules_disabledat 1, which stops all module loading until the next reboot, including modules the server needs later. This file leaves it out, and the last step records why. log_martianslogs packets with impossible source addresses. It is useful evidence but can be noisy behind some cloud load balancers.
sudo tee /etc/sysctl.d/60-lynis-hardening.conf > /dev/null <<'EOF'
# Kernel settings from the Lynis KRNL-6000 profile that fit this server
fs.suid_dumpable = 0
fs.protected_fifos = 2
fs.protected_regular = 2
kernel.kptr_restrict = 2
kernel.dmesg_restrict = 1
kernel.sysrq = 0
dev.tty.ldisc_autoload = 0
net.ipv4.conf.all.accept_redirects = 0
net.ipv4.conf.default.accept_redirects = 0
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.all.log_martians = 1
net.ipv4.conf.default.log_martians = 1
net.ipv6.conf.all.accept_redirects = 0
net.ipv6.conf.default.accept_redirects = 0
EOFA misspelt key makes sysctl report an error and skip that line. Check that every key exists on this kernel before you apply the file:
grep -Ev '^[[:space:]]*(#|$)' /etc/sysctl.d/60-lynis-hardening.conf | cut -d= -f1 | tr -d ' ' | while read -r key; do
[ -e "/proc/sys/$(echo "$key" | tr . /)" ] || echo "unknown key: $key"
doneNo output means every key is known. Now load all sysctl files and read two values back:
sudo sysctl --system
sysctl kernel.kptr_restrict kernel.dmesg_restrictCheck it worked
The last command prints kernel.kptr_restrict = 2 and kernel.dmesg_restrict = 1. The Lynis log lists each key that differed from its profile at the last audit, with the value it expected and the value it found (Expected=..., Real=...); run this again after the final audit:
sudo grep 'different value than expected' /var/log/lynis.log > ~/lynis-demo/sysctl-diff.txt \
&& sed 's/.*sysctl key //' ~/lynis-demo/sysctl-diff.txt || echo "no sysctl differences"KRNL-6000 often stays in the list, because Lynis wants every one of its keys to match. Each remaining line is a decision: change the key, or skip it in the profile as shown in the last step.
Step 8 · Turn on process accounting
ACCT-9622 checks for process accounting: a kernel log of every command that ran, who ran it and for how long. After an incident it helps you work out what an account did. The acct package provides it and the lastcomm and sa tools to read it.
It costs some disk space on a busy server. Check sudo du -h /var/log/account after a week, especially on a small VPS with a few gigabytes of disk.
sudo apt-get install -y acctStart it and make it start at boot, then read the most recent commands:
sudo systemctl enable --now acct
sudo lastcomm | head -n 5Check it worked
sudo ls -l /var/log/account/pacctThe file exists and is owned by root. lastcomm lists commands such as head and sudo with the user who ran them.
Step 9 · Record what you skipped and measure again
Lynis reads /etc/lynis/custom.prf if it exists. A skip-test= line stops a test from running; skip-test=TEST-ID:item skips one part of a test, such as one sysctl key. Write the reason above each line, so the next person (or you, in six months) knows it was a decision and not an oversight.
The examples below fit a cloud VM. BOOT-5122 asks for a GRUB password, which protects against someone at the physical console; on a VM the console sits behind your provider account, so protect that account with two-factor sign-in instead. Replace these with your own decisions. The first command creates the file only if it is missing; the second adds to it.
[ -e /etc/lynis/custom.prf ] && echo "custom.prf exists, the next command adds to it" || sudo install -m 644 /dev/null /etc/lynis/custom.prf
sudo tee -a /etc/lynis/custom.prf > /dev/null <<'EOF'
# BOOT-5122 GRUB password: cloud VM, console access is through the provider account (2FA on).
skip-test=BOOT-5122
# KRNL-6000 kernel.modules_disabled: would block module loading until reboot.
skip-test=KRNL-6000:kernel.modules_disabled
EOFRun the audit again, keep the new report and compare:
sudo lynis audit system --quick --no-colors > ~/lynis-demo/audit-after.txt
sudo cp /var/log/lynis-report.dat ~/lynis-demo/after.dat
sudo chown "$(id -un)" ~/lynis-demo/after.dat
grep -h '^hardening_index=' ~/lynis-demo/before.dat ~/lynis-demo/after.dat
ids() { grep '^suggestion\[\]=' "$1" | cut -d'|' -f1 | cut -d= -f2 | sort -u; }
comm -23 <(ids ~/lynis-demo/before.dat) <(ids ~/lynis-demo/after.dat)The first output is two hardening_index= lines, before and after. The second lists the test IDs that are no longer in the suggestions.
Check it worked
Confirm the file-based fixes from this tutorial all cleared, and that Lynis honoured your profile:
left=0
for id in AUTH-9286 BANN-7126 BANN-7130 USB-1000 STRG-1846 NETW-3200 ACCT-9622; do
if grep -q "^suggestion\[\]=$id|" ~/lynis-demo/after.dat; then echo "still listed: $id"; left=$((left + 1)); fi
done
if grep -q 'umask in /etc/login.defs' ~/lynis-demo/after.dat; then echo "still listed: AUTH-9328 login.defs umask"; left=$((left + 1)); fi
if grep -q '^suggestion\[\]=KRNL-5820|.*limits.conf' ~/lynis-demo/after.dat; then echo "still listed: KRNL-5820 limits.conf"; left=$((left + 1)); fi
echo "$left left"
[ "$left" -eq 0 ]
sudo grep -F 'Atomic test (KRNL-6000:kernel.modules_disabled) skipped by configuration' /var/log/lynis.logThe loop ends with 0 left. Any still listed: line names a step to revisit, or a step you chose to skip. The last command prints a line ending Atomic test (KRNL-6000:kernel.modules_disabled) skipped by configuration (skip-test), which proves Lynis read your profile. On a VM that boots with GRUB, sudo grep -A1 'Skipped test BOOT-5122' /var/log/lynis.log also shows Reason to skip: Skipped by profile setting (skip-test); on a host without a GRUB configuration, such as a container or some VPSs, the reason reads Prerequisites not met instead, because Lynis checks prerequisites first. The hardening index is higher by several points; how many depends on what your server started with. Keep before.dat and after.dat with your server notes, and repeat this comparison after each large change.
Next steps
- Lock down SSH: /tutorials/harden-ssh-on-ubuntu-26-04
- Keep security patches flowing without you: /tutorials/automatic-security-updates-ubuntu
- Go back over the basics of reading a Lynis report: /tutorials/audit-a-server-with-lynis
- Close the ports you do not use: /tutorials/set-up-ufw-firewall-ubuntu-26-04
Related tutorials
- Write custom Fail2ban jails and filters for your own appsYou will have Fail2ban 1.1 banning repeated failed sign-ins to your own app and to an nginx basic-auth area, with filters you tested against sample logs, your own addresses allow-listed, and longer bans for repeat offenders.
- Validate file uploads before you store or serve themYou will take a FastAPI image upload route that trusts everything the browser sends, prove its flaws with pytest, and rebuild it with size limits, type checks from the file's own bytes, Pillow re-encoding, random storage names and safe download headers.
- Use SSH certificates instead of copying keys to every serverYou will run a small SSH certificate authority with ssh-keygen, log in to an Ubuntu 26.04 server with an eight-hour user certificate, give the server a host certificate so clients stop asking about fingerprints, and revoke a certificate the server then refuses.
Need this done for you?
Padding does this work for growing companies, from the first diagnosis to a system that keeps working.
Get new tutorials