Open Specific Ports on Ubuntu 22.04 with UFW or iptables
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.
- Install it Run the following command:
sudo apt-get install iptables
- 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.
- 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.
- Persist the rule
Install a helper package. Here I use
iptables-persistent.
- Install
iptables-persistent
sudo apt-get install iptables-persistent
- Save the rules persistently
sudo netfilter-persistent save
sudo netfilter-persistent reload
After that, the required port will remain open permanently.