Limit number of incoming connections on Linux

⚓ Neptune    📅 2025-03-19    👤 sword_smith    👁️ 126      

sword_smith

Well-connected nodes that receive many incoming connections attempts can experience hanging problems. It’s a problem we should fix in the neptune-core application but until we have, you can use the following commands to let the Linux kernel do the heavy lifting for us.

sudo iptables -A INPUT -p tcp --dport 9798 -m connlimit --connlimit-above 50 -j REJECT --reject-with tcp-reset
sudo iptables -A INPUT -p tcp --dport 9798 -m limit --limit 2/sec --limit-burst 2 -j ACCEPT
sudo ip6tables -A INPUT -p tcp --dport 9798 -m connlimit --connlimit-above 50 -j REJECT --reject-with tcp-reset
sudo ip6tables -A INPUT -p tcp --dport 9798 -m limit --limit 2/sec --limit-burst 2 -j ACCEPT

This limits the number of incoming TCP connections to your 9798 port to 50. And prevents more than 2 new connections per second. This limit applies regardless of the IP of the machine connecting to you. The rules apply for both IPv4 and IPv6.

Verify new rules:

sudo iptables -L -v -n
sudo ip6tables -L -v -n

Persist the new rules:

sudo apt update && sudo apt install netfilter-persistent
sudo netfilter-persistent save

Port 9798 is the standard port used by neptune-core. If you have set your --peer-port value to something, adjust the above commands.

🏷️ networking

sword_smith    2025-03-19 👍 👎 [op]

In case you want to clear all rules, use:

sudo iptables -F && sudo ip6tables -F && sudo iptables -X && sudo ip6tables -X
1