Linux Kernel Hardening: How to Make Your Server Truly Secure
A complete guide to Linux kernel hardening for server security, covering sysctl, AppArmor, SELinux, SSH hardening, Fail2Ban, and firewall configuration.
The kernel is the heart of the operating system. If the kernel can be exploited, an attacker gains full access to the entire system. Kernel hardening is the process of strengthening defenses at the deepest level of the operating system.
Sysctl Hardening
Sysctl is an interface for changing kernel parameters at runtime. By setting the right parameters, you can block many types of attacks.
Create a Configuration File
# /etc/sysctl.d/99-hardening.conf
# IP spoofing protection
net.ipv4.conf.all.rp_filter = 1
net.ipv4.conf.default.rp_filter = 1
# Ignore ICMP redirects (prevents MITM)
net.ipv4.conf.all.accept_redirects = 0
net.ipv6.conf.all.accept_redirects = 0
net.ipv4.conf.all.send_redirects = 0
# Disable source packet routing
net.ipv4.conf.all.accept_source_route = 0
net.ipv6.conf.all.accept_source_route = 0
# SYN flood protection
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_max_syn_backlog = 2048
net.ipv4.tcp_synack_retries = 2
# Log suspicious packets
net.ipv4.conf.all.log_martians = 1
# ASLR (Address Space Layout Randomization)
kernel.randomize_va_space = 2
# Disable SysRq key
kernel.sysrq = 0
# Restrict dmesg access
kernel.dmesg_restrict = 1
# Restrict kernel pointer exposure
kernel.kptr_restrict = 2
# Disable IPv6 if unused
net.ipv6.conf.all.disable_ipv6 = 1
net.ipv6.conf.default.disable_ipv6 = 1
Apply the Configuration
sudo sysctl --system
sudo sysctl -p /etc/sysctl.d/99-hardening.conf
AppArmor (Ubuntu/Debian)
AppArmor restricts programs based on path. Each program can run in enforce mode (block violations) or complain mode (only log violations).
# Check AppArmor status
sudo aa-status
# Install additional profiles
sudo apt install apparmor-profiles apparmor-profiles-extra
# Set profile to enforce
sudo aa-enforce /etc/apparmor.d/usr.sbin.nginx
# Set profile to complain (for testing)
sudo aa-complain /etc/apparmor.d/usr.sbin.nginx
# Reload all profiles
sudo systemctl reload apparmor
SELinux (RHEL/Fedora)
SELinux uses label-based access control. Every file and process has a label that determines who can access it.
# Check SELinux status
sestatus
# Set to enforcing
sudo setenforce 1
# Check file context
ls -Z /var/www/html/
# Restore context after install
sudo restorecon -Rv /var/www/html/
# Create a custom policy
ausearch -c --raw | audit2allow -M mypol
sudo semodule -i mypol.pp
SSH Hardening
SSH is the main gateway for accessing a server. A weak configuration is an invitation to attackers.
# /etc/ssh/sshd_config
# Change the default port
Port 2222
# Prohibit root login
PermitRootLogin prohibit-password
# Disable password auth (SSH key only)
PasswordAuthentication no
PubkeyAuthentication yes
# Limit login attempts
MaxAuthTries 3
# Connection timeout
ClientAliveInterval 300
ClientAliveCountMax 2
# Allow only specific users
AllowUsers yuki
# Disable empty passwords
PermitEmptyPasswords no
# Disable X11 forwarding
X11Forwarding no
# Disable TCP forwarding if not needed
AllowTcpForwarding no
Generate SSH Key
# On the client (Windows/Linux/Mac)
ssh-keygen -t ed25519 -C "[email protected]"
# Copy the public key to the server
ssh-copy-id -p 2222 yuki@server-ip
Fail2Ban Configuration
Fail2Ban monitors logs and automatically blocks suspicious IP addresses.
# /etc/fail2ban/jail.local
[sshd]
enabled = true
port = 2222
filter = sshd
logpath = /var/log/auth.log
maxretry = 3
bantime = 3600
findtime = 600
[nginx-http-auth]
enabled = true
filter = nginx-http-auth
logpath = /var/log/nginx/error.log
maxretry = 5
bantime = 7200
[nginx-limit-req]
enabled = true
filter = nginx-limit-req
logpath = /var/log/nginx/error.log
maxretry = 10
bantime = 3600
# Restart Fail2Ban
sudo systemctl restart fail2ban
# Check status
sudo fail2ban-client status
sudo fail2ban-client status sshd
Firewall (UFW)
# Default deny
sudo ufw default deny incoming
sudo ufw default allow outgoing
# Allow specific ports
sudo ufw allow 2222/tcp # SSH
sudo ufw allow 80/tcp # HTTP
sudo ufw allow 443/tcp # HTTPS
# Enable
sudo ufw enable
sudo ufw status verbose
Disable Unnecessary Services
# Check all active services
systemctl list-unit-files --state=enabled
# Disable unnecessary services
sudo systemctl disable bluetooth
sudo systemctl disable cups
sudo systemctl disable avahi-daemon
sudo systemctl disable ModemManager
Monitoring and Logging
# Install logwatch for a daily summary
sudo apt install logwatch
sudo logwatch --detail high --mailto [email protected] --range today
# Monitor the auth log in real time
sudo tail -f /var/log/auth.log | grep "Failed password"
# Check failed login attempts
sudo journalctl -u sshd --since "1 hour ago" | grep "Failed"
# Install rkhunter for rootkit detection
sudo apt install rkhunter
sudo rkhunter --check
Conclusion
Linux kernel hardening is not just about changing one or two settings. It is a layered approach that covers sysctl, AppArmor/SELinux, SSH hardening, Fail2Ban, and the firewall. Start with small steps: enable sysctl hardening and configure SSH correctly. Security is not a product but an ongoing process.
Related Articles
Hardening Homelab Server Security: A Complete Guide
Secure your homelab server with essential hardening steps from SSH key authentication to service isolation and automated updates using this practical checklist.
SecuritySSH Hardening: Securing a Server's sshd_config on the Internet
Harden any internet-facing SSH server by disabling password logins, restricting users, adding fail2ban, and applying a proven best-practices checklist.
SecurityIDS and IPS: Intrusion Detection and Prevention
Learn how IDS and IPS differ, how signature-based and anomaly-based detection work, where to deploy them, and practical Snort and Suricata examples.
Your ideas deserve to be read by thousands of people.
ByteNesia is built for everyone. If you have experience, tutorials, or opinions about technology, write and share them here. Your name will be credited as a contributor.
Write anything: tool reviews, homelab tutorials, security tips, or your tech stories.