UFW

Introduction

For an introduction to firewalls, please see Firewall.

UFW - Uncomplicated Firewall

Default Ubuntu Firewall is ufw.

It was developed to simplify iptables configuration, ufw provides a more human friendly way to configure an IPv4 or IPv6 host-based firewall.

UFW is disabled by default.

The command outputs on this page come from Ubuntu 24.04.5 with ufw 0.36.2 in a test container. Each group of examples was run on a firewall without the rules of the groups before it.

Gufw

Gufw is a GUI that is available as a frontend.

Basic Syntax and Examples

Default rules are fine for the average home user

When you turn UFW on, it uses a default set of rules (profile) that should be fine for the average home user. That's at least the goal of the Ubuntu developers. In short, all 'incoming' is being denied, with some exceptions to make things easier for home users.

Enable UFW

To turn UFW on with the default set of rules:

sudo ufw enable

Firewall is active and enabled on system startup

Check Status

To check the status of UFW:

sudo ufw status verbose

Status: active Logging: on (low) Default: deny (incoming), allow (outgoing), deny (routed) New profiles: skip

Note that by default, deny is being applied to incoming. There are exceptions, which can be found in the output of this command:

The output is long. It begins like this:

sudo ufw show raw

IPV4 (raw): Chain INPUT (policy DROP 0 packets, 0 bytes) pkts bytes target prot opt in out source destination 0 0 ufw-before-logging-input 0 -- * * 0.0.0.0/0 0.0.0.0/0 0 0 ufw-before-input 0 -- * * 0.0.0.0/0 0.0.0.0/0 0 0 ufw-after-input 0 -- * * 0.0.0.0/0 0.0.0.0/0 0 0 ufw-after-logging-input 0 -- * * 0.0.0.0/0 0.0.0.0/0 0 0 ufw-reject-input 0 -- * * 0.0.0.0/0 0.0.0.0/0 0 0 ufw-track-input 0 -- * * 0.0.0.0/0 0.0.0.0/0 Chain FORWARD (policy DROP 0 packets, 0 bytes) pkts bytes target prot opt in out source destination 0 0 ufw-before-logging-forward 0 -- * * 0.0.0.0/0 0.0.0.0/0 0 0 ufw-before-forward 0 -- * * 0.0.0.0/0 0.0.0.0/0

You can also read the rules files in /etc/ufw (the files whose names end with .rules).

Disable UFW

To disable ufw use:

sudo ufw disable

Firewall stopped and disabled on system startup

Allow and Deny (specific rules)

Allow

sudo ufw allow <port>/<optional: protocol>

Allow Port

Example: allow incoming tcp and udp packets on port 53

sudo ufw allow 53

Rule added Rule added (v6)

Example: allow incoming tcp packets on port 53

sudo ufw allow 53/tcp

Rule added Rule added (v6)

Example: allow incoming udp packets on port 53

sudo ufw allow 53/udp

Rule added Rule added (v6)

Example: open port 5901 for VNC

sudo ufw allow 5901

Rule added Rule added (v6)

To check the result (here on a firewall that has only this rule):

sudo ufw status verbose

Status: active Logging: on (low) Default: deny (incoming), allow (outgoing), deny (routed) New profiles: skip To Action From -- ------ ---- 5901 ALLOW IN Anywhere 5901 (v6) ALLOW IN Anywhere (v6)

Allow Port Range

To open several consecutive ports, for example for a Docker agent of Jenkins, give the first and the last port separated by a colon, and name the protocol.

sudo ufw allow 32768:60999/tcp

Rule added Rule added (v6)

Without the protocol ufw refuses the range:

sudo ufw allow 32768:60999

ERROR: Must specify 'tcp' or 'udp' with multiple ports

Deny

sudo ufw deny <port>/<optional: protocol>

Example: block tcp and udp packets on port 53

sudo ufw deny 53

Rule added Rule added (v6)

Example: block incoming tcp on port 53

sudo ufw deny 53/tcp

