Linux hardening & logs
Harden SSH, disable unused services and find authentication events in Linux logs.
By Invicly Tech, , in Cyber Security Fundamentals. About 10 minutes to read.
In short
Hardening removes unnecessary exposure: lock down SSH, disable unused services, run a host firewall and keep patches current. Linux auth logs then record who logged in and who failed, which is where many investigations start.
Explained simply
Hardening means making a system harder to attack by removing things it does not need and locking down the things it does. A fresh server often runs extra services, accepts passwords over SSH and has no firewall. Each of these is an open door.
SSH (secure shell) is the main way people log in to Linux servers, so it is the main target. Bots on the internet try thousands of username and password guesses every day. Using SSH keys instead of passwords, and blocking direct root login, makes these guesses useless.
Every running service that listens on the network adds attack surface, meaning more places an attacker can poke. If you do not use a printing service or an old file-sharing service, turn it off. A host firewall then makes sure only the ports you chose can be reached.
Finally, logs tell you what happened. Linux records every successful and failed login. When something goes wrong, the auth log is often the first place an investigator looks: who logged in, from which IP address, and with which method.
Press Play on a diagram to walk through it one step at a time.
- 1Patch
Apply updates promptly.
- 2Harden SSH
Keys only, no root login.
- 3Reduce services
Disable what you do not use.
- 4Firewall
Allow only needed ports.
- 5Log
Collect and review auth logs.
- 1Network firewall
Only needed ports reach the server
- 2Host firewall
ufw or firewalld on the machine
- 3Hardened services
SSH keys only, unused services off
- 4Accounts and sudo
Named users, narrow sudo rules
- 5Patching
Automatic security updates
- 6Logging and audit
Auth logs and auditd, shipped off-host
In depth
Hardening sshd safely
The SSH server reads /etc/ssh/sshd_config, and on most current distributions also files in /etc/ssh/sshd_config.d/. Important detail: for most options, sshd uses the first value it finds. Because the Include line sits at the top, a drop-in file wins over a later line in the main file. Run sudo sshd -T to see the settings sshd will really use.
The core settings: PermitRootLogin no, PasswordAuthentication no, KbdInteractiveAuthentication no, and PubkeyAuthentication yes. MaxAuthTries 3 limits guesses per connection, and AllowGroups sshusers allows only one group to log in. Turn off what you do not use, such as X11Forwarding.
Never lock yourself out. Check the syntax with sudo sshd -t, reload the service, and test a new login in a second terminal before closing the first one.
- sshd -t checks syntax; sshd -T prints the effective config
- Service name: ssh on Debian/Ubuntu, sshd on RHEL
- Use ed25519 keys: ssh-keygen -t ed25519
Reducing services and open ports
Start by seeing what is actually listening. sudo ss -tulpn shows every TCP and UDP port open for connections, and the program behind each. Then compare with what the server is for. A web server needs 80, 443 and SSH; it does not need a mail server, rpcbind or a printer service.
To stop a service now and at boot, use sudo systemctl disable --now name. If other units might start it again, systemctl mask name blocks it completely. Removing the package is even better, because code that is not installed cannot be exploited.
Also bind internal services to localhost. A database that only the local app uses should listen on 127.0.0.1, not 0.0.0.0 (all interfaces). That one setting takes it off the network entirely.
- ss -tulpn: listening ports with process names
- systemctl list-unit-files --state=enabled: what starts at boot
- 0.0.0.0 or [::] in ss output means reachable from the network
Reading authentication logs
SSH, sudo and local logins write to the auth facility. Debian and Ubuntu save it in /var/log/auth.log, RHEL family systems in /var/log/secure. Some newer installs (for example Debian 12) keep only the systemd journal, so use journalctl -u ssh or journalctl -u sshd.
Know the key lines. "Failed password for invalid user admin from 203.0.113.5" means someone guessed a username that does not exist, typical of bots. "Failed password for alice" means a real user name with a wrong password. "Accepted publickey for alice from 198.51.100.7" is a success, with the key fingerprint. Many failures from one IP followed by one Accepted line from that same IP is the pattern to alert on.
The last command reads successful logins from /var/log/wtmp, and sudo lastb reads failed ones from /var/log/btmp.
- 1sshd logs event
Failed or Accepted, with user and IP
- 2journald / rsyslog
Stores it locally
- 3Forwarder
Ships logs to a central server
- 4Rule
Many fails then success from one IP
- 5Alert
Analyst checks the account
Host firewall and brute-force protection
A host firewall runs on the server itself, so it still protects the machine if a network firewall is misconfigured. On Ubuntu, ufw is the simple front end: set the default to deny incoming, allow only what you need, then enable it. Always allow SSH before enabling, or you will cut off your own session. On RHEL family systems, firewalld does the same with zones and services. Both write rules into the kernel's nftables (or iptables) layer.
ufw limit 22/tcp adds simple rate limiting: it blocks an address that opens 6 or more connections in 30 seconds. fail2ban goes further: it reads the auth log and bans IPs after repeated failures. Its defaults are 5 failures within 10 minutes, banned for 10 minutes. With key-only SSH, these tools mostly reduce log noise, but they still help against slow guessing on other services.
- sudo ufw default deny incoming
- sudo ufw allow OpenSSH, then sudo ufw enable
- sudo firewall-cmd --permanent --add-service=https; sudo firewall-cmd --reload
Updates and file auditing
Most real break-ins use known bugs that already have a patch. On Debian and Ubuntu, the unattended-upgrades package installs security updates automatically; on RHEL family systems, dnf-automatic does the job. Kernel updates need a reboot to take effect, so plan for that too.
For sensitive files, auditd records who changed them. The rule auditctl -w /etc/sudoers -p wa -k sudoers watches the file for writes and attribute changes and tags events with the key sudoers. Then ausearch -k sudoers shows each change with the user and the program that made it. Put permanent rules in /etc/audit/rules.d/ so they survive a reboot.
Learn it in 4 short steps
About 45m each. Track them with a timer in your plan.
- 1Harden sshd configuration~45m
- 2Disable unused services~45m
- 3Read auth logs and journalctl~45m
- 4Set up a host firewall~45m
Key points
- Prefer SSH keys and disable direct root login.
- Every running service is attack surface; remove the unused.
- A host firewall backs up network controls.
- Auth logs record successful and failed logins.
Key terms
Tap a card to flip it. 0/4 known
Examples
Locking SSH down to keys only
Step 1: from your laptop, copy your public key with ssh-copy-id and confirm key login works. Step 2: create a drop-in file with the hardened settings. Step 3: check syntax with sshd -t and confirm the effective values with sshd -T. Step 4: reload the service, keeping your current session open. Step 5: in a new terminal, log in again with your key, and try a password login to confirm it is refused.
ssh-copy-id -i ~/.ssh/id_ed25519.pub alice@server
sudo tee /etc/ssh/sshd_config.d/10-hardening.conf <<'EOF'
PermitRootLogin no
PasswordAuthentication no
KbdInteractiveAuthentication no
MaxAuthTries 3
AllowGroups sshusers
X11Forwarding no
EOF
sudo sshd -t && sudo sshd -T | grep -Ei 'permitroot|passwordauth|allowgroups'
sudo systemctl reload ssh # sshd on RHELWho is guessing passwords?
Step 1: pull the last day of sshd failures from the journal. Step 2: extract the source IP from each Failed password line. Step 3: count and sort. Step 4: check whether any of the top IPs also has an Accepted line, which would mean a guess succeeded. Step 5: if one did, treat it as an incident: lock the account, check its recent commands and keys, and look for new files.
journalctl -u ssh --since "24 hours ago" --no-pager \
| grep 'Failed password' \
| grep -oE 'from [0-9.]+' | awk '{print $2}' \
| sort | uniq -c | sort -rn | head
# then, for a suspicious IP:
journalctl -u ssh --since "24 hours ago" | grep 'Accepted' | grep 203.0.113.5Quick reference
- sudo sshd -t checks syntax; sudo sshd -T shows the effective SSH settings
- sshd uses the first value it finds, so drop-ins in sshd_config.d usually win
- sudo ss -tulpn lists listening ports and the programs behind them
- systemctl disable --now svc stops it now and at boot; mask blocks it fully
- Auth log: /var/log/auth.log (Debian/Ubuntu), /var/log/secure (RHEL), or journalctl
- last shows successful logins; sudo lastb shows failed ones
- ufw limit 22/tcp blocks IPs with 6+ connections in 30 seconds
- auditctl -w /etc/sudoers -p wa -k sudoers records every change to the file
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
On a lab VM, list every listening port and the program behind it. For each one, write whether the server needs it.
- Exercise 2medium
Enable ufw so the server only accepts SSH and HTTPS, with rate limiting on SSH. Prove other ports are blocked.
- Exercise 3hard
Use auditd to record any change to /etc/passwd and /etc/sudoers, make the rules permanent, then trigger and find an event.
Check yourself
Answer in your head, reveal, then be honest with yourself.
Feel confident about this topic?
Frequently asked questions
- Should I change the SSH port from 22?
- It cuts log noise from bots, but it is not real security, because scanners find SSH on any port in seconds. Keys only, no root login and a firewall matter far more.
- Where are SSH logs on Ubuntu?
- In /var/log/auth.log and in the journal. Use journalctl -u ssh to see them, or journalctl -u sshd on RHEL family systems.
- Is fail2ban still needed with key-only SSH?
- Password guessing cannot succeed then, so it matters less for SSH. It still reduces noise and helps protect other services that accept passwords, such as web logins or mail.
- What is a CIS Benchmark?
- A free, community-written hardening guide for a specific system, such as Ubuntu 24.04. Each item says what to set, why, and how to check it, and many scanners can test against it.