Master Ubuntu’s UFW: Simple Commands to Enable, Configure, and Manage Firewall Rules

Learn how to quickly set up and manage Ubuntu’s Uncomplicated Firewall (UFW) with step‑by‑step commands for enabling, disabling, resetting, configuring default policies, adding protocol, IP, and port rules, and deleting rules, empowering you to secure client machines efficiently.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Master Ubuntu’s UFW: Simple Commands to Enable, Configure, and Manage Firewall Rules

Wiki

UFW (Uncomplicated Firewall) is the default firewall component on Ubuntu, designed to simplify iptables configuration. It provides a friendly interface for creating IPv4/IPv6 firewall rules.

Enable and Disable

# ufw enable // enable firewall
# ufw disable // disable firewall
# ufw reset // reset firewall, removing all rules and disabling it

You can check the firewall status with:

# ufw status
// inactive
Status: inactive

// after enabling
Status: active
... // additional rule output

The default policy is to allow all traffic.

# ufw default allow|deny  // set default rule
allow : allow
deny : deny

Protocol Rules

Protocol rules define firewall behavior based on protocols.

ufw [delete] [insert NUM] allow|deny [in|out] [PORT[/PROTOCOL]] [comment COMMENT]

delete : delete the rule
insert : insert at position NUM
allow|deny : allow or deny the rule
in|out : direction of traffic
PORT : port number
protocol : e.g., tcp or udp
comment : comment

Example: add a rule allowing SSH (port 22, TCP) at position 2:

# ufw insert 2 allow in 22/tcp

Disable inbound SSH:

# ufw deny in 22

IP Rules

IP rules can include ports and protocols.

ufw [delete] [insert NUM] allow|deny [in|out [on INTERFACE]] [proto PROTOCOL] [from ADDRESS [port PORT]] [to ADDRESS [port PORT]] [comment COMMENT]

INTERFACE : network interface
from ADDRESS : source IP
to ADDRESS : destination IP
PORT : port number (source or destination)

Example: allow TCP port 22 from 192.168.0.2:

# ufw allow proto tcp from 192.168.0.2 port 22

Example: allow traffic from 192.168.0.2:80 to 192.168.0.2:8080:

# ufw allow from 192.168.0.2 port 80 to 192.168.0.2 port 8080

Delete Rules

Rules can be deleted by content or by number.

Method 1

Prepend delete to the original rule command.

# ufw allow 22/tcp // add SSH rule
# ufw delete allow 22/tcp // delete SSH rule

Method 2

Use ufw status numbered to view rule numbers, then delete by number.

# ufw status numbered
Status: active

     To                         Action      From
     --                         ------      ----
[ 1] 22                         ALLOW IN    Anywhere

Delete the first rule:

# ufw delete 1

Recommended Settings

# ufw enable
# ufw allow ssh
# ufw default deny
# ... you can add further custom rules

For deeper understanding, refer to the official documentation; UFW is a wrapper around iptables, which works on all Linux distributions.

Link: https://www.cnblogs.com/Johnson-lin/p/11261338.html

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

LinuxUbuntuufw
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.