Linux Port Opening Guide: Port Management with Firewall
The basic way to control network traffic on Linux servers is port management.
In this guide, you will learn to list open ports on your Linux system, open a new port, and manage these settings securely.
What Will You Learn in This Guide?
- List open ports in Linux
- Open ports with ufw, firewalld and iptables
- Testing whether the opened ports are working or not
- Making port rules permanent
What is a Port?
A port is a communication endpoint on the network.
In Linux, each service receives or sends data through a specific port.
Port ranges:
- 0–1023 → Known ports (SSH 22, HTTP 80, HTTPS 443)
- 1024–49151 → User ports
- 49152–65535 → Dynamic ports
In this guide, the 4000/TCP port is used as an example.
Prerequisites
- Basic command of terminal usage
- Root or
sudoauthority
1. List Open Ports
You should check existing ports before opening a new port.
View open ports with netstat
netstat -lntu
- This command lists TCP and UDP listening sockets.
1. view open ports with ss
ss -lntu
- This command is faster and more up to date than netstat.
2. Opening a Port in Linux
Ubuntu/Debian (ufw)
sudo ufw allow 4000
- This command allows TCP port 4000.
CentOS/RHEL (firewalld)
sudo firewall-cmd --add-port=4000/tcp
- This command opens the port temporarily.
Public Method (iptables)
sudo iptables -A INPUT -p tcp --dport 4000 -j ACCEPT
- This command adds a new permission to the system's packet filtering rules.
3. Testing the Opened Port
- Even if a port is open, if there is no service listening on that port, a connection cannot be established.
Port listening with netcat
ls | nc -l -p 4000
- This command starts listening on port 4000.
- connection test with telnet
telnet localhost 4000
- This command checks whether the port accepts connections.
4. Making Port Rules Persistent
-
ufw
-
Rules are persistent by default.
firewalld
sudo firewall-cmd --permanent --add-port=4000/tcp
sudo firewall-cmd --reload
iptables
- Rules must be registered with the iptables-persistent package.
| Vehicle | Ease of Use | Distribution | Example Command |
|---|---|---|---|
| ufw | Very Easy | Ubuntu/Debian | ufw allow 80 |
| firewalld | Medium | CentOS/RHEL | firewall-cmd --add-port=80/tcp |
| iptables | Difficult | All Linux | iptables -A INPUT -p tcp --dport 80 -j ACCEPT |
Frequently Asked Questions (FAQ)
1. Is it safe to open ports? Unnecessary open ports increase the attack surface. You should only open the ports that are needed.
2. The port is open but the connection cannot be established, why? There is no active service listening to the port.
3. Can I open a port range? Yes.
sudo ufw allow 5000:6000/tcp
4. Is it useful to change the SSH port? Yes. Significantly reduces brute-force attacks.
Result
Port management in Linux is the basis of system security. A properly configured firewall increases both performance and security.
You can immediately try high-performance and secure Linux servers on the GenixNode infrastructure.

