Linux — Level by Level›Level 3 · Cheat sheet & self-check

Level 3 — Advanced: kernel & security · wrap-up

Cheat sheet & self-check

Every command from this section on one page.

13 · Kernel, modules & sysctl

Kernel & modules

uname -rRunning kernel version
lsmod | grep br_netfilterIs a module loaded?
modinfo overlayModule details and parameters
sudo modprobe br_netfilterLoad a module now
echo br_netfilter | sudo tee /etc/modules-load.d/k8s.confLoad it at every boot

sysctl

sysctl net.ipv4.ip_forwardRead one parameter
sudo sysctl -w vm.swappiness=10Change it now (lost at reboot)
sudo sysctl --systemApply all files in /etc/sysctl.d/
cat /proc/cmdlineParameters the kernel was booted with

14 · cgroups & namespaces

Namespaces

lsnsList namespaces on the host
sudo unshare --pid --fork --mount-proc bashA shell in a new PID namespace (it becomes PID 1)
sudo unshare --net bashA shell with its own, empty network stack
sudo nsenter -t <pid> -n ip addrRun a command inside another process's network namespace
ls -l /proc/<pid>/nsWhich namespaces a process belongs to

cgroups v2

stat -fc %T /sys/fs/cgroupcgroup2fs = cgroup v2 in use
systemd-cglsThe cgroup tree
systemd-cgtopLive resource use per cgroup
sudo systemd-run --unit=demo -p MemoryMax=64M -p CPUQuota=20% sleep 600Run something in a limited cgroup
cat /sys/fs/cgroup/<path>/memory.maxA cgroup's memory limit

15 · NUMA, huge pages & CPU pinning

Inspect the hardware

lscpu | grep -i -E 'numa|socket|thread'Sockets, NUMA nodes, threads per core
numactl --hardwareNUMA nodes, their CPUs, memory and distances
lscpu -eEach CPU with its core, socket and NUMA node
grep -i huge /proc/meminfoHuge page totals and usage

Control placement

taskset -c 2,3 ./appRun on CPUs 2 and 3 only
numactl --cpunodebind=0 --membind=0 ./appRun and allocate memory on NUMA node 0
sudo sysctl -w vm.nr_hugepages=512Reserve 512 × 2 MiB huge pages
cat /sys/kernel/mm/transparent_hugepage/enabledTransparent huge pages mode

16 · Tracing with perf & eBPF

perf (CPU profiling)

sudo perf topLive view of the hottest functions system-wide
sudo perf record -F 99 -a -g -- sleep 30Sample all CPUs at 99 Hz for 30 s, with stacks
sudo perf report --stdio | head -50Where CPU time went
sudo perf record -F 99 -g -p <pid> -- sleep 30Profile one process

eBPF tools (bcc; Ubuntu names end in -bpfcc)

sudo execsnoop-bpfccEvery new process, as it starts
sudo opensnoop-bpfcc -p <pid>Files a process opens
sudo biolatency-bpfcc 10 1Disk I/O latency histogram over 10 s
sudo tcpconnect-bpfccOutgoing TCP connections as they happen
sudo runqlat-bpfcc 10 1How long tasks wait for a CPU

bpftrace one-liners

sudo bpftrace -e 'tracepoint:raw_syscalls:sys_enter { @[comm] = count(); }'System calls per process (Ctrl+C to print)
sudo bpftrace -e 'tracepoint:syscalls:sys_enter_execve { printf("%s\n", comm); }'Which programs call execve

17 · SELinux & AppArmor

SELinux (RHEL, Rocky, Alma, Fedora)

getenforce / sestatusMode: Enforcing, Permissive or Disabled
ls -Z /var/www/html / ps -eZ | grep nginxFile and process contexts (labels)
sudo ausearch -m AVC -ts recentRecent denials from the audit log
sudo restorecon -Rv /srv/wwwReset files to their policy-defined labels
sudo semanage fcontext -a -t httpd_sys_content_t '/srv/www(/.*)?'Teach the policy a new web content path
sudo setsebool -P httpd_can_network_connect onFlip a policy boolean permanently
sudo semanage port -a -t http_port_t -p tcp 8081Allow a service type to use another port

AppArmor (Ubuntu, Debian, SUSE)

sudo aa-statusLoaded profiles and their modes
sudo journalctl -k | grep -i apparmorDenials (apparmor="DENIED")
sudo aa-complain /etc/apparmor.d/<profile>Log instead of block (for debugging)
sudo aa-enforce /etc/apparmor.d/<profile>Enforce again

18 · Host hardening

Attack surface

sudo ss -ltnupEverything listening on TCP and UDP
systemctl list-unit-files --state=enabledServices that start at boot
sudo find / -xdev -perm -4000 -type f 2>/dev/nullsetuid programs

SSH & firewall

sudo sshd -tValidate sshd_config before reloading
sudo ufw default deny incoming && sudo ufw allow OpenSSH && sudo ufw enableDefault-deny firewall (Ubuntu)
sudo firewall-cmd --permanent --add-service=https && sudo firewall-cmd --reloadOpen HTTPS (RHEL family)

Updates & audit

sudo apt install unattended-upgradesAutomatic security updates (Ubuntu/Debian)
sudo systemctl enable --now dnf-automatic.timerAutomatic updates (RHEL family; configure apply_updates)
sudo auditctl -lActive audit rules
sudo ausearch -k identity -ts todayAudit events tagged 'identity'

19 · Capstone: harden & tune a host

Evidence to capture before and after

sudo ss -ltnup > listen.txtOpen ports
systemctl list-unit-files --state=enabled > enabled.txtEnabled services
sudo sysctl -a 2>/dev/null > sysctl.txtAll kernel parameters
vmstat 1 10 > vmstat.txt; iostat -xz 1 10 > iostat.txtPerformance baseline
diff before/listen.txt after/listen.txtWhat changed

Load test (from another machine)

ab -n 20000 -c 100 http://<server>/Apache Bench: requests/second and latency
wrk -t4 -c200 -d60s --latency http://<server>/wrk: throughput and latency percentiles