Complete Hardened sysctl.conf for Linux Server Security

Author: JJustis | Published: 2025-10-15 17:24:24
Article Image 1
Complete Hardened sysctl.conf for Linux Server Security

File:/etc/sysctl.conf
Purpose:Secure network stack, kernel behavior, and memory handling against common attacks.
Applies to:Ubuntu, Debian, CentOS, Rocky, Alma, Arch, Fedora, RHEL

🌐 Network Stack Hardening
  • Ignore ICMP broadcast requests to prevent smurf attacks
  • net.ipv4.icmp_echo_ignore_broadcasts = 1
  • Ignore bogus ICMP errors
  • net.ipv4.icmp_ignore_bogus_error_responses = 1
  • Disable ICMP redirects (spoofing vector)
  • net.ipv4.conf.all.accept_redirects = 0
    net.ipv4.conf.default.accept_redirects = 0
  • Disable sending ICMP redirects
  • net.ipv4.conf.all.send_redirects = 0
    net.ipv4.conf.default.send_redirects = 0
  • Enable reverse path filtering (anti-spoofing)
  • net.ipv4.conf.all.rp_filter = 1
    net.ipv4.conf.default.rp_filter = 1
  • Enable TCP SYN cookies (DoS mitigation)
  • net.ipv4.tcp_syncookies = 1
  • Disable source routing
  • net.ipv4.conf.all.accept_source_route = 0
    net.ipv4.conf.default.accept_source_route = 0
  • Disable proxy ARP unless explicitly needed
  • net.ipv4.conf.all.proxy_arp = 0
  • Enable ignoring of ICMP echo requests to broadcast/multicast
  • net.ipv4.icmp_echo_ignore_all = 0

    🧱 IPv6 Security (if used)
  • Disable IPv6 if unused (set to 1 to disable)
  • net.ipv6.conf.all.disable_ipv6 = 0
  • Disable IPv6 router advertisements
  • net.ipv6.conf.all.accept_ra = 0
    net.ipv6.conf.default.accept_ra = 0
  • Disable IPv6 redirects
  • net.ipv6.conf.all.accept_redirects = 0
    net.ipv6.conf.default.accept_redirects = 0

    🧠 Kernel Protection and Memory Hardening
  • Enable address space layout randomization (ASLR)
  • kernel.randomize_va_space = 2
  • Disable core dumps for setuid programs
  • fs.suid_dumpable = 0
  • Prevent unprivileged users from using BPF JIT compiler
  • net.core.bpf_jit_harden = 2
  • Prevent kernel pointer exposure
  • kernel.kptr_restrict = 2
  • Restrict use of kernel dmesg buffer
  • kernel.dmesg_restrict = 1
  • Enable kernel exec-shield (if supported)
  • kernel.exec-shield = 1
  • Restrict access to kernel sysrq (system request) keys
  • kernel.sysrq = 0
  • Disable magic SysRq entirely unless debugging
  • kernel.sysrq = 0
  • Disable unprivileged user namespaces
  • kernel.unprivileged_userns_clone = 0

    🔒 Network Layer Timeouts and Limits
  • Reduce FIN timeout to prevent socket exhaustion
  • net.ipv4.tcp_fin_timeout = 15
  • Limit backlog queue length
  • net.ipv4.tcp_max_syn_backlog = 2048
  • Enable TCP SYN backlog protection
  • net.ipv4.tcp_syn_retries = 5
  • Drop time-waited connections faster
  • net.ipv4.tcp_tw_reuse = 1
  • Limit TCP keepalive probes
  • net.ipv4.tcp_keepalive_probes = 5
  • Adjust TCP keepalive interval (seconds)
  • net.ipv4.tcp_keepalive_intvl = 30
  • Adjust TCP keepalive time (seconds)
  • net.ipv4.tcp_keepalive_time = 1800

    📶 ARP & Routing Controls
  • Ignore ARP requests from non-local sources
  • net.ipv4.conf.all.arp_ignore = 1
  • Reply only to ARP requests destined for local address
  • net.ipv4.conf.all.arp_announce = 2
  • Disable dynamic route cache (helps prevent route cache DoS)
  • net.ipv4.route.flush = 1

    🛡️ Logging and Audit Controls
  • Log martian packets (spoofed or invalid sources)
  • net.ipv4.conf.all.log_martians = 1
    net.ipv4.conf.default.log_martians = 1
  • Enable auditing of suspicious network activity
  • net.ipv4.tcp_invalid_ratelimit = 500
  • Increase TCP SYN backlog
  • net.ipv4.tcp_max_syn_backlog = 4096

    ⚙️ Filesystem Hardening
  • Disable hardlink following
  • fs.protected_hardlinks = 1
  • Disable symlink following
  • fs.protected_symlinks = 1
  • Enable stricter FIFO and regular file restrictions
  • fs.protected_fifos = 2
    fs.protected_regular = 2

    🧰 Performance and Misc
  • Increase file descriptor limit
  • fs.file-max = 2097152
  • Increase maximum connection backlog
  • net.core.somaxconn = 1024
  • Reduce TCP retransmission attempts
  • net.ipv4.tcp_retries2 = 5
  • Optimize TCP window scaling
  • net.ipv4.tcp_window_scaling = 1
  • Enable network congestion control (CUBIC by default)
  • net.ipv4.tcp_congestion_control = cubic
  • Enable fair queue (FQ) pacing for smoother bandwidth
  • net.core.default_qdisc = fq

    💡 Apply & Verify Changes
  • Save the file:
  • sudo nano /etc/sysctl.conf
  • Apply all rules immediately:
  • sudo sysctl -p
  • Verify settings applied:
  • sudo sysctl -a | grep net.ipv4

    ✅ Recommended Companion Tools
  • fail2ban — blocks repeated login attempts
  • ufw — simple firewall interface
  • rkhunter — rootkit scanner
  • lynis — full system audit
  • auditd — kernel-level logging for compliance

  • Conclusion
    This configuration protects your VPS from spoofing, DoS floods, kernel exploits, and filesystem abuse. It reduces network noise, strengthens address randomization, and enforces privilege separation at the kernel level — forming a solid foundation for any hardened Linux system.