Ubuntu 22.04 - Firewall

Default setup

Set up:

# ufw status
# ufw default allow outgoing
# ufw default deny incoming
# ufw allow ssh

Make sure IPv6 is enable and if not, edit configuration:

# grep IPV6 /etc/default/ufw

# vim /etc/default/ufw

Enable and check status:

# ufw enable
# ufw status

Adding custom ports/services

Adding custom tcp/udp ports:

# ufw allow 80/tcp comment 'Allow Apache HTTP'
# ufw allow 443/tcp comment 'Allow Nginx HTTPS'
# ufw allow 41194/udp comment 'Allow WireGuard VPN'

Adding port ranges:

# ufw allow 4000:4200/tcp
# ufw allow 6000:7000/udp

Allow ALL connections from specific IP address:

# ufw allow from 1.2.3.4

Allow specific ports from specific IP address:

# ufw allow from 1.2.3.4 to any port 25 proto tcp

Allow specific ports from specific IP address TO specific IP address:

# ufw allow from 1.2.3.4 to 5.6.7.8 port 25 proto tcp

More info: https://www.cyberciti.biz/faq/ubuntu-22-04-lts-set-up-ufw-firewall-in-5-minutes/