UFW commands cheat sheet
Installing and enabling UFW
-
sudo apt install ufwInstalls UFW on Ubuntu and Debian. UFW is the default firewall configuration tool on Ubuntu, and after installation it stays disabled until you enable it.
-
sudo ufw allow sshAllows incoming SSH connections on port 22. Run it before enabling the firewall for the first time, so your connection to the remote server isn't blocked.
-
sudo ufw allow 2222/tcpAllows SSH when the SSH service is set to a different port, here 2222. Add this rule before enabling the firewall as well.
-
sudo ufw enableEnables the firewall and sets it to start at boot. Caution: enabling flushes and rebuilds the rule chains and may drop existing connections, so allow SSH first. When run over SSH, ufw asks for confirmation.
-
sudo ufw --force enableEnables the firewall without the confirmation prompt, for example in a setup script. Caution: run it only after the SSH rule is already in place.
-
sudo ufw --dry-run allow httpShows the changes the command would make, without applying them.
-
sudo ufw reloadReloads the firewall and its rules, for example after editing the configuration files by hand.
-
sudo ufw disableDisables the firewall and stops it from starting at boot. The rules are kept and take effect again the next time you enable it.
-
ufw --versionShows the installed UFW version.
Firewall status and viewing rules
-
sudo ufw statusShows whether the firewall is active and lists the rules. Anywhere in the output means any address, on IPv4 and IPv6.
Example:
Status: active -
sudo ufw status verboseShows the rules with extra details, such as the default policies and the logging level.
-
sudo ufw status numberedShows the rules with a number for each rule. The numbers are used to delete a rule and to insert a rule at a specific position.
-
sudo ufw show addedShows the rules you added, in ufw command syntax. It doesn't show the firewall status; use ufw status for that.
-
sudo ufw show listeningShows the ports the server listens on and the processes listening on them, so you can check which services need a rule to open them.
-
sudo ufw show rawShows all the firewall rules at the iptables and ip6tables level, including rules that aren't managed with ufw commands.
Default policies
-
sudo ufw default deny incomingBlocks all incoming traffic that no explicit rule allows. This is the setting after installation and the basis of a server firewall.
-
sudo ufw default allow outgoingLets the server start any outgoing traffic, for example system updates and requests to external services. This is the setting after installation.
-
sudo ufw default deny routedBlocks traffic routed through the server (forwarded), for example when the server acts as a router or a VPN gateway.
-
sudo ufw default reject incomingRejects incoming traffic that isn't allowed and tells the sender the connection was refused, instead of ignoring it. Caution: existing rules aren't adjusted to the new policy automatically, and you have to update them by hand.
Opening and blocking ports
-
sudo ufw allow 80/tcpOpens TCP port 80 to incoming traffic, for a website over HTTP. Without a direction, the rule applies to incoming traffic.
-
sudo ufw allow 443/tcpOpens TCP port 443, for a website over HTTPS.
-
sudo ufw allow proto tcp from any to any port 80,443Opens several ports in one rule. Write the port list without spaces; one rule can list up to 15 ports.
-
sudo ufw allow 6000:6007/tcpOpens a port range, including the first and last ports. A port range requires the protocol, tcp or udp.
-
sudo ufw allow 53Opens port 53 on both TCP and UDP, because no protocol is given.
-
sudo ufw allow httpOpens a port by service name. ufw looks up the port number in /etc/services, and http is port 80.
-
sudo ufw deny 23/tcpBlocks port 23 (Telnet) with an explicit rule. When the default policy is deny, a port that wasn't opened is blocked anyway, and an explicit deny rule is mainly needed to override a broader allow rule.
-
sudo ufw reject out smtpRejects outgoing connections from the server to port 25 (SMTP). Outgoing rules are written with the word out.
-
sudo ufw allow in on eth0 to any port 80 proto tcpOpens port 80 only on the eth0 network interface. The ip addr command shows the interface names on the server.
Rules by IP address and subnet
-
sudo ufw allow from 203.0.113.10Allows all incoming traffic from one IP address, to all ports.
-
sudo ufw allow proto tcp from 203.0.113.10 to any port 22Allows SSH only from one IP address. Once the general rule for port 22 is deleted, only this address can connect to the server over SSH.
-
sudo ufw allow proto tcp from 192.168.1.0/24 to any port 3306Allows access to port 3306 (MySQL and MariaDB) only from an internal network in CIDR notation.
-
sudo ufw deny from 198.51.100.7Blocks all traffic from one IP address. A new rule is added at the end of the list, so if an earlier rule allows the same traffic, the block won't take effect.
-
sudo ufw insert 1 deny from 198.51.100.7Adds the deny rule first in the list, so it's checked before the allow rules. In the firewall, the first rule that matches a packet decides.
-
sudo ufw prepend deny from 198.51.100.7Adds the rule at the top of the list, before all rules of the same address type (IPv4 or IPv6).
-
sudo ufw deny proto tcp from 2001:db8::/32 to any port 25Blocks an IPv6 address range on a specific port. The syntax is the same as for IPv4.
Rate limiting (limit)
-
sudo ufw limit ssh/tcpAllows SSH but blocks an IP address that tried to open 6 or more connections within 30 seconds. This protects against repeated password-guessing attempts.
-
sudo ufw limit 2222/tcp comment 'SSH port'Rate limiting for an SSH service on a different port, here 2222, with a comment that identifies the rule in the list.
Application profiles
-
sudo ufw app listLists the available application profiles. A profile defines a service's ports, and the profiles are in the /etc/ufw/applications.d directory.
-
sudo ufw app info allShows the details of all profiles, including the ports each profile opens. Instead of all, you can give the name of one profile from the list.
-
sudo ufw allow SambaOpens all the ports defined in the profile. Samba is an example name; take the exact name from ufw app list. With a profile name, you don't give a protocol.
-
sudo ufw allow from 192.168.0.0/24 to any app SambaOpens the profile's ports only to a specific network.
Deleting, inserting, and comments
-
sudo ufw delete 3Deletes rule number 3 in the ufw status numbered list. The numbering changes after every deletion, so list the rules again before deleting another one.
-
sudo ufw delete allow 80/tcpDeletes a rule by its original wording, with the word delete in front. When IPv6 is enabled, deleting by wording removes the rule for both IPv4 and IPv6, while deleting by number removes only one of them.
-
sudo ufw insert 1 allow 80Adds a rule at a specific position in the list, here first. Useful when order matters, because the first matching rule is the one applied.
-
sudo ufw allow 443/tcp comment 'HTTPS'Adds a comment to a rule, shown in ufw status. Running the same rule with a different comment updates it, and an empty comment ('') removes it.
Deny vs. reject
-
sudo ufw deny 8080/tcpdeny silently drops the packet, and the sender gets no reply. The connection on the other side waits until it times out.
-
sudo ufw reject 8080/tcpreject refuses the packet and tells the sender the connection was refused, so the other side gets an immediate error instead of waiting.
Logging
-
sudo ufw logging onTurns on logging at the low level, which is the default: packets blocked by the policy and packets matching rules with logging.
-
sudo ufw logging mediumThe medium level also logs packets allowed by rules, invalid packets, and new connections, with rate limiting on the log.
-
sudo ufw logging highThe high level removes the rate limiting of the medium level and also logs all other packets, with rate limiting. Caution: at the high and full levels, the log grows fast and may fill the disk.
-
sudo ufw logging offTurns off UFW logging.
-
sudo ufw allow log 22/tcpAdds a rule that logs every new connection matching it. log-all instead of log logs every packet that matches the rule.
Reset, IPv6, and routing
-
sudo ufw resetDisables the firewall and returns it to the installation defaults. Caution: all rules are deleted, so add the SSH rule again before enabling it.
-
IPV6=yesThe setting in /etc/default/ufw that controls whether UFW also manages IPv6 rules. The default is yes, and rules without an address then apply to both IPv4 and IPv6. With no, IPv6 traffic is accepted only on the local interface (loopback). After a change, reload with sudo ufw reload.
-
sudo ufw route allow in on eth1 out on eth2Allows routing traffic from the eth1 interface to the eth2 interface. Routing rules are written with the word route and require IP forwarding on the server.
-
net/ipv4/ip_forward=1The line in /etc/ufw/sysctl.conf that turns on IPv4 forwarding, once you remove the comment mark at its start. After the change, run sudo ufw disable and then sudo ufw enable.
Docker and UFW
-
docker run -p 127.0.0.1:8080:80 -p '[::1]:8080:80' nginxAccording to the Docker documentation, Docker and UFW rules are incompatible: traffic to a port published from a container is routed before the UFW rules are checked, so the port is open to the network even when no rule allows it. Publishing on 127.0.0.1 and ::1 limits access to the server itself, for example behind a proxy server.
No matching commands.
Before enabling a firewall on a remote server
- SSH first: add a rule that allows SSH, on the port the service actually uses, and only then enable the firewall. Enabling rebuilds the rule chains, and without this rule your connection to the server may be blocked.
- Keep a session open: after changing rules, keep your current SSH session open and test logging in from a new window. This way, you can fix a mistake without losing access to the server.
- Order matters: rules are checked in order, and the first rule that matches a packet decides. Add a specific rule, such as blocking one address, before a general rule with insert or prepend.
- Check what's listening: before opening a port, check with sudo ufw show listening which services are listening on the server, and open only the ports that need to be reachable from outside.
The IP addresses on this page, such as 203.0.113.10 and 198.51.100.7, belong to ranges reserved for documentation; replace them with your real addresses. Network notation such as 192.168.1.0/24 is explained in the CIDR table, and the private and reserved ranges are listed on the reserved IP ranges page. Commands for connecting to a server and for securing the SSH service are on the SSH commands page.
On a Linux VPS, you connect over SSH with full root access and configure the server's firewall with every command on this page: default policies, opening ports, rules by IP address, rate limiting, and logs.
UFW FAQ
How do I open a port in the UFW firewall?
Run sudo ufw allow with the port number and protocol, for example sudo ufw allow 443/tcp for a website over HTTPS. Without a protocol, the port opens on both TCP and UDP, and you can open several ports in one rule, for example sudo ufw allow proto tcp from any to any port 80,443. When you're done, check the new rule with sudo ufw status.
How do I enable UFW on a remote server without losing my connection?
Before enabling it, add a rule that allows SSH, with sudo ufw allow ssh or with the SSH service's port number if it isn't 22, and only then run sudo ufw enable. We recommend keeping your current session open and testing a login from a new window. This way, even if something isn't set up right, you can fix it from the open session.
How do I allow SSH access only from a specific IP address?
Add a rule for the address, for example sudo ufw allow proto tcp from 203.0.113.10 to any port 22, and then delete the general rule for port 22 with sudo ufw delete allow ssh or by its number in sudo ufw status numbered. First make sure the address you connect from is static, so access to the server stays available.
What does the ufw limit command do?
The ufw limit command allows connections to a port but blocks an IP address that tried to open 6 or more connections within 30 seconds. It's especially suited to SSH, because it slows down repeated password-guessing attempts, and you use it instead of a regular allow rule, for example sudo ufw limit ssh/tcp.
What's the difference between deny and reject in UFW?
Both commands block the traffic. deny silently drops the packet, and the other side waits until it times out, while reject sends the sender a message that the connection was refused. Most servers use deny as the default policy for incoming traffic, and reject is useful when you want legitimate clients to get an immediate error instead of waiting.