How to Open a Port or Ports in Ubuntu (Linux) Using IP Tables

Tiempo de lectura: 2 minutos

Reading time: 1 minute

Ubuntu

When deploying a service or server on a machine that uses Ubuntu, we often face the issue of not being able to connect to the site. Whether it’s an apache2 (which works on port 80 by default) or any other server with a different port.

First, we can check if the service is running on the expected port by using the following command:

netstat -tulnp  grep:80

In this case, we use grep:80 to determine which service is listening on port 80. If you want to use a different port, you will need to replace it with the desired port number:

netstat -tulnp  grep:PORT_NUMBER

After executing the command, it will return the service that is listening on the specified port in PORT_NUMBER.

Now, to allow connections through that port, we need to execute the following command:

iptables -I INPUT -p tcp --dport 80 -j ACCEPT

It is possible that the indicated commands require the use of sudo in some cases.

This command allows TCP traffic through port 80 and adds the rule to iptables.

A word of caution, the command I provided is not persistent and the rule only applies until the machine is restarted. In future posts, I will explain how to make these rules persistent.

If you want to specify another port, simply change “80” to any other port of your choice:

iptables -I INPUT -p tcp --dport PORT_NUMBER -j ACCEPT

Replace PORT_NUMBER with the desired port number that you want to expose.

And that’s all for today’s tutorial.

Leave a Comment