This wikiHow will teach you how to open ports in three popular Linux firewalls. If you're using a product like ConfigServer Firewall (CSF) or Advanced Policy Firewall (ADP), you can control open ports in the firewall's main configuration...
Method 1 of 3:
Using Uncomplicated Firewall for Ubuntu

Log in to your server. If you're using Ubuntu on your desktop, press Ctrl+Alt+T to open a terminal window.
Type sudo ufw status verbose and press ↵ Enter. If UFW is already running, you'll see a status message, as well as a list of any firewall rules (including opened ports) that already exist.[1]
Use sudo ufw allow [port number] to open a port. For example, if you want to open the SSH port (22), you'd type kbd and press ↵ Enter to open the port. There's no need to restart the firewall, as the change will take effect immediately.[2]
Delete firewall rules that aren't needed. Any ports that aren't specifically opened are blocked by default. If you open a port and decide you want to close it, use these steps:Method 2 of 3:
Using ConfigServer Firewall

Log in to your server. If you're not logged in as the root user, you can su to root to adjust your configuration.

Go to directory that contains your CSF config file. The file is called csf.conf, and it's saved to /etc/csf/csf.conf by default.[3] To do this, type cd /etc/csf and press ↵ Enter.
Open csf.conf in a text editor. You can use any text editor you wish, such as vim or nano.
Add an incoming port to the TCP_IN list. TCP ports. Once you have the file open, you will see TCP_IN and TCP_OUT sections. The TCP_IN section lists open inbound TCP ports separated by commas. The ports are in numerical order to make things easy, but it's not required that the ports you stick to the order. You can add ports to the end of the sequence, just separate them with commas.
Allow outgoing TCP to the TCP_OUT list. Just as you did with the incoming port, add any outbound TCP ports you'd like to open to the TCP_OUT list.
Save your changes and exit the file. Follow these steps to save and exit the file:
Type service csf restart and press ↵ Enter. This restarts the firewall and opens the new ports.Method 3 of 3:
Using Advanced Policy Firewall

Log in to your server. If you're not logged in as the root user, you can su to root to adjust your configuration.

Go to the directory that contains your APF config file. The file you're looking for is called conf.apf, and it'll be in /etc/apf by default.[4] Type cd /etc/apf to enter that directory.
Open conf.apf in a text editor. You can use any text editor you wish, such as vim or nano.
Add inbound ports to the IG_TCP_CPORTS list. Once you have the file open, you will see IG_TCP_CPORTS and EG_TCP_CPORTS sections. The IG_TCP_CPORTS section lists open inbound ports separated by commas. The ports are listed in numerical order to make things easy, but it's not required to stick with it. You can add ports to the end of the sequence, just separate them with commas.
Allow outbound ports to the EG_TCP_CPORTS list. Just as you did with the incoming port, add any outbound TCP ports you'd like to open to the the EG_TCP_CPORTS list.
Save your changes and exit the file. Follow these steps to save and exit the file:
Type service apf -r and press ↵ Enter. This restarts the APF firewall and opens the new ports.Update 04 March 2020
ncG1vNJzZmismaXArq3KnmWcp51ktbDDjK2mZqegmrtuvM6rq6xlmaN6rbXNrq9mq5Wnw6a%2BjJ%2Bgq52nlrmt