While firewalls like UFW are excellent for closing unnecessary ports, and web servers can rate-limit application traffic, your server's lowest line of defense is the Linux kernel itself. By default, most Linux distributions ship with network settings tuned for maximum compatibility and routing performance rather than strict security.

When a malicious actor launches a low-level network attack—such as an IP spoofing campaign or a TCP SYN flood—the kernel is responsible for handling those packets. By modifying the kernel parameters through the sysctl interface, you can harden your VPS network stack to automatically drop malicious packets before they overwhelm your system resources.

Introduction to Sysctl

The sysctl utility allows you to read and modify the attributes of the Linux kernel in real-time. While you can change these settings temporarily on a running server, making them permanent requires editing the main configuration file.

  • Always back up the default configuration before making changes by running sudo cp /etc/sysctl.conf /etc/sysctl.conf.bak.

  • Open the configuration file using your preferred text editor: sudo nano /etc/sysctl.conf.

  • Most of the security settings are already present in this file but are commented out (preceded by a #). You can either uncomment them or add them to the bottom of the file.

Mitigating TCP SYN Floods (SYN Cookies)

A SYN flood is a classic Layer 4 DDoS attack. The attacker sends thousands of TCP connection requests (SYN packets) but never completes the final step of the handshake. This leaves "half-open" connections consuming your server's memory until it can no longer accept legitimate traffic.

Enabling TCP SYN Cookies forces the kernel to handle these handshakes cryptographically, preventing the server from allocating memory until the connection is fully verified.

  • Locate or add the following line in your sysctl.conf file: net.ipv4.tcp_syncookies = 1

  • Set the maximum number of remembered connection requests to a higher threshold to accommodate the cookies during an attack: net.ipv4.tcp_max_syn_backlog = 2048

Preventing IP Spoofing (Reverse Path Filtering)

Attackers often disguise their identity by forging (spoofing) the source IP address on the packets they send to your server. This makes it incredibly difficult to block them via a firewall since the traffic appears to be coming from a trusted or random source.

Reverse Path Filtering instructs the kernel to verify that incoming packets are arriving on the logical network interface they claim to be from. If the kernel determines that a packet's source address is cryptographically impossible based on the server's routing table, it drops the packet immediately.

  • Enable strict reverse path filtering for all network interfaces by adding: net.ipv4.conf.all.rp_filter = 1 net.ipv4.conf.default.rp_filter = 1

Defending Against Smurf Attacks and Ping Floods

ICMP (Internet Control Message Protocol) is primarily used for diagnostic tools like ping. However, attackers can exploit this by sending a massive wave of ping requests to a broadcast address while spoofing your server's IP (a Smurf attack), or simply by flooding your server with direct pings to eat up bandwidth.

You can configure the kernel to silently ignore broadcast pings and malicious ICMP redirect messages, which are often used to maliciously alter your server's routing tables.

  • Ignore all ICMP echo requests sent to broadcast addresses: net.ipv4.icmp_echo_ignore_broadcasts = 1

  • Disable the acceptance of ICMP redirects (unless your server is specifically acting as a network router): net.ipv4.conf.all.accept_redirects = 0 net.ipv4.conf.default.accept_redirects = 0

  • Log packets with impossible addresses (Martian packets) so they appear in your system logs for auditing: net.ipv4.conf.all.log_martians = 1

Applying Your Kernel Changes

Once you have added the necessary directives to the file, simply saving it will not apply the changes to the running system.

  • Save and exit the sysctl.conf file.

  • Apply the new rules immediately without needing to reboot the server by executing: sudo sysctl -p.

  • The terminal will output the list of variables that were successfully updated, confirming that your server's network stack is now actively rejecting spoofed and flooded packets.

Hjalp dette svar dig? 0 Kunder som kunne bruge dette svar (0 Stem)