中文 English

Open Specific Ports on Ubuntu 22.04 with UFW or iptables

Published: 2023-02-26
Ubuntu iptables Firewall GFW FuckGFW

Add firewall rules on Ubuntu 22.04

Check firewall status

ufw status

Open port 22

ufw allow 22

Check firewall status again

ufw status

Open port 8888 and allow TCP only

ufw allow 8888/tcp

Check firewall status again

ufw status

Show numbered firewall rules

ufw status numbered

Delete rule number 3

ufw delete 3

Check the numbered rule list again

ufw status numbered

Deprecated method: use iptables

In most cases, iptables is already installed on Ubuntu. If not, install it first.

  1. Install it Run the following command:
sudo apt-get install iptables
  1. Add a rule Run the following command:
iptables -I INPUT -p tcp --dport 80 -j ACCEPT

Replace 80 with the port you want to open.

  1. Save the rule Run the following command:
iptables-save

At this point the port is open, but the rule will disappear after a reboot, so persistence is still needed.

  1. Persist the rule Install a helper package. Here I use iptables-persistent.
  1. Install iptables-persistent
sudo apt-get install iptables-persistent
  1. Save the rules persistently
sudo netfilter-persistent save
sudo netfilter-persistent reload

After that, the required port will remain open permanently.