How to use
- Choose a template, such as Web server (SSH, HTTP, HTTPS), or edit the rules directly.
- For each rule, enter ports, the protocol, the source address (leave empty for anywhere) and Allow or Block.
- Set your SSH port and decide whether to block all other incoming traffic.
- Pick your firewall and copy the commands into a root shell on the server.
Options explained
- Ports
- A single port (22), a list (80,443) or a range (8000-8100).
- From
- An IPv4 or IPv6 address or network, such as 203.0.113.0/24. IPv6 sources go to ip6tables.
- Block all other incoming traffic
- Sets the default incoming policy to deny. Outgoing traffic stays allowed.
Example output
Three rules, with SSH on port 22 and the default policy set to deny: allow SSH only from 203.0.113.5, allow TCP ports 80 and 443 from anywhere, and block 198.51.100.0/24 from port 25. The block rule is written first, because the first matching rule wins.
# ufw
ufw default deny incoming
ufw default allow outgoing
ufw deny from 198.51.100.0/24 to any port 25 proto tcp
ufw allow from 203.0.113.5 to any port 22 proto tcp
ufw allow 80,443/tcp
ufw enable
# iptables
iptables -A INPUT -p tcp -s 198.51.100.0/24 --dport 25 -j DROP
iptables -A INPUT -p tcp -s 203.0.113.5 --dport 22 -j ACCEPT
iptables -A INPUT -p tcp -m multiport --dports 80,443 -j ACCEPT
iptables -P INPUT DROP
For firewalld, ports open to everyone use --add-port, while rules with a source or a block use a rich rule. Because the only SSH rule has a source address, the firewalld commands also remove the default ssh service from the zone. Without that step the zone would still let everyone in on port 22.
Frequently asked questions
How do I avoid locking myself out?
Keep a rule that allows your SSH port, and if you limit SSH to certain IPs, check your current IP first. Keeping the cloud provider’s web console open gives you a way back in.
I opened the port but still cannot connect. Why?
Cloud servers usually have a security group or network firewall in the provider’s console as well; the port must be open there too. Also check that the service is listening, for example with ss -tlnp.
Which firewall does my server use?
Ubuntu and Debian usually use ufw; RHEL, Rocky, AlmaLinux and CentOS use firewalld. Both are front ends for the kernel firewall, so use one of them rather than mixing in raw iptables commands.
Privacy
This tool runs entirely in your browser. Your files and text are processed on your device and are never uploaded to our servers.