Skip to main content
XsiSec.com
HomeReposBlogProjectsPortfolio
© 2026 XsiSec.com
Security rules |security.txt
Updated 2026-08-15 · v1.0.0+2026-08-14.82f92cb · 82f92cb
← Back to overview
Security article

HTB Academy - Linux Security

HTB Academy - Linux Security: Harden a Linux host quickly and safely by applying high‑impact controls: timely patching, host firewalling, MAC (SELinux/AppArmor), account & password policy, logging/time sync, and surface‑area reduction (services, SUID/SGID). This guide keeps the original intent of your notes but expands them into a repeatable checklist with copy‑paste commands. • HTB Academy • suid, sgid

2022-09-156 tags
Tags

🎯 Objective

Harden a Linux host quickly and safely by applying high‑impact controls: timely patching, host firewalling, MAC (SELinux/AppArmor), account & password policy, logging/time sync, and surface‑area reduction (services, SUID/SGID). This guide keeps the original intent of your notes but expands them into a repeatable checklist with copy‑paste commands.


⚡ TL;DR — High‑Impact Checklist

AreaWhat to doWhy it matters
Patchingapt update && apt full-upgrade (Debian/Ubuntu) / dnf upgrade (RHEL/Fedora)Fixes known vulns fast
FirewallDefault‑deny inbound, allow only required ports (e.g., SSH 22, HTTP/HTTPS)Shrinks attack surface
MACEnable SELinux (Enforcing) or tune AppArmor profilesContain process compromise
AccountsUnique users, strong passwords, aging, lockout after N failures, sudo least privilegeLimits lateral movement
Logging & TimeEnsure rsyslog/journald + logrotate + NTP (timesyncd/chrony)Forensics & correlation
ServicesRemove/disable unnecessary packages & servicesReduce exposed code
SUID/SGIDAudit and remove risky SUID/SGID, prefer capabilitiesKill common privilege-escalation paths
Integrity/Threat detectLynis, AIDE, rkhunter/chkrootkit, Snort/SuricataFind misconfig & tampering

1) Patch Management

Keeping the OS and packages updated is the #1 defensive control.

Debian/Ubuntu

bash
sudo apt update && sudo apt full-upgrade -y    # (aka dist-upgrade)
sudo apt autoremove --purge -y                 # clean unused

Note: apt full-upgrade (newer) ≈ apt dist-upgrade (older).

RHEL/CentOS/Alma/Rocky

bash
sudo dnf upgrade -y
sudo dnf autoremove -y

Schedule automatic security updates (Debian/Ubuntu)

bash
sudo apt install -y unattended-upgrades
sudo dpkg-reconfigure --priority=low unattended-upgrades

2) Host Firewalling (nftables/ufw/firewalld/iptables)

If network firewalls aren’t strict, enforce host‑level policy.

Quick policy with UFW (Ubuntu/Debian)

bash
sudo apt install -y ufw
sudo ufw default deny incoming
sudo ufw default allow outgoing
sudo ufw allow 22/tcp        # SSH
# sudo ufw allow 80/tcp      # HTTP (if needed)
# sudo ufw allow 443/tcp     # HTTPS (if needed)
sudo ufw enable
sudo ufw status verbose

firewalld (RHEL/Fedora)

bash
sudo dnf install -y firewalld
sudo systemctl enable --now firewalld
sudo firewall-cmd --set-default-zone=public
sudo firewall-cmd --permanent --add-service=ssh
# sudo firewall-cmd --permanent --add-service=http
# sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload

Prefer nftables where possible; iptables is legacy on many distros though still present. For raw nftables use nft list ruleset and nft add rule ....


3) Mandatory Access Control (SELinux or AppArmor)

Kernel security modules enforce policy between labeled processes/objects.

SELinux (RHEL/Fedora/CentOS/Alma/Rocky)

  • Each process, file, dir, and object has a label.
  • Rules (policy) constrain which domains can access which types.

Status & Modes

bash
getenforce                       # Enforcing | Permissive | Disabled
sestatus

Enable Enforcing (requires reboot if changing from disabled)

bash
# /etc/selinux/config
SELINUX=enforcing
SELINUXTYPE=targeted
bash
sudo setenforce 1               # switch to enforcing at runtime (if supported)

Common ops

bash
sudo seinfo -t httpd_t -x 2>/dev/null || true        # needs setools-console
# manage file contexts
sudo semanage fcontext -a -t httpd_sys_content_t "/srv/www(/.*)?"
sudo restorecon -Rv /srv/www
# toggle booleans (feature switches)
sudo getsebool -a | grep httpd
sudo setsebool -P httpd_can_network_connect on

