
How to Open a Port in Linux/Ubuntu?
Ports are essential for data communication when a device is connected to a network. Ports differentiate between different network services and it serves as an endpoint of a data communication network. There are 65536 ports among which 1024 ports are reserved for popular network services such as HTTP, HTTPS, SSH, TCP, etc. A port can be opened or closed for incoming/outgoing network traffic for a specific network service. This article will demonstrate the following methods to open a port in Ubuntu 22.04 LTS systems:
- Checking all Open Ports
- Opening a Port.
- Opening a Port to Allow Traffic from a Specific Service.
- Opening a Port to Allow Traffic from an IP Address.
- Closing/Blocking a Port
How to Check all Open Ports in Ubuntu 22.04?
To check all open ports, several utilities can be used such as netstat, ss, etc. netstat enables users to view network-related statistics, such as open ports, network connections, etc. “netstat” is not pre-installed in Ubuntu 22.04. To install “netstat”, first, execute the following command to update system repositories:
$ sudo apt update
Then, run the following command to install netstat:
$ sudo apt install net-tools
The output indicates a successful installation. Next, run the following command to check all open ports:
$ netstat -lntu
Where:
- l: Listening ports
- n: Port Number
- t: TCP ports
- u: UDP ports
How to Open a Port in Ubuntu 22.04?
In Ubuntu 22.04, the default firewall is UFW (Uncomplicated Firewall) and it is used to open ports in Ubuntu systems. UFW is pre-installed in Ubuntu and in order to enable UFW, run the following command:
$ sudo ufw enable
The output indicates that the UFW firewall is active and enabled. In order to open any closed port, use the following syntax:
$ sudo ufw allow <port>
Where:
- ufw: Uncomplicated firewall client.
- <port>: Port to be opened.
In the following subsections, a few examples of opening well-known ports are demonstrated.
How to Open SSH Port (Port 22)?
SSH (Secure Shell) protocol enables secure data communication over insecure networks, for example, secure file transfer, logins, port forwarding, etc. Port 22 is reserved for SSH services. To open the SSH port, execute either of the below-mentioned commands:
$ sudo ufw allow ssh
or
$ sudo ufw allow 22
The output indicates that rules are updated to allow SSH network traffic on port 22.
How to Open HTTP Port (Port 80)?
HTTP (Hypertext Transfer Protocol) is a popular internet protocol for transferring information between connected devices. Port 80 is reserved for HTTP services. To open the HTTP port, execute either of the below-mentioned commands:
$ sudo ufw allow http
or
$ sudo ufw allow 80
The output indicates that rules are added to allow HTTP network traffic on port 80.
How to Open HTTPS Port (Port 443)?
HTTPS (Hypertext Transfer Protocol Secure) is an enhancement of HTTP protocol enabling secure information transfer between connected devices. Port 443 is reserved for HTTPS services. To open the HTTPS port, execute either of the below-mentioned commands:
$ sudo ufw allow https
or
$ sudo ufw allow 443
The output indicates that rules are added to allow HTTPS network traffic on port 443.
How to Open Port 8080?
Port 8080 is a default port for web servers such as Apache Server. To open the port 8080, execute the below-mentioned command:
$ sudo ufw allow 8080
The output indicates that rules are added to allow network traffic on port 8080.
How to Open a Port to Allow Traffic From a Specific Service in Ubuntu 22.04?
To open a port to allow traffic from a specific service, the following syntax is used:
$ sudo ufw allow <port>/<protocol>
Where:
- ufw: Uncomplicated firewall client.
- <port>: Port to be opened.
- <protocol>: Network service/protocol.
For example, to allow TCP (Transmission Control Protocol) traffic on port 80, execute the following command:
$ sudo ufw allow 80/tcp
The output indicates that rules are updated to allow TCP traffic on port 80.
How to Open a Port to Allow Traffic From an IP Address in Ubuntu 22.04?
To allow traffic from a particular IP address, i.e., 127.0.0.10/8 on a certain port for example, on port 21, run the following command:
$ sudo ufw allow from 127.0.0.10/8 to any port 21
Where:
- 127.0.0.10/8: IP Address
- 21: Port
The output indicates that rules are added to allow traffic from IP address 127.0.0.10/8 on port 21.
How to Check the Status of Ports via UFW in Ubuntu 22.04?
To verify the status of ports, execute the following command:
$ sudo ufw status
The output verifies that all the ports opened in the previous sections are enabled to allow incoming network traffic.
How to Close/Block a Port in Ubuntu 22.04?
If in case, there is a need to close/block an opened port, use the following syntax:
$ sudo ufw deny <Port>
For example, to close/block port 22, execute the following command:
$ sudo ufw deny 22
The below output indicates that rules are updated to deny network traffic on port 22, disabling SSH traffic:
Further, it is verified from the status command that port 22 is blocked for network traffic.
Conclusion
In Ubuntu 22.04, a port can be enabled via UFW by executing the “sudo ufw allow <port>” command. Moreover, a specific service on a port can be opened by executing the “sudo ufw allow <port>/ <protocol>” command while network traffic from a particular IP address can be allowed by executing “sudo ufw allow from <ip_address> to any port <port>” command. This article demonstrated methods to open a port on Linux/Ubuntu 22.04 LTS systems.