How to Open Port in Linux

Opening ports on a Linux server is essential when you want to allow external devices to communicate with specific services, like web servers, SSH, or database servers. This tutorial will guide you through the process of opening a port in Linux safely and effectively.

Step 1: Check if the Port is Already Open

Before opening a port, check whether it’s already open or in use.

sudo netstat -tuln | grep PORT_NUMBER

Replace PORT_NUMBER with the actual port you want to check, for example, 8080.

If nothing returns, the port is closed or unused.

Step 2: Identify Your Firewall System

Most modern Linux distros use either firewalld, iptables, or ufw (Ubuntu) to manage firewall rules.

Check which firewall is running:

sudo systemctl status firewalld

or

sudo systemctl status ufw

Step 3: Open the Port

If You Use firewalld (CentOS, RHEL, Fedora)

  1. Open the port temporarily (until next reboot or firewall restart):

sudo firewall-cmd --add-port=PORT_NUMBER/tcp
  1. To make the change permanent:

sudo firewall-cmd --add-port=PORT_NUMBER/tcp --permanent sudo firewall-cmd --reload

Example: Open port 8080 TCP

sudo firewall-cmd --add-port=8080/tcp --permanent sudo firewall-cmd --reload

If You Use ufw (Ubuntu)

  1. Allow the port:

sudo ufw allow PORT_NUMBER/tcp
  1. Reload ufw (usually not necessary, but to be sure):

sudo ufw reload

Example: Open port 8080 TCP

sudo ufw allow 8080/tcp

If You Use iptables (Older distros or manual firewall rules)

  1. Add a rule to allow traffic on the port:

sudo iptables -A INPUT -p tcp --dport PORT_NUMBER -j ACCEPT
  1. Save the iptables rules to persist after reboot.

For Debian/Ubuntu:

sudo netfilter-persistent save

For CentOS/RHEL:

sudo service iptables save

Example: Open port 8080 TCP

sudo iptables -A INPUT -p tcp --dport 8080 -j ACCEPT sudo netfilter-persistent save

Step 4: Verify the Port is Open

You can check again with:

sudo firewall-cmd --list-ports # for firewalld sudo ufw status # for ufw sudo iptables -L -n | grep PORT_NUMBER # for iptables

Or test externally by using:

telnet YOUR_SERVER_IP PORT_NUMBER

Bonus: Open a Port in SELinux (If Enabled)

If SELinux is enforcing, you may also need to allow the port explicitly:

sudo semanage port -a -t http_port_t -p tcp PORT_NUMBER

If semanage is not installed:

sudo yum install policycoreutils-python-utils

Summary

  • Identify your firewall management tool (firewalld, ufw, iptables).

  • Add rules to open your desired port.

  • Reload or save your firewall settings.

  • Verify the port is open.

  • Consider SELinux settings if applicable.

StarCode Kh

StarCode Kh

Website that learns and reads, PHP, Framework Laravel, How to and download Admin template sample source code free.

Post a Comment

close