1. Home
  2. Developer
  3. Firewall Rule Generator

Firewall Rule Generator

Pick ports and IPs to get ufw, iptables or firewalld rule commands.

For every format and option in one place, use Server Config Generators →

Runs in your browser · No upload
Add a common port
Rule 1
The source for incoming rules, the destination for outgoing ones. e.g. 203.0.113.0/24
Rule 2
The source for incoming rules, the destination for outgoing ones. e.g. 203.0.113.0/24
Rule 3
The source for incoming rules, the destination for outgoing ones. e.g. 203.0.113.0/24
Commands to run
sudo ufw default deny incoming sudo ufw default allow outgoing sudo ufw limit 22/tcp comment 'SSH' sudo ufw allow 80/tcp sudo ufw allow 443/tcp sudo ufw enable sudo ufw status numbered
Review the rules before applying them to a production server. On a remote machine, confirm SSH is allowed first and apply while you still have console access. A cloud security group, if any, must be opened separately.

Your input is processed in this browser and never sent to a server.

How to use

  1. Choose the firewall (ufw, iptables or firewalld). Ubuntu usually has ufw, and RHEL, Rocky and AlmaLinux use firewalld.
  2. Set the default policy for incoming and outgoing traffic. A typical server blocks incoming and allows outgoing.
  3. Add rules with the common port buttons (SSH 22, HTTP 80, HTTPS 443 and others) or with Add a custom rule, where you set action, direction, protocol, port and peer IP.
  4. Run the generated commands on the server from the top, in order. Check commands such as sudo ufw status numbered and firewall-cmd --list-all are included at the end.

Rule fields

Field Notes
Action allow, deny (drop silently), reject (send a refusal) and limit (rate limit)
Direction incoming (in) or outgoing (out). firewalld basics cannot create outgoing rules
Protocol TCP, UDP or both. A port range for both protocols is split into two lines
Port 22, 80,443, 8000-8010. Commas split into several rules, and empty means all ports
Peer IP A single IP or a CIDR (203.0.113.0/24), or IPv6 (2001:db8::/32). The source for incoming rules, the destination for outgoing ones

One rule, three tools

A rule that allows 3306/tcp (MySQL) only from 203.0.113.0/24 becomes:

Tool Command
ufw sudo ufw allow from 203.0.113.0/24 to any port 3306 proto tcp
iptables sudo iptables -A INPUT -p tcp -s 203.0.113.0/24 --dport 3306 -j ACCEPT
firewalld sudo firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="203.0.113.0/24" port port="3306" protocol="tcp" accept'

For the default setup (SSH limit, 80, 443), ufw gets sudo ufw limit 22/tcp comment 'SSH', sudo ufw allow 80/tcp and sudo ufw allow 443/tcp, then sudo ufw enable. limit is the ufw rule that blocks an IP making 6 or more connection attempts within 30 seconds. On iptables it becomes three recent module lines, and on firewalld a rich rule with limit value="6/m"; these are similar but not identical in behavior.

Order that avoids locking yourself out

  • Check SSH is allowed first. If the default incoming policy is block and no rule allows 22/tcp, a warning appears. If you moved SSH off port 22, put that port in a rule.
  • The iptables output first allows loopback and ESTABLISHED,RELATED connections, and sets the default policy (-P INPUT DROP) last. Changing the policy only after all rules are in keeps a mistake in the middle from cutting you off.
  • iptables rules vanish on reboot. On Debian and Ubuntu, install iptables-persistent and save with netfilter-persistent save.
  • Leave “flush INPUT and OUTPUT” off on servers that run Docker, because it can interfere with the rules Docker created. Also note that ports published by Docker can bypass ufw rules.
  • Cloud providers (AWS, GCP, Oracle and others) have security groups or security lists separate from the firewall inside the server. Both must allow the traffic.

Things to watch

  • IPs, ports and notes are validated, and a rule with an invalid value is not generated. A note may only use letters, digits, spaces, ., _ and -.
  • IPv6 source rules become ip6tables commands on iptables, and the IPv6 default policy and base allow rules must be set separately.
  • Review the commands before applying them to a production server, ideally while you still have console access. If you download and run the script, ufw enable may ask for confirmation.
  • Continue with the Nginx Config Generator for the web server, the SSH Config Generator for logins, and the systemd Service Generator for services. Use the IP Subnet Calculator for the ranges you allow and HTTP Status Codes for response codes.

Frequently asked questions

ufw or iptables?

ufw is a simpler front end that works on top of iptables (or nftables). For simple rules ufw is easier to read, and for fine control you use iptables directly. Mixing both tends to tangle the rules, so pick one.

What is the difference between deny and reject?

deny (DROP) silently discards the packet, so the other side waits for a timeout, while reject sends back a refusal and fails immediately. Deny is common for the public internet and reject is handy for diagnosing an internal network.

How do I remove or undo a rule?

With ufw, list numbers with sudo ufw status numbered and run sudo ufw delete number. With iptables, change -A to -D to delete the same rule, and with firewalld change --add to --remove.

Will enabling it drop my current SSH session?

ufw and the iptables output here allow already established connections (ESTABLISHED), but new connections must match a rule. If you enable it with no SSH rule, your next login is blocked.

Is my input sent to a server?

No. The commands are generated in your browser only.

Your input is processed in this browser and never sent to a server.

Can't find the tool you need?

Tell us what you want to do. We build the most requested tools first.