AppArmor (Ubuntu/Debian)

bash
sudo aa-status
sudo aa-enforce /etc/apparmor.d/*      # enforce loaded profiles
sudo aa-complain /etc/apparmor.d/abstractions/*  # tune in complain mode

Profiles live in /etc/apparmor.d/. Use aa-logprof to iteratively build rules.


4) Accounts, Passwords & SSH

Principles: each user has a unique account; least‑privilege sudo; strong secrets; aging; lockouts.

Ensure unique users and sane defaults

bash
# list local users (with shells)
awk -F: '$7 ~ /(bash|zsh|fish|sh)$/ {print $1 ":" $7}' /etc/passwd

# new user example (no sudo by default)
sudo adduser alice
# grant limited admin (create a bounded sudoers drop-in if needed)
echo 'alice ALL=(ALL:ALL) /usr/bin/systemctl restart myapp' | sudo tee /etc/sudoers.d/alice-myapp
sudo visudo -cf /etc/sudoers.d/alice-myapp     # validate syntax

Password policy (PAM)

Complexity (pwquality) — Debian/Ubuntu:

bash
sudo apt install -y libpam-pwquality
sudoedit /etc/security/pwquality.conf
# Recommended baseline
# minlen = 14
# dcredit = -1
# ucredit = -1
# lcredit = -1
# ocredit = -1
# maxrepeat = 3
# dictcheck = 1

Aging & history

bash
# default for new users
sudo sed -i 's/^PASS_MAX_DAYS.*/PASS_MAX_DAYS   365/' /etc/login.defs
sudo sed -i 's/^PASS_MIN_DAYS.*/PASS_MIN_DAYS   1/'   /etc/login.defs
sudo sed -i 's/^PASS_WARN_AGE.*/PASS_WARN_AGE   14/'  /etc/login.defs

# per-user example
sudo chage -M 365 -m 1 -W 14 alice

Lockout after N failures (pam_faillock on modern distros)

bash
# RHEL9+/Fedora, Debian 12+ typically have pam_faillock
sudo authselect enable-feature with-faillock 2>/dev/null || true
# Or edit /etc/pam.d/system-auth and password-auth (RHEL) or common-auth (Debian):
# auth required pam_faillock.so preauth silent deny=5 unlock_time=900
# auth [default=die] pam_faillock.so authfail deny=5 unlock_time=900
# account required pam_faillock.so

SSH hardening

bash
sudo cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak.$(date +%F)
sudo tee -a /etc/ssh/sshd_config >/dev/null <<'EOF'

# --- Hardening ---
Protocol 2
PermitRootLogin no
PasswordAuthentication no        # consider key-only if feasible
PubkeyAuthentication yes
MaxAuthTries 3
LoginGraceTime 30
ClientAliveInterval 300
ClientAliveCountMax 2
AllowUsers alice                  # limit if practical
EOF

sudo systemctl restart sshd

Consider MFA via PAM (e.g., libpam-google-authenticator or pam_oath) for privileged access.


5) Logging & Time Sync

Consistent time + logs = reliable evidence.

bash
# journald + rsyslog present on most systems
systemctl is-enabled rsyslog 2>/dev/null || echo "rsyslog not installed"
sudo journalctl --disk-usage
sudo journalctl -p warning -xn

# log rotation
sudo ls /etc/logrotate.d/

# NTP
timedatectl status
sudo timedatectl set-ntp true                 # systemd-timesyncd
# or install chrony for servers
# Debian/Ubuntu: sudo apt install -y chrony
# RHEL/Fedora:   sudo dnf install -y chrony ; sudo systemctl enable --now chronyd

6) Threat Detection & Integrity

  • Lynis — config audit & best-practice checks
    bash
    sudo apt install -y lynis || sudo dnf install -y lynis
    sudo lynis audit system
  • AIDE — filesystem integrity baseline
    bash
    sudo apt install -y aide || sudo dnf install -y aide
    sudo aideinit
    sudo mv /var/lib/aide/aide.db.new.gz /var/lib/aide/aide.db.gz
    sudo aide --check
  • rkhunter/chkrootkit — quick malware heuristics (signal only)
    bash
    sudo apt install -y rkhunter chkrootkit || true
    sudo rkhunter --propupd
    sudo rkhunter --check --sk
    sudo chkrootkit
  • Snort/Suricata — NIDS for traffic visibility (optional on endpoints).

7) Remove/Disable Unnecessary Services

