Allow a port through the firewall with ufw

Add a UFW rule to permit inbound traffic on a TCP or UDP port, such as SSH on port 22.

LinuxnetworksecurityHigh risk
Run sudo ufw allow 22/tcp to permit inbound SSH, then check the rule with sudo ufw status verbose.

01Command

Command
$ sudo ufw allow 22/tcp

02What it does

Rules accept port/protocol (80/tcp), service names from /etc/services (ufw allow ssh), or app profiles (ufw allow 'Nginx Full'); omit the protocol to allow both TCP and UDP. By default an allow rule opens the port on all interfaces — restrict it to a subnet with ufw allow from 192.168.1.0/24 to any port 22. Order matters: first matching rule wins, and ufw status numbered plus ufw delete <n> lets you remove by position. Verify with sudo ufw status verbose; only rules while ufw is enabled are enforced.

03Risk

RiskHigh risk
RequirementsUFW installed and enabled (sudo ufw enable); on Ubuntu it is preinstalled but inactive by default
!Warning: Enable UFW only after adding a rule for your active SSH session — on a remote server, enabling a firewall without an allow rule locks you out immediately.

04Undo

Undo
$ sudo ufw delete allow 22/tcp

05Sources

Last verified: 2026-09-17 · Category: Security & Privacy

06Related solutions