How to Open Port in Linux
Check Listening Ports on Your System
You can use `ss` or `netstat` command line utility to list all the ports listening on your local system.
This command will list all the ports listening on your machine along with the socket connected to that ports. You can filter the listening port with the following command.
You will see output like below:
Opening a Port with FirewallD:
FirewallD is a firewall management tool that is included in many popular Linux distributions, including CentOS, Fedora, and Red Hat Enterprise Linux. It provides a simple and easy-to-use interface for configuring the firewall rules on your system.
- List Services: To open a port with FirewallD, you first need to check if the service you want to allow is listed in the predefined service list. You can do this by running the following command:
This will display a list of all the predefined services that are supported by FirewallD.
- Open Port by Service Name: If the service you want to allow is listed, you can open the port by running the following command:
Replace “service_name” with the name of the service you want to allow. For example, to open port 80 for HTTP traffic, you would run the following command:
- Open Port by Number: If the service you want to allow is not listed in the predefined service list, you can open a specific port by running the following command:
Replace “port_number” with the number of the port you want to open, and “protocol” with the protocol used by the service (either “tcp” or “udp”). For example, to open port 8080 for HTTP traffic using the TCP protocol, you would run the following command:
Save the firewall rules: Once you have added the necessary firewall rule, you need to reload the firewall to apply the changes. You can do this by running the following command:
Opening a Port with UFW:
UFW (Uncomplicated Firewall) is a firewall management tool that is included in many popular Linux distributions, including Ubuntu and Linux Mint. It provides a simple and easy-to-use interface for configuring the firewall rules on your system.
- List Services: To open a port with UFW, you first need to check if the service you want to allow is listed in the predefined service list. You can do this by running the following command:
This will display a list of all the predefined services that are supported by UFW.
- Open Port by Service name: If the service you want to allow is listed, you can open the port by running the following command:
Replace “service_name” with the name of the service you want to allow. For example, to open port 80 for HTTP traffic, you would run the following command:
- Open port by number: If the service you want to allow is not listed in the predefined service list, you can open a specific port by running the following command:
Replace “port_number” with the number of the port you want to open, and “protocol” with the protocol used by the service (either “tcp” or “udp”). For example, to open port 8080 for HTTP traffic using the TCP protocol, you would run the following command:
- Check firewall status: Once you have added the necessary firewall rule, you can check the status of the UFW firewall by running the following command:
This will display a list of all the active firewall rules, along with their status (either “enabled” or “disabled”).
You can also use the UFW command line interface to enable or disable specific rules, or to delete them altogether.
Opening a Port with Iptables:
Iptables is a powerful firewall management tool that is included in most Linux distributions. It provides a wide range of options for configuring the firewall rules on your system, but it can be more complex to use than FirewallD and UFW.
- Open Port by Number: To open a port with iptables, you need to use the “iptables” command followed by the appropriate options and arguments. For example, to open port 80 for HTTP traffic using the TCP protocol, you would run the following command:
This command adds a new firewall rule that allows incoming traffic on port 80 using the TCP protocol. The “-A” option specifies that the rule should be appended to the end of the INPUT chain, and the “-j” option specifies the action to be taken (in this case, ACCEPT).
- Open Port Range: If you want to open a range of ports, you can use the “-m multiport” module and specify the range of ports separated by a comma. For example, to open ports 80 to 90 for HTTP traffic using the TCP protocol, you would run the following command:
- Save firewall rules: Once you have added the necessary firewall rule, you can save the iptables configuration by running the following command:
This will save the current firewall configuration to the appropriate configuration file so that the rules are applied every time the system is restarted.
Conclusion
In this article, we explored three different tools that can be used to open a port in Linux: FirewallD, UFW, and iptables. FirewallD is a firewall management tool that provides a front-end interface for iptables. It is a user-friendly tool that allows you to easily manage your firewall rules. UFW is another user-friendly firewall tool that allows you to easily open and close ports. Finally, we looked at iptables, which is a more advanced tool that gives you greater control over your firewall. All three of these tools can be used to open a port in Linux and allow network traffic to flow through to specific programs or services. In conclusion, the choice of which tool to use will depend on your level of familiarity with Linux firewalls and your personal preference.