How to Open and Close a Port in Ubuntu?

The previous article explained how to open and close ports in RockyLinux/AlmaLinux/CentOS. This article will explain how to open and close a port in Ubuntu.

 

Problem

How to open and close a port in Ubuntu?

 

Solution

A. Check the firewall

By default, Ubuntu and Debian use the UFW or Uncomplicated Firewall application as the default firewall, and it is installed automatically when you install Ubuntu/Debian. If the firewall is not installed on your Ubuntu/Debian distro, use the command below:

sudo apt install ufw

 

To see whether ufw is running or not, use the command below:

sudo ufw status

Check status ufw

 

From the image above, you can see that the application is not yet active. To enable it, type the command below:

sudo ufw enable

Enable ufw

 

If you want to see the complete current status of the firewall, use the command below:

sudo ufw status verbose

Display the complete current status of the firewall

 

By default, the firewall only opens the OpenSSH service, which you can view by using the command below:

sudo ufw app list

Display the service that is open in the firewall

 

B. Open the port

To open a port, for example, port 43210, use the command below:

sudo ufw allow 43210

Open the port

 

WARNING
If you open the port using the command above, it means you will open the port for both TCP and UDP.

 

To open a port range, for example, from port numbers 45000 to 45010 with the TCP protocol, use the command below:

sudo ufw allow 45000:45010/tcp

Open the range ports

C. Open the service

You can see from the image above that port 43210 has been opened on your Ubuntu server. You can also use the service name when opening a port. For example, if you want to open the SMTP service on your Ubuntu server, then use the command below:

sudo ufw allow smtp

Open the SMTP service

 

D. Open the port from a certain IP

If you want to open a port from a certain IP, for example, you  only allow IP 192.168.56.1 to access port 22 on this server, then use the command below:

sudo ufw allow from 192.168.56.1 to any port 22

Open and Close a port in Ubuntu
Allow the IP to a certain port

 

To allow the 192.168.56.0 subnet to the SMTP service, use the command below:

sudo ufw allow from 192.168.56.0/24 to any port 25

Open and Close a port in Ubuntu
Allow the subnet to a certain port

 

E. Close the port

To close port 25, use the command below:

sudo ufw deny 25

Open and Close a port in Ubuntu
Close the port

 

F. Delete the port

You can also close a port and delete the port that has been opened, for example, port 43210, using the syntax below:

sudo ufw delete number

Open and Close a port in Ubuntu
Close and delete the port

 

WARNING
You don’t need to run sudo ufw reload after each rule change using ufw commands (such as ufw allow or ufw deny). However, you will need to run sudo ufw reload if you are editing the ufw configuration file manually (such as /etc/ufw/before.rules or /etc/ufw/after.rules), or if you want to make sure all the latest rules and settings are loaded.

 

Note

You can remove all the rules in ufw by using the command below:

sudo ufw reset

 

After that, enable the ufw by using the command below:

sudo ufw enable

 

Reset ufw

 

By default, if you open a port, it will automatically open in IPv4 and IPv6, and likewise, if you close the port. To see the UFW settings, open the /etc/default/ufw file.

Open and Close a port in Ubuntu
Configuration of ufw

 

References

cyberciti.biz
phoenixnap.com
digitalocean.com
help.ubuntu.com
askubuntu.com