🔐 Lock the Door: Hardening Linux Against AI-Assisted Attacks
security guide / hardening basics for any distribution
Lock the Door: hardening any Linux server against AI-assisted attacks Tested 2026-11-26 on Ubuntu 24.04.5 LTS and Rocky Linux 9 defence only: no attack steps on this page AI didn't invent new ways in. It makes the old ones fast, cheap and tireless: scanning, phishing, turning new bugs into attacks within hours, and guessing passwords the way people make them. This page is the seven basics that blunt all of that, on any distribution, with the real commands and real output from our lab. It is also the written companion to "Lock the Door", our crossover special with the Ghost Circuit: Shanghai anime, where Mo, Yao, Wen and Old Gu harden the clinic's little server at three in the morning. And the honest part, up front: there is no 100% method.
What AI changes, and what it doesn't
AI didn't invent new doors. It tries the old ones faster, and with fewer mistakes: Scanning: the whole internet can be swept for open services in hours, and the results sorted for the weakest-looking targets. Phishing: emails with perfect spelling and the right tone, in any language, written for one person at a time. New bugs: when a vulnerability is announced, a working attack can follow in hours instead of weeks. The time you have to patch is short. Passwords: guesses shaped like the passwords people really choose: a season, a year, a symbol. Summer2026! is a guess, not a secret. What it is still trying, on almost every server: old, unpatched software weak or reused passwords services nobody needed, left listening mistakes in the config (root logins, passwords on SSH, no firewall) So the defence is the same basics, done faster and done everywhere. Nothing on this page is new. That is the point: the basics are what an automated attacker is counting on you to skip.
There is no 100% method
No lock stops everyone. A good one does two things: it makes them choose another door, and it tells you who tried. Be a harder door than the next one. most automated attacks move on when the easy checks fail Notice fast. logs, file checks and audits, read by a person Recover fast. backups you have actually restored, and a plan for the bad night Our lab box went from a Lynis hardening index of 64 to 69 after all seven steps below (Rocky: 65 to 67). Not a hundred. It never will be, and that is fine.
Before you start
Only harden systems you own, or are authorised to manage. If it isn't your server, ask first. Keep a way back in that doesn't depend on SSH: a console, an out-of-band card, a cloud provider's serial console. Test every change on a copy first: a spare box, a virtual machine, a container built like the real thing. The lab for this page, run on 2026-11-26: clinic Ubuntu 24.04.5 LTS nginx on 80/443, vsftpd on 21, SSH with passwords and root login allowed, no firewall clinic-rocky Rocky Linux 9 the same starting state laptop the admin's own machine where the SSH key is made noise our own test client makes failed key-less logins as admin, clinic, test, backup, wen, oracle, support Both servers start deliberately soft, the way plenty of real ones are. Each step below shows the commands and the real output.
Step 1: know what you run
You can't lock a door you don't know you have.
sudo ss -tlnp # everything listening for TCP connections, and which program owns it (add -u for UDP)
systemctl list-units --type=service --state=running --no-pager
Lab, Ubuntu (trimmed):
LISTEN 0 511 0.0.0.0:443 users:(("nginx",...))
LISTEN 0 4096 0.0.0.0:22 users:(("sshd",...))
LISTEN 0 511 0.0.0.0:80 users:(("nginx",...))
LISTEN 0 32 *:21 users:(("vsftpd",...))
vsftpd.service loaded active running vsftpd FTP server
Port 21 is FTP, and nobody needed it. Disable it: it stops now, and stays off after a reboot.
sudo systemctl disable --now vsftpd
sudo ss -tln | grep ':21 ' || echo 'port 21: closed'
port 21: closed
Rocky Linux: the same commands. Read the list line by line and ask of each one: do we need this? Everything you switch off is one less door.
Step 2: patch fast, automatically if possible
The most important step. When a new bug can become an attack in hours, patching by hand once a month is not fast enough.
Automatic, if possible. Some places can't, and that's fair: a patch can break the one application that pays the bills. Then the rule is:
Security updates: automatic where you can.
Everything else: tested in a lab first (a spare box, a VM, a copy of the real thing), then rolled out quickly.
Never: no updates at all.
Ubuntu and Debian: unattended-upgrades
sudo apt-get install -y unattended-upgrades
sudo dpkg-reconfigure -plow unattended-upgrades # answer Yes
cat /etc/apt/apt.conf.d/20auto-upgrades
APT::Periodic::Update-Package-Lists "1";
APT::Periodic::Unattended-Upgrade "1";
systemctl list-timers apt-daily-upgrade.timer --no-pager # lab: next run 06:04 the next morning
By default it installs from the security pocket only; /etc/apt/apt.conf.d/50unattended-upgrades says which origins.
Rocky, Alma, RHEL and Fedora: dnf-automatic
sudo dnf install -y dnf-automatic
grep -E '^(upgrade_type|apply_updates)' /etc/dnf/automatic.conf
upgrade_type = default
apply_updates = no # the default only downloads
sudo sed -i 's/^upgrade_type.*/upgrade_type = security/; s/^apply_updates.*/apply_updates = yes/' /etc/dnf/automatic.conf
sudo systemctl enable --now dnf-automatic.timer
systemctl list-timers dnf-automatic.timer --no-pager
(Lab: Rocky 9 with dnf-automatic 4.14. On other versions, check man dnf-automatic for the file and key names.)
Some fixes need a reboot, mostly the kernel. These tell you when:
ls /var/run/reboot-required 2>/dev/null || echo 'no reboot needed right now' # Ubuntu/Debian
sudo dnf needs-restarting -r # Rocky/RHEL
Reboot should not be necessary.
Plan a quiet time and do it. A new kernel does nothing until you boot into it.
Long-term security support: who patches it, and for how long?
If you run a long-term release, check who is patching it, and until when. Terms change, so check the current pages (sources at the bottom).
Ubuntu Pro free for personal use on up to 5 machines. Extends security fixes to 10 years and to the Universe packages
(esm-apps), and Livepatch applies some high and critical kernel fixes without a reboot.
Red Hat Enterprise a 10-year life cycle, with paid extended support after that, and kpatch live kernel patching.
Linux A free individual developer subscription covers personal use on up to 16 systems.
Oracle Linux free to download and use, and its updates and errata are free. 10-year support; Ksplice live patching
comes with Premier Support (and on Oracle Cloud).
Rocky and Alma rebuild Red Hat's fixes for free, usually shortly after Red Hat ships them.
For a business that can't afford surprises, paid support with live patching is worth what it costs.
On Ubuntu, one command tells you where you stand:
pro security-status
Lab, Ubuntu 24.04, not attached to Pro:
This machine is receiving security patching for Ubuntu Main/Restricted
repository until 2029.
This machine is NOT attached to an Ubuntu Pro subscription.
Ubuntu Pro with 'esm-infra' enabled provides security updates for
Main/Restricted packages until 2034.
Step 3: the front door, SSH keys only
The noise client in our lab tried names like admin, clinic and test. The log calls those "Invalid user". One line was different:
Connection closed by authenticating user backup 172.31.66.77 port 59638
backup is a real, built-in account on Debian and Ubuntu. Finding a name is fine, if a name is all it can find. So: no passwords on SSH.
Make a key on your own computer, with a passphrase, and copy the public half to the server. The private half never leaves your laptop.
ssh-keygen -t ed25519 -C 'mo@laptop' # set a passphrase when asked
ssh-copy-id mo@172.31.66.10 # asks for the account password one last time
Number of key(s) added: 1
eval "$(ssh-agent -s)"; ssh-add # type the passphrase once per session
ssh mo@172.31.66.10 'echo logged in with my key on $(hostname)'
logged in with my key on clinic
Then turn off what you no longer need, in a small file of its own:
printf 'PermitRootLogin no\nPasswordAuthentication no\nKbdInteractiveAuthentication no\nMaxAuthTries 3\n' | sudo tee /etc/ssh/sshd_config.d/10-hardening.conf
sudo sshd -t && echo 'config OK' # checks the files; prints nothing else when happy
sudo systemctl reload ssh # Ubuntu/Debian; Rocky/RHEL: sudo systemctl reload sshd
sudo sshd -T | grep -E '^(permitrootlogin|passwordauthentication|maxauthtries)'
maxauthtries 3
permitrootlogin no
passwordauthentication no
Name it with a low number. sshd reads the drop-in files in name order and the FIRST value it sees wins, and some cloud images ship a
50-cloud-init.conf that turns passwords back on. Capital -T shows what sshd will really use, whatever the files say.
Keep your current session open. Test a NEW login in a second window before you log out:
ssh -o PubkeyAuthentication=no mo@172.31.66.10
mo@172.31.66.10: Permission denied (publickey).
ssh mo@172.31.66.10 'echo key login still works on $(hostname)'
key login still works on clinic
Locking yourself out is the most common hardening mistake there is. The deeper guide: SSH Hardening in Depth (link below).
Passwords and two-factor
Keys protect SSH. Your server still has passwords around it: the hosting account, the email, the DNS registrar, the admin page.
AI guesses the way people think. So:
Length beats clever: 14 characters or more, a passphrase of several random words.
One per site: kept in a password manager, never reused.
Two-factor: everywhere it's offered: email, hosting, DNS, admin panels, the password manager itself.
A stolen password with no second factor is just a word.
On the server, one line sets a minimum length for local passwords. Ubuntu (package libpam-pwquality) and Rocky (installed by default) both read it:
sudo mkdir -p /etc/security/pwquality.conf.d # Ubuntu doesn't create the folder
echo 'minlen = 14' | sudo tee /etc/security/pwquality.conf.d/10-length.conf
passwd # as a normal user, try Summer2026!
BAD PASSWORD: The password is shorter than 14 characters
Root can still set a short password with chpasswd (it only warns), so this protects users from themselves, not from an admin.
Step 4: a firewall that says no first
Say no to everything coming in, then yes to only what you need. Allow SSH BEFORE you switch it on.
Ubuntu: ufw
sudo ufw default deny incoming
sudo ufw allow OpenSSH
sudo ufw allow 443/tcp
sudo ufw enable
sudo ufw status verbose
Default: deny (incoming), allow (outgoing), deny (routed)
22/tcp (OpenSSH) ALLOW IN Anywhere
443/tcp ALLOW IN Anywhere
Rocky: firewalld. Same idea, different words. Our Rocky box came with the Cockpit web console port open, which nobody asked for:
sudo systemctl enable --now firewalld
sudo firewall-cmd --list-services
cockpit dhcpv6-client ssh
sudo firewall-cmd --permanent --remove-service=cockpit # one change per command: firewall-cmd won't take both at once
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload
sudo firewall-cmd --list-all | grep services
services: dhcpv6-client https ssh
Always check from OUTSIDE the server. From the laptop:
curl -sk https://172.31.66.10
<h1>Clinic bookings</h1>
curl -s -m 3 http://172.31.66.10 || echo 'port 80: blocked'
port 80: blocked
timeout 3 bash -c '</dev/tcp/172.31.66.10/21' 2>/dev/null || echo 'port 21: closed'
port 21: closed
More: Firewall Basics (nftables, ufw, firewalld).
Step 5: slow the swarm with fail2ban
fail2ban reads the log, and when one address keeps failing, it blocks that address for a while.
sudo apt install fail2ban # Ubuntu
sudo dnf install fail2ban # Rocky/RHEL (from EPEL: sudo dnf install epel-release first)
printf '[sshd]\nenabled = true\nbackend = systemd\nmaxretry = 3\nfindtime = 10m\nbantime = 1h\n' | sudo tee /etc/fail2ban/jail.local
sudo systemctl enable --now fail2ban
sudo fail2ban-client status sshd
Lab, Ubuntu, after our own test client failed on purpose for a while:
|- Total failed: 389
|- Currently banned: 1
`- Banned IP list: 172.31.66.77
Honest note: a swarm uses thousands of addresses, so this is a speed bump, not a wall. That's why the keys in Step 3 matter more.
fail2ban keeps the noise down, so you can see what's real.
On Rocky 9 the SSH log lines come from sshd-session (newer OpenSSH). fail2ban 1.1.0's filter already matches them; very old fail2ban versions may not.
Don't lock yourself out: add your own address to ignoreip in jail.local if you work from a fixed one. More: fail2ban: Automatic Banning.
Step 6: watch and check
A good lock tells you who tried. Three tools, three questions.
Who tried, and who got in? The SSH log tells both stories:
sudo journalctl -u ssh --since today --no-pager | grep -c 'Invalid user' # Rocky: -u sshd
1301
sudo journalctl -u ssh --since today --no-pager | grep 'Accepted publickey' | tail -2
sshd[54222]: Accepted publickey for mo from 172.31.66.99 port 60034 ssh2: ED25519 ...
last -n 5 # who logged in
sudo lastb -n 5 # who failed (lab: support, oracle, wen, test, clinic, all from our test client)
What changed? AIDE takes a fingerprint of important files, and a later check lists exactly what changed.
Ubuntu: sudo aideinit -y -f # builds and installs the database (17 s, 16,536 entries in the lab)
Rocky: sudo aide --init && sudo mv /var/lib/aide/aide.db.new.gz /var/lib/aide/aide.db.gz
echo '# changed' | sudo tee -a /etc/ssh/sshd_config # a harmless test change
sudo aide --check # Ubuntu: sudo aide --check --config /etc/aide/aide.conf
Changed entries: 1
f > ... ..H.. . : /etc/ssh/sshd_config
sudo sed -i '$d' /etc/ssh/sshd_config && echo 'put back' # and put it back
Ubuntu also listed /var/log/aide/aideinit.log, AIDE's own log. Check what your AIDE config actually watches: Rocky's watches only
permissions for most of /etc, but full content for named files such as sshd_config, passwd and sudoers. Run the check from a timer, and
read the result; rebuild the database after changes you made on purpose.
How am I doing overall? Lynis audits the whole system and hands you a to-do list:
sudo lynis audit system --quick
Lab hardening index, before and after Steps 1 to 7:
Ubuntu 24.04 64 -> 69
Rocky 9 65 -> 67
Not a hundred. The suggestions list is the useful part. More: OpenSCAP, USG and Lynis.
AI can help defenders too
Ask an AI assistant to explain a Lynis warning, a strange log line or an sshd option, or to summarise a long log. Read the answer yourself. It can be confidently wrong. Test any suggested change in a lab first, like any other change. Never paste passwords, private keys, tokens, customer data or whole config files with secrets into a chatbot.
Step 7: backups you have restored
Everything else can fail. This is the one that saves you.
3 copies of your data
2 kinds of storage
1 kept offline, where nothing on the network can reach it (ransomware deletes the backups it can see)
A backup you have never restored is just a hope. The lab does the smallest possible version of the whole loop:
sudo mkdir -p /backup
sudo tar -czf /backup/clinic-$(date +%F).tar.gz -C /srv clinic
mkdir -p ~/restore-test && sudo tar -xzf /backup/clinic-*.tar.gz -C ~/restore-test
sudo diff -r /srv/clinic ~/restore-test/clinic && echo 'restore matches'
restore matches
diff printing nothing means the restored copy matches. For real data use a proper tool (restic, borg, your provider's snapshots), keep one
copy offline, and put a restore test in the calendar.
If it happens
Don't panic, and don't wipe it straight away.
1. Isolate: unplug it from the network (or detach it in the cloud console). Don't power it off yet if you can avoid it.
2. Keep the evidence: copy the logs off the box. They are evidence, and they show how it happened.
3. Rebuild: from a clean install and a clean backup, not by "cleaning" the broken one.
4. Rotate: change every password and every key that ever touched that machine.
5. Report it: to your local police or national cyber centre.
US: the FBI's IC3 (ic3.gov) and CISA (cisa.gov/report)
UK: Action Fraud (actionfraud.police.uk) and the NCSC (report.ncsc.gov.uk)
There's no shame in being broken into. Only in pretending you weren't.
What this page doesn't cover
The lab ran in containers, so these were left out on purpose. Do them on real hosts and VMs: auditd rules, kernel and sysctl hardening, SELinux / AppArmor policy, disk encryption, MFA for SSH itself. The compliance side (CIS, STIG, PCI DSS and the rest) has its own guides on the Hardening page.
Related guides
Linux Hardening Basics
SSH Hardening in Depth
Firewall Basics (nftables, ufw, firewalld)
fail2ban: Automatic Banning
OpenSCAP, USG and Lynis
Ghost Circuit: Shanghai, the anime
Sources (checked 2026-11-26)
- Ubuntu Pro
- Ubuntu Pro services overview
- Red Hat Enterprise Linux life cycle
- RHEL subscription guide (developer subscription)
- Oracle Linux: free updates and errata
- Oracle Linux support (Ksplice)
- FBI Internet Crime Complaint Center (IC3)
- CISA: report an incident
- Action Fraud (UK)
- NCSC: report an incident (UK)
⚠️ Heads up: Only harden systems you own or are authorised to manage. Hardening can break things: test on a copy, keep console access, and keep your current SSH session open until a new login works.