Rule added Rule added (v6)

Example: deny incoming udp packets on port 53

sudo ufw deny 53/udp

Rule added Rule added (v6)

Delete an existing rule

To delete a rule, put delete in front of the original rule. For example, if the original rule was:

sudo ufw deny 80/tcp

Rule added Rule added (v6)

delete it with:

sudo ufw delete deny 80/tcp

Rule deleted Rule deleted (v6)

Services

You can also allow or deny by service name, since ufw reads /etc/services. To see the list of services:

less /etc/services

The lines for SSH, DNS and the web:

grep -E '^(ssh|domain|http|https)[[:space:]]' /etc/services

ssh 22/tcp # SSH Remote Login Protocol domain 53/tcp # Domain Name Server domain 53/udp http 80/tcp www # WorldWideWeb HTTP https 443/tcp # http protocol over TLS/SSL https 443/udp # HTTP/3

Allow by Service Name

sudo ufw allow <service name>

Example: allow ssh by name

sudo ufw allow ssh

Rule added Rule added (v6)

Deny by Service Name

sudo ufw deny <service name>

Example: deny ssh by name. A rule for the same port already exists, so ufw replaces it and answers that the rule was updated.

sudo ufw deny ssh

Rule updated Rule updated (v6)

Check which rules apply

The status shows whether ufw is on and lists the current rules:

sudo ufw status

Status: active To Action From -- ------ ---- 22/tcp DENY Anywhere 22/tcp (v6) DENY Anywhere (v6)

If ufw is not enabled, the output is:

sudo ufw status

Status: inactive

Logging

To enable logging use:

sudo ufw logging on

Logging enabled

To disable logging use:

sudo ufw logging off

Logging disabled

Advanced Syntax

You can also use a fuller syntax, specifying the source and destination addresses, ports and protocols.

Allow Access

This section shows how to allow specific access.

Allow by Specific IP

sudo ufw allow from <ip address>

Example: allow packets from 207.46.232.182:

sudo ufw allow from 207.46.232.182

Rule added

Allow by Subnet

You may use a net mask:

sudo ufw allow from 192.168.1.0/24

Rule added

Allow by specific port and IP address

sudo ufw allow from <source> to <destination> port <port number>

Example: allow IP address 192.168.0.4 access to port 22 for all protocols

sudo ufw allow from 192.168.0.4 to any port 22

Rule added

Allow by specific port, IP address and protocol

sudo ufw allow from <source> to <destination> port <port number> proto <protocol name>

Example: allow IP address 192.168.0.4 access to port 22 using TCP

sudo ufw allow from 192.168.0.4 to any port 22 proto tcp

Rule added

Block ping

Note: security by obscurity may be of very little actual benefit with modern cracker scripts. By default, UFW answers ping requests. You may wish to leave (icmp) ping requests enabled to diagnose networking problems.

The rules for ping are in /etc/ufw/before.rules. The line with ufw-before-input is for pings sent to this machine, the line with ufw-before-forward for pings it forwards:

grep -n 'icmp-type echo-request' /etc/ufw/before.rules

37:-A ufw-before-input -p icmp --icmp-type echo-request -j ACCEPT 43:-A ufw-before-forward -p icmp --icmp-type echo-request -j ACCEPT

To stop answering ping, change ACCEPT to DROP in the ufw-before-input line only. Leave the other ICMP lines as they are: the network needs those messages to report errors.

sudo sed -i '/ufw-before-input .*echo-request/s/ACCEPT/DROP/' /etc/ufw/before.rules

grep -n 'icmp-type echo-request' /etc/ufw/before.rules

37:-A ufw-before-input -p icmp --icmp-type echo-request -j DROP 43:-A ufw-before-forward -p icmp --icmp-type echo-request -j ACCEPT

Load the changed file:

sudo ufw reload

Firewall reloaded

From another machine, before the change:

ping -c 3 172.19.0.2

