Every server with SSH open to the internet gets hammered by password guessers all day. Keys-only auth is the real fix, but I also like to slow the noise down at the firewall. nftables can do it with no extra daemon. Here’s a small ruleset that works on Debian and Arch.
1. Install and enable nftables
# Debian
sudo apt install nftables
# Arch
sudo pacman -S --needed nftables
sudo systemctl enable --now nftables
2. Add a per-IP limit for new SSH connections
Put this in /etc/nftables.conf, or merge it into your existing input chain. The policy here is accept on purpose so this only adds the limit, not a full lockdown.
table inet sshguard {
set ssh4 { type ipv4_addr; flags dynamic, timeout; timeout 10m; }
set ssh6 { type ipv6_addr; flags dynamic, timeout; timeout 10m; }
chain input {
type filter hook input priority -10; policy accept;
ct state established,related accept
tcp dport 22 ct state new add @ssh4 { ip saddr limit rate over 5/minute burst 5 packets } drop
tcp dport 22 ct state new add @ssh6 { ip6 saddr limit rate over 5/minute burst 5 packets } drop
}
}
Each address gets about five new SSH connections a minute. Anything faster gets dropped, and the entry expires after ten minutes.
3. Test before you load it
Keep your current SSH session open the whole time.
sudo nft -c -f /etc/nftables.conf # syntax check only
sudo nft -f /etc/nftables.conf
sudo nft list set inet sshguard ssh4
Open a second SSH session to confirm you can still get in before closing the first one.
Tips
- If SSH is on another port, change
22to match. - If your automation opens lots of SSH connections fast (Ansible with many forks), raise the rate or add an accept rule for your management address above the limit lines.
- This doesn’t replace
PasswordAuthentication no. Do that too.
God Bless, and May the Source Be with You!