February 1, 2023

How To Open A Port In Ubuntu 20.04

Abhishek Thakur


In Ubuntu or any OS applications which implement common services often use  port numbers to define it. The process applied is called listening . Which involves receiving and transferring requests for the communication of services. Ports allows a computer to easily distinguish between different types of network traffics. For example, email goes to a different port than web pages, even though they both reach the same computer through the same Internet connection. This brings us to firewall, which is a security system that blocks or allows network traffic based on a set of security rules. Firewalls often lie between trusted and untrusted networks. Often the unreliable network is the Internet. For example, office networks often use firewalls to protect their networks from online threats. In this guide we will explain how to open a port in Ubuntu 20.04 and use networking tools to list and check open ports.

Requirements

Steps To Open A Port In Ubuntu 20.04 With UFW

The correct procedure for opening a port in Ubuntu 20.04 depends upon UFW(Uncomplicated Firewall) or the iptable utility. Uncomplicated Firewall (UFW) is a network firewall management program designed to be easy to use. It uses a command line interface that includes a small number of simple commands and uses iptables for configuration. UFW is available by default in all Ubuntu installations since 8.04 LTS. However, if there is a case that your Ubuntu installation does not have UFW. To check that you can run the given command in the terminal, this will verify the presence of UFW.

# ufw status

If the output returns the given, it means UFW is not installed for the system or it may have been removed.

 -bash: ufw: command not found

If that is the case you can install the UFW by running the given command. The whole idea of UFW is that it makes the port configuration much easy.

# apt-get install ufw -y

After installing UFW you have to enable the app for running. Before enabling UFW it’s better to add the SSH port, or it will block the SSH connection to the server. Run the given commands to enable and allow the SSH port for the system. ufw allow portnumber”

# ufw allow 22

# ufw enable

After opening the port 22 for incoming and outgoing traffics you can enable the UFW without any errors . To verify the process run the given command and it will output the list of open ports.

# ufw status verbose

Open a Port

Steps To Open A Port In Ubuntu 20.04 Without UFW

Mangaing the ports for a Ubuntu system with UFW is the recommended way for system security. However there are system which still uses the very old iptable solution to communicate the traffic using the Linux kernel firewall which removes the dependency of apps like UFW.

Run the given command in terminal to add an ipv4 rule for opening a port.

# iptables -A INPUT -p tcp –dport 22 -j ACCEPT

Similary for ipv6 rule you run the given command.

# ip6tables -A INPUT -p tcp –dport 22 -j ACCEPT

The rules created with iptables does not persist after a system reboot. Now we have to install the iptables-persistent package to make these rules exist even after a restart or shutdown. Run the given command to install the iptables-persistent package. Automatically the installation will save the current deployed ipv4 and ipv6 rules to a file to trigger them after a restart in order to make it persistent.

# apt install iptables-persistent

To check the staus of all table rules, run the given command. This will generate a long output for each protocol in iptables. In the list you will also see the ports which we have added.

# iptables -L -v -n | more

Chain ufw-user-input (1 references)
pkts bytes target prot opt in  out        source    destination
14    672  ACCEPT tcp  --  * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:22
0      0   ACCEPT udp  --  * * 0.0.0.0/0 0.0.0.0/0 udp dpt:22

Checking The Open Ports

After using any of the given methods to open a port in Ubuntu 20.04, make sure the process was successful.  To do so you can use any program like UFW or iptables, however too make thing simpler you can use the netstat command.  The given command will show you the list of running process with its active listening port.

# netstat -lntu

How To Open a Port In Ubuntu

Conclusion

There are various tools that are available to monitor and configure ports on your server. In this tutorial we showed how to open a port on Ubuntu 20.04 server server using different command line tools and utility like UFW, iptables, and netstat. If a port is in the listening state and is allowed through the firewall, then that port is ready for incoming connections. But it also depends on your router or other network devices between your server and the VPS provider, as they may have their own rules blocking incoming and outgoing connections.


Buy Dedicated Root Server @ $89/m Only

Popular Blog Posts