PING 172.19.0.2 (172.19.0.2) 56(84) bytes of data. 64 bytes from 172.19.0.2: icmp_seq=1 ttl=64 time=0.119 ms 64 bytes from 172.19.0.2: icmp_seq=2 ttl=64 time=0.060 ms 64 bytes from 172.19.0.2: icmp_seq=3 ttl=64 time=0.071 ms --- 172.19.0.2 ping statistics --- 3 packets transmitted, 3 received, 0% packet loss, time 2027ms rtt min/avg/max/mdev = 0.060/0.083/0.119/0.025 ms

And after it:

ping -c 3 172.19.0.2

PING 172.19.0.2 (172.19.0.2) 56(84) bytes of data. --- 172.19.0.2 ping statistics --- 3 packets transmitted, 0 received, 100% packet loss, time 2050ms

Deny Access

Deny by specific IP

sudo ufw deny from <ip address>

Example: block packets from 207.46.232.182:

sudo ufw deny from 207.46.232.182

Rule added

Deny by specific port and IP address

sudo ufw deny from <ip address> to any port <port number>

Example: deny IP address 192.168.0.1 access to port 22 for all protocols

sudo ufw deny from 192.168.0.1 to any port 22

Rule added

Working with numbered rules

Listing rules with a reference number

Use status numbered to show the order and the number of each rule. Here are the three rules of the example below:

sudo ufw status numbered

Status: active To Action From -- ------ ---- [ 1] 22 DENY IN 192.168.0.1 [ 2] 22 DENY IN 192.168.0.7 [ 3] 22/tcp ALLOW IN 192.168.0.0/24

Editing numbered rules

Delete numbered rule

You can delete a rule by its number. This deletes the first rule, and the rules below it move up.

sudo ufw delete 1

Deleting: deny from 192.168.0.1 to any port 22 Proceed with operation (y|n)? y Rule deleted

Insert a rule at a given number

sudo ufw insert 1 deny from 192.168.0.1 to any port 22

Rule inserted

The rule is first again:

sudo ufw status numbered

Status: active To Action From -- ------ ---- [ 1] 22 DENY IN 192.168.0.1 [ 2] 22 DENY IN 192.168.0.7 [ 3] 22/tcp ALLOW IN 192.168.0.0/24

Advanced Example

Scenario: you want to block access to port 22 from 192.168.0.1 and 192.168.0.7, but let all other 192.168.0.x addresses reach port 22 using tcp. Starting from a firewall without rules:

sudo ufw deny from 192.168.0.1 to any port 22

Rule added

sudo ufw deny from 192.168.0.7 to any port 22

Rule added

sudo ufw allow from 192.168.0.0/24 to any port 22 proto tcp

Rule added

This puts the specific rules first and the generic one second. Once a rule matches a packet, the others are not checked, so the specific rules must come first.

Check the order with the status:

sudo ufw status

Status: active To Action From -- ------ ---- 22 DENY 192.168.0.1 22 DENY 192.168.0.7 22/tcp ALLOW 192.168.0.0/24

Scenario change: you want to block 192.168.0.3 from port 22 as well. First remove the generic rule. It must be written exactly as it was added; without the protocol ufw does not find it:

sudo ufw delete allow from 192.168.0.0/24 to any port 22

Could not delete non-existent rule

With the protocol it is deleted:

sudo ufw delete allow from 192.168.0.0/24 to any port 22 proto tcp

Rule deleted

sudo ufw status

Status: active To Action From -- ------ ---- 22 DENY 192.168.0.1 22 DENY 192.168.0.7

Add the new deny rule, then the generic rule again:

sudo ufw deny from 192.168.0.3 to any port 22

Rule added

sudo ufw allow from 192.168.0.0/24 to any port 22 proto tcp

Rule added

sudo ufw status

Status: active To Action From -- ------ ---- 22 DENY 192.168.0.1 22 DENY 192.168.0.7 22 DENY 192.168.0.3 22/tcp ALLOW 192.168.0.0/24

