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.
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 itYou can check the firewall status with:
# ufw status
// inactive
Status: inactive
// after enabling
Status: active
... // additional rule outputThe default policy is to allow all traffic.
# ufw default allow|deny // set default rule
allow : allow
deny : denyProtocol 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 : commentExample: add a rule allowing SSH (port 22, TCP) at position 2:
# ufw insert 2 allow in 22/tcpDisable inbound SSH:
# ufw deny in 22IP 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 22Example: 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 8080Delete 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 ruleMethod 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 AnywhereDelete the first rule:
# ufw delete 1Recommended Settings
# ufw enable
# ufw allow ssh
# ufw default deny
# ... you can add further custom rulesFor 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
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
