How to Open a Port in Ubuntu Firewall

In This UFW Tutorial We are going to Learn How to open a port in Ubuntu Firewall.

ufw allow command use to open port in Ubuntu Firewall. By default, if you did not specify the protocol, the port will open for both TCP and UDP protocols.

ufw allow port-number/protocol

Examples

ufw allow 53

The Above rule will open both TCP and UDP port 53 to All networks.

ufw allow 22/tcp

This rule will open TCP port 22 to all networks.

How to Open a Port in Ubuntu Firewall

We can delete firewall rules using ufw delete command

ufw delete allow 22/tcp

Open Port By Service Name in Ubuntu Firewall

It is also possible to open port by service name instead of the port number.

ufw allow ssh

This rule will Allow ssh protocol (which use TCP port 22 by default) from the Ubuntu Firewall.

ufw will check /etc/services file for the corresponding port if we specify the protocol by service name instead of the port number.

Open Port to Specific IP Address in UFW

Rather than opening a port to everyone, UFW allows to open ports to specific IP Address using following format.

ufw allow from <Remote IP> to <Local IP> port <Port number> proto <Protocol>

Examples

ufw allow from 192.168.1.50 to any port 53

Allow Access from IP Address 192.168.1.50 on Both TCP and UDP Port 53 (Since Protocol not specified).

ubuntu firewall open port ip address

ufw allow from 192.168.1.50 to any port 22 proto tcp

This rule open TCP port 22 to remote IP Address 192.168.1.50 from the Ubuntu Firewall.

ufw allow from 192.168.1.50 to 192.168.1.200 port 22 proto tcp

This firewall rule will open port 22 to the IP Address 192.168.1.50, But Connection can only establish through local IP Address (192.168.1.200). This is useful if you have configured more than one IP Address on your Ubuntu Server.

Open Port to a Network

Using subnet mask we can open network port to Entire network or IP range.

ufw allow from 192.168.1.0/24 to any port 22 proto tcp

This Firewall Rule will open port TCP port 22 to entire 192.168.1.0 network.

Open Multiple Ports from single firewall rule

We can open multiple port by separating ports using comma or Port range using Colon.

ufw allow from any to any proto tcp port 21,22

Open Port 21 and 22 from the Ubuntu Firewall.

ufw allow from any to any proto tcp port 8080:8090

This firewall rule will open TCP port 8080 to 8090 from the Ubuntu Firewall.

More Firewall rules Example

ufw allow 80/tcp

Open http port 80 to anyone from the Ubuntu Firewall.

ufw allow 21/tcp

Open FTP port 21 from the firewall.

ufw allow from 192.168.1.10 to any port 21 proto tcp

Open FTP port 21 to IP Address 192.168.1.10

Summary - What We Learned

In this UFW Tutorial We learned How to open port from Ubuntu Firewall using ufw allow command.

What Next ?

In the Next tutorial We will learn how to block network port in Ubuntu UFW Firewall.