If you only added the deny rule, it would go below the allow rule, and the allow rule would apply to 192.168.0.3 first.

Reading the log entries

When logging is on, every logged packet becomes one line. On Ubuntu these lines are collected in /var/log/ufw.log. To watch new lines arrive:

sudo tail -f /var/log/ufw.log

To list only the packets that were blocked:

sudo grep "UFW BLOCK" /var/log/ufw.log

The messages come from the kernel, so they are also in the journal:

sudo journalctl -k | grep "UFW "

Two example lines, one for a UDP packet and one for a TCP packet. The addresses are reserved for documentation:

Feb 4 23:33:37 hostname kernel: [ 3529.289825] [UFW BLOCK] IN=eth0 OUT= MAC=00:11:22:33:44:55:66:77:88:99:aa:bb:08:00 SRC=203.0.113.53 DST=192.0.2.10 LEN=103 TOS=0x00 PREC=0x00 TTL=52 ID=0 DF PROTO=UDP SPT=53 DPT=36427 LEN=83

Feb 4 23:34:02 hostname kernel: [ 3554.110342] [UFW BLOCK] IN=eth0 OUT= MAC=00:11:22:33:44:55:66:77:88:99:aa:bb:08:00 SRC=198.51.100.7 DST=192.0.2.10 LEN=60 TOS=0x00 PREC=0x00 TTL=49 ID=54321 DF PROTO=TCP SPT=51514 DPT=23 WINDOW=64240 RES=0x00 SYN URGP=0

Read a line from the verdict in square brackets, then SRC (who sent the packet), PROTO and DPT (which service it tried to reach). The first line is a blocked UDP packet from port 53, the port of a DNS server. The second is a blocked attempt to open a TCP connection to port 23, used by Telnet.

FieldMeaning
Feb 4 23:33:37when the line was written. The date format differs between Ubuntu releases
hostnamethe name of the machine that wrote the line
[ 3529.289825]the kernel timestamp: seconds since the machine booted
[UFW BLOCK]what happened to the packet: BLOCK it was stopped, ALLOW it was let through, LIMIT BLOCK a ufw limit rule stopped it, AUDIT it is only recorded, at the higher logging levels
INthe network interface the packet arrived on. Empty for a packet that this machine sends
OUTthe interface the packet leaves by. Empty for a packet addressed to this machine. When both IN and OUT have a value, the packet is being forwarded
MAC14 bytes from the Ethernet header: the destination MAC address (the first 6), the source MAC address (the next 6) and the type of the content (the last 2, 08:00 is IPv4)
SRCthe IP address of the sender. It can be forged, most easily with UDP. whois shows who an address belongs to
DSTthe IP address the packet was sent to
LEN (first)the length of the whole IP packet in bytes, headers included
TOS, PRECtwo parts of the type of service byte of the IPv4 header, which asks for a priority. 0x00 means no special treatment
TTLtime to live: how many routers the packet may still pass. Each router subtracts 1, and a packet that reaches 0 is discarded
IDthe identification number of the IP header, used to put a fragmented packet back together. It is not a number given by ufw
DFthe do not fragment flag. The word appears only when the flag is set
PROTOthe protocol, for example TCP, UDP or ICMP. An ICMP line has TYPE and CODE instead of ports
SPTthe source port: the port on the sender
DPTthe destination port: the port on the receiving side, which tells the service the sender tried to reach
LEN (second, UDP)the length of the UDP header and data. In the first line 83 bytes is the 103 bytes of the packet without its 20 byte IP header
WINDOWTCP only: how many bytes the sender is ready to receive
RESTCP only: reserved bits of the header, normally 0x00
SYNTCP only: a flag of the packet. SYN alone asks to open a new connection. Other flags, such as ACK, FIN and RST, are written the same way
URGPTCP only: the urgent pointer, normally 0

An IPv6 line has TC, HOPLIMIT and FLOWLBL where IPv4 has the type of service, TTL and ID.

Article author: Andrei Olegovich