Linux SYN Flood Protection: sysctl and nftables Hardening
Linux SYN flood protection starts with four things: SYN cookies, correctly sized SYN and accept queues, strict reverse-path filtering, and a per-source rate limit in nftables. These settings help a server survive small L4 attacks. They cannot help when the attack fills your uplink or exhausts the NIC's packet rate, because the packets have already arrived by the time Linux sees them.
This guide gives you a tested baseline, shows how to confirm it works, and explains when to move the problem upstream with RTBH, FlowSpec or a DDoS tunnel.
What a SYN flood does to a Linux host
When a SYN arrives at a listening socket, the kernel puts a request in the SYN queue and sends a SYN-ACK. The entry stays there until the client's ACK arrives or retries expire. After the handshake completes, the connection moves to the accept queue and waits for the application to call accept().
A flood hits both queues:
- SYN queue full: new SYNs are dropped, or answered with SYN cookies if enabled.
- Accept queue full: completed handshakes are dropped or ignored, and the app looks hung even though CPU is idle.
- Softirq saturation: at high packet rates one CPU core spends all its time in network receive processing, and no queue tuning helps.
Knowing which of the three you are hitting decides which fix applies.
Step 1: Measure before you tune
Check the counters first so you can see what changes later:
nstat -az TcpExtListenOverflows TcpExtListenDrops \
TcpExtTCPReqQFullDoCookies TcpExtTCPReqQFullDrop \
TcpExtSyncookiesSent TcpExtSyncookiesFailed
ss -lnt '( sport = :443 )'
For listening sockets, ss shows the current accept queue in Recv-Q and the configured maximum in Send-Q. If Recv-Q sits near Send-Q during an event, the accept queue is the bottleneck. If TcpExtTCPReqQFullDoCookies climbs, the SYN queue is overflowing and cookies are being used.
Step 2: sysctl baseline
Create /etc/sysctl.d/90-synflood.conf:
# Answer with SYN cookies when the SYN queue overflows
net.ipv4.tcp_syncookies = 1
# SYN queue size (the effective limit also depends on the listen() backlog)
net.ipv4.tcp_max_syn_backlog = 8192
# Upper cap for listen() backlog
net.core.somaxconn = 4096
# Fewer SYN-ACK retries = half-open entries expire sooner (default 5)
net.ipv4.tcp_synack_retries = 3
# Backlog of packets queued per CPU before the stack starts dropping
net.core.netdev_max_backlog = 5000
# Reverse path filtering: 1 = strict, 2 = loose
net.ipv4.conf.all.rp_filter = 1
net.ipv4.conf.default.rp_filter = 1
Apply with sysctl --system.
What each setting really does
| Setting | Effect | Common mistake |
|---|---|---|
tcp_syncookies |
Encodes connection state in the SYN-ACK so no queue entry is needed. Used only when the queue overflows (value 1). | Setting it to 2 permanently. That forces cookies for all connections and is meant for testing. |
tcp_max_syn_backlog |
Sizes the SYN queue. | Raising it to a huge value and expecting more protection. A bigger queue just holds more spoofed entries. |
somaxconn |
Caps the backlog an application can request. | Raising it without raising the application's own backlog. The smaller value wins. |
tcp_synack_retries |
Controls how long half-open entries linger. | Setting it to 0 or 1, which breaks clients on lossy links. |
rp_filter |
Drops packets whose source would not be routed back out the same interface. | Using strict mode on multi-homed or asymmetric setups. |
Leave net.ipv4.tcp_timestamps enabled. SYN cookies use TCP timestamp bits to carry options such as window scaling and SACK, so disabling timestamps degrades cookie connections.
rp_filter and tunnels
Strict rp_filter=1 is right for a single-homed server. It drops simple spoofed-source floods using bogus source addresses. If the host has several uplinks, policy routing, or receives traffic through a GRE tunnel with different return paths, use loose mode (2) or you will silently drop legitimate traffic. The kernel applies the maximum of the all and per-interface values, so check both with sysctl -a | grep rp_filter.
Step 3: nftables rate limiting
Rate limiting new SYNs per source IP cuts off non-spoofed floods and noisy scanners. Use a dynamic set so each source gets its own counter:
table inet filter {
set syn_limit_v4 {
type ipv4_addr
size 65535
flags dynamic,timeout
timeout 1m
}
chain input {
type filter hook input priority 0; policy drop;
iif "lo" accept
ct state established,related accept
ct state invalid drop
# Per-source SYN rate limit on web ports
tcp dport { 80, 443 } tcp flags & (fin|syn|rst|ack) == syn \
add @syn_limit_v4 { ip saddr limit rate over 30/second burst 60 packets } \
counter drop
tcp dport { 22, 80, 443 } ct state new accept
icmp type echo-request limit rate 10/second accept
}
}
Test with nft list ruleset and watch the counter. Start with generous numbers. A corporate NAT or mobile carrier can put thousands of users behind one IPv4 address, and a tight limit will block them.
The limits of per-IP limiting
- Spoofed sources defeat it. Each fake address sends one SYN, so none exceeds the limit. SYN cookies and upstream filtering handle that case.
- Memory: the set holds one entry per active source. Keep
sizebounded, and expect entries to be evicted or new ones refused under extreme source diversity. - CPU: the rule still runs per packet. You save the application, not the NIC.
If conntrack is also a bottleneck, consider a raw table rule with notrack for pure SYN-cookie traffic. Be careful, because that is easy to get wrong with NAT or stateful rules.
Step 4: Web server backlog tuning
If the app requests a backlog of 511 and your sysctl allows 4096, you still get 511. Set both.
nginx (default backlog is 511 on Linux):
server {
listen 443 ssl backlog=4096 reuseport;
listen [::]:443 ssl backlog=4096 reuseport;
}
reuseport creates a socket per worker, which spreads accept load across CPUs.
Apache:
ListenBacklog 4096
Restart the service (not just reload) so the listening socket is recreated, then verify with ss -lnt. Send-Q should show the new value.
Raising backlogs helps with legitimate traffic bursts and with slow accept loops. It does not stop floods. It just buys time.
What host tuning cannot do
Host-level controls work only on packets that reach the server. They fail in these cases:
- Link saturation. If the attack fills your port or transit link, packets are dropped upstream, before nftables can act. Legitimate traffic dies with it.
- High packet rates. Small-packet floods can saturate a single receive queue. Check
mpstat -P ALL 1for one core at 100% softirq. RSS, IRQ pinning and XDP can raise the ceiling, but only to a point. - Shared infrastructure. An attack on one IP can degrade every server behind the same router, firewall or uplink.
- Non-TCP attacks. UDP amplification and ICMP floods are not helped by SYN settings.
When to escalate upstream
Use these signals to decide:
| Signal | Likely next step |
|---|---|
| Queue overflow, CPU fine, link has headroom | Host tuning above is sufficient |
| One core saturated in softirq | NIC and kernel tuning, or filter upstream |
| Uplink utilization near 100% | Upstream mitigation required |
| Attack targets one IP, rest of the network must survive | RTBH for that /32 |
| Attack has a clear signature (protocol, port, length) | BGP FlowSpec |
| Sustained or repeated attacks, service must stay up | Scrubbing through a DDoS tunnel |
RTBH announces the victim's /32 with your provider's blackhole community. It protects the rest of the network but completes the attack against that IP. A Juniper example:
set routing-options static route 192.0.2.10/32 discard community 64496:666
Use the community your upstream documents. FlowSpec is more precise, but it needs router support and careful guardrails. A DDoS tunnel announces your prefixes from a scrubbing edge and returns clean traffic over GRE with a BGP session inside, so the service stays reachable.
Where NixMon fits
Host tuning needs a trigger for escalation. NixMon analyzes NetFlow v5/v9, IPFIX and sFlow from your routers. It applies per-IP and per-prefix bps/pps thresholds, and it can announce RTBH or FlowSpec when a threshold is crossed. If you have your own ASN and at least one /24, NixGuard Edge provides always-on or on-demand scrubbing with clean traffic delivered over a GRE/BGP tunnel. You can try the detection side with the 15-day free trial, no credit card required.
Common mistakes checklist
- Raising
somaxconnbut not the application backlog. - Enabling
tcp_syncookies=2permanently. - Using strict
rp_filteron asymmetric or multi-homed hosts. - Setting rate limits without checking for NAT or CGNAT clients.
- Tuning the host while the uplink is saturated.
- Having no flow-based alerting, so you learn about attacks from customers.
Frequently asked questions
Is tcp_syncookies enabled by default on Linux?
On most modern distributions it is set to 1, which means cookies are used only when the SYN queue overflows. Confirm with sysctl net.ipv4.tcp_syncookies and set it explicitly in /etc/sysctl.d so it survives image changes.
Does increasing tcp_max_syn_backlog stop a SYN flood?
No. A larger SYN queue only holds more half-open entries, including spoofed ones. It helps with bursts, but SYN cookies, rate limiting and upstream filtering are what actually reduce the impact of a flood.
Should I use strict or loose rp_filter?
Use strict (1) on single-homed servers. Use loose (2) if the host has multiple uplinks, policy routing, or receives traffic through tunnels with asymmetric return paths. Strict mode in those setups drops legitimate packets.
When is host tuning not enough and I need RTBH or a DDoS tunnel?
When the attack saturates your uplink or the NIC packet rate, because packets are lost before the host can filter them. At that point use RTBH or FlowSpec at your edge, or route traffic through a scrubbing service over a DDoS tunnel.