bash
# list enabled units
systemctl list-unit-files --type=service | grep enabled
# stop + disable what you do not need
sudo systemctl disable --now cups.service avahi-daemon.service || true

# enumerate listening sockets
ss -tulpen

8) SUID/SGID Audit & Linux Capabilities

Unnecessary SUID/SGID binaries are frequent privilege‑escalation vectors.

bash
# enumerate SUID/SGID
sudo find / -xdev -type f \( -perm -4000 -o -perm -2000 \) -print 2>/dev/null

# remove SUID bit when not required
sudo chmod u-s /path/to/binary

# prefer capabilities over SUID when you must grant specific privileges
sudo getcap -r / 2>/dev/null | sort
sudo setcap cap_net_bind_service=+ep /usr/sbin/mydaemon  # example

9) Validation — Quick Health Checks

bash
# Patching
/usr/bin/env bash -c 'if command -v apt >/dev/null; then apt -s upgrade | grep -q "0 upgraded" || echo "Upgrades pending"; fi'

# Firewall
sudo ufw status 2>/dev/null || sudo firewall-cmd --list-all 2>/dev/null || echo "Check nftables/iptables"

# MAC
(command -v getenforce >/dev/null && getenforce) || (command -v aa-status >/dev/null && aa-status) || echo "No MAC module active?"

# Accounts
sudo awk -F: '$7 ~ /(bash|zsh|fish|sh)$/ {print $1}' /etc/passwd | wc -l
sudo grep -E "^[^#].*pam_faillock" -r /etc/pam.d/ 2>/dev/null || echo "No faillock?"

# Logging/Time
timedatectl status | sed -n '1,6p'
journalctl -p err -n 5 --no-pager

10) Complementary Tools

  • Snort – Network IDS/IPS: https://www.snort.org/
  • chkrootkit – Rootkit indicators: http://www.chkrootkit.org/
  • rkhunter – Rootkit hunter: https://packages.debian.org/sid/rkhunter
  • Lynis – Security auditing: https://cisofy.com/lynis/

Appendix A — Quick Reference (What you originally listed)

  • Keep OS & packages up to date: apt update && apt dist-upgrade (or apt full-upgrade)
  • Use Linux firewall (ufw, firewalld, nftables) and/or iptables to restrict traffic.
  • SELinux/AppArmor for granular access control policies: labels + kernel‑enforced rules.
  • Recommended security utilities: Snort, chkrootkit, rkhunter, Lynis.
  • Baseline system settings:
    • Remove/disable unnecessary services/software
    • Remove services using unencrypted auth
    • Ensure NTP enabled and syslog/journald active
    • Each user must have their own account
    • Enforce strong passwords & aging; restrict re‑use
    • Lock accounts after repeated login failures
    • Disable unwanted SUID/SGID binaries

References

  • CIS Controls v8: https://www.cisecurity.org/controls/cis-controls-list
  • CIS Benchmarks (Linux): https://www.cisecurity.org/benchmark/ubuntu_linux
  • Red Hat SELinux Guide: https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/9/html/using_selinux
  • Ubuntu AppArmor: https://ubuntu.com/server/docs/security-apparmor
  • Debian Security Manual: https://www.debian.org/doc/manuals/securing-debian-howto/
Navigate

In this post

  1. 01🎯 Objective
  2. 02⚡ TL;DR — High‑Impact Checklist
  3. 031) Patch Management
  4. 04Debian/Ubuntu
  5. 05RHEL/CentOS/Alma/Rocky
  6. 06Schedule automatic security updates (Debian/Ubuntu)
  7. 072) Host Firewalling (nftables/ufw/firewalld/iptables)
  8. 08Quick policy with UFW (Ubuntu/Debian)
  9. 09firewalld (RHEL/Fedora)
  10. 103) Mandatory Access Control (SELinux or AppArmor)
  11. 11SELinux (RHEL/Fedora/CentOS/Alma/Rocky)
  12. 12AppArmor (Ubuntu/Debian)
  13. 134) Accounts, Passwords & SSH
  14. 14Ensure unique users and sane defaults
  15. 15Password policy (PAM)
  16. 16SSH hardening
  17. 175) Logging & Time Sync
  18. 186) Threat Detection & Integrity
  19. 197) Remove/Disable Unnecessary Services
  20. 208) SUID/SGID Audit & Linux Capabilities
  21. 219) Validation — Quick Health Checks
  22. 2210) Complementary Tools
  23. 23Appendix A — Quick Reference (What you originally listed)
  24. 24References
Search
Explore

Popular tags

Browse all 30 tags

Comments

0 comments

No comments yet — be the first to comment.