how to open port in linux

How To Open Port in Linux

Sometimes you may need to open port in Linux or enable port in Linux firewall to run specific applications. In this article, we will look at how to open port in Linux. Since different Linux systems have different kinds of firewall systems, we will look at how to enable port in Ubuntu, Redhat and other systems.

How To Open Port in Linux

Here are the steps to open port in Linux.

1. List open ports in Linux

Before we open a port in Linux, it is good to list all open ports first. Open terminal and run the following command to list all open ports in Linux. We will use netstat command to display all open ports – both TCP as well as UDP ones.

$ sudo netstat -lntu

In the above command, the different options mean the following

  • l – list all ports that are listening
  • n – show port number
  • t – include tcp ports
  • u – include udp ports

You can also use the ss command to get the same output

$ sudo ss -lntu

Also read : How to Create Remote Git Repository

2. Open port in Linux

Different Linux distros use different firewall systems. Here is the command to open port, depending on your system.

Ubuntu/Debian systems

Ubuntu/Debian systems use ufw based firewalls. Here is the command to open http port 80.

$ sudo ufw allow 80

The above command will enable port 80 and ensure that it remains open even when you reboot the system.

Also read : How to Enable Keep Alive in NGINX

Redhat/Suse/Fedora/CentOS systems

These systems use firewalld based firewalls that can be opened using firewall command. Here is the command to open 80 port.

$ sudo firewall-cmd --add-port=80/tcp --permanent

Please remember to add –permanent option to ensure that the port remains open even after reboot.

Also read : How to Redirect 403 to 404 in Apache

You might also use iptables command to open port 80

$ sudo iptables -A INPUT -p tcp --dport 80 -j ACCEPT

However, iptables are reset on reboot so you need to save them using iptables-save command

Debian and Ubuntu Linux user type:

$ sudo /sbin/iptables-save > /etc/iptables/rules.v4
$ sudo /sbin/ip6tables-save > /etc/iptables/rules.v6

CentOS/RHEL systems:

$ sudo /sbin/iptables-save > /etc/sysconfig/iptables
$ sudo /sbin/ip6tables-save > /etc/sysconfig/ip6tables

Restart iptables to apply changes.

$ sudo service iptables restart
$ sudo systemctl restart iptables

That’s it. In the above tutorial, you have seen how to open a port in Linux.

Leave a Reply

Your email address will not be published. Required fields are marked *