A Linux service can stay unreachable while the host firewall blocks its port. One rule in the active firewall manager opens that port to clients while SSH or other management access stays in place, and a connection from another host proves the path works.
Most Linux hosts expose firewall policy through one active manager, commonly ufw on Ubuntu or Debian systems, firewalld on RHEL-family and Fedora systems, or a direct nftables ruleset on leaner servers. Use the active manager for the host instead of editing the backend rules behind another tool, because mixed firewall frontends can overwrite or hide each other's changes.
Firewall changes affect live access, so keep the current session open on a remote server until the new rule works. The port must also match the application's transport protocol, and a persistent rule is needed when the port should stay open after a reload or reboot.
Steps to allow a port through a Linux firewall:
Check the listener first, then follow the section for the firewall manager on the host: ufw on most Ubuntu and Debian systems, firewalld on RHEL-family and Fedora systems, or direct nftables rules when neither of those owns the ruleset.
- Confirm that the service is listening on the port you want to open.
$ sudo ss -ltnp State Recv-Q Send-Q Local Address:Port Peer Address:PortProcess LISTEN 0 5 0.0.0.0:8080 0.0.0.0:* users:(("python3",pid=136,fd=3)) LISTEN 0 4096 0.0.0.0:22 0.0.0.0:* users:(("sshd",pid=213,fd=3),("systemd",pid=1,fd=69)) ##### snipped #####Use sudo ss -lunp for a UDP service, and test it at the end with the application's own client, because nc can report success even when a firewall drops the datagrams.
Allow a port with ufw
- Allow the port, replacing 8080/tcp with the port and protocol the application uses.
$ sudo ufw allow 8080/tcp Rule added Rule added (v6)
To allow one client instead of every client, use sudo ufw allow from 192.0.2.10 to any port 8080 proto tcp, or a network such as 192.0.2.0/24 after from. An existing rule from Anywhere keeps admitting every client until it is deleted.
- Check that the new rule is listed.
$ sudo ufw status numbered Status: active To Action From -- ------ ---- [ 1] 22/tcp ALLOW IN Anywhere [ 2] 8080/tcp ALLOW IN Anywhere [ 3] 22/tcp (v6) ALLOW IN Anywhere (v6) [ 4] 8080/tcp (v6) ALLOW IN Anywhere (v6)Status: inactive means ufw is off and is not blocking the port, so the rule waits until ufw is enabled. Turning it on over a remote session blocks new SSH connections unless that port is allowed first.
- Test the port from another host, replacing server.example.net with the server's name or address.
$ nc -vz -w 5 server.example.net 8080 Connection to server.example.net (192.0.2.20) 8080 port [tcp/http-alt] succeeded!
A timed out result usually means a firewall on the path, such as a cloud firewall, still blocks the port. Connection refused usually means nothing is listening on it.
Allow a port with firewalld
- Add the port to the permanent firewalld configuration, replacing 8080/tcp with the port and protocol the application uses.
$ sudo firewall-cmd --permanent --add-port=8080/tcp success
The rule goes to the default zone. If sudo firewall-cmd --get-active-zones shows the network interface in another zone, add that zone to this command and the port check, such as --zone=work.
- Reload firewalld to apply the permanent rule.
A reload discards every runtime-only rule, so a management port opened without --permanent stops accepting new connections.
$ sudo firewall-cmd --reload success
- Check that the port is listed.
$ sudo firewall-cmd --list-ports 8080/tcp
- Test the port from another host, replacing server.example.net with the server's name or address.
$ nc -vz -w 5 server.example.net 8080 Connection to server.example.net (192.0.2.30) 8080 port [tcp/http-alt] succeeded!
A No route to host or timed out result usually means the rule is in the wrong zone or a firewall on the path, such as a cloud firewall, still blocks the port. Connection refused usually means nothing is listening on it.
Allow a port with direct nftables rules
- Back up the nftables rules file.
$ sudo cp /etc/nftables.conf /etc/nftables.conf.bak
On RHEL-family and Fedora hosts, use /etc/sysconfig/nftables.conf in place of /etc/nftables.conf in each step, because that is the file their nftables service loads.
- Open the rules file in your text editor.
$ sudoedit /etc/nftables.conf
- Insert the following line immediately after tcp dport 22 accept in the input chain.
tcp dport 8080 accept
Applying the file replaces the live rules, so a rule added with nft add rule and missing from the file disappears unless you add it here too.
- Save the file and exit the editor.
- Check the file for errors.
$ sudo nft -c -f /etc/nftables.conf
No output means the file parsed successfully.
- Apply the file.
Keep the current session open until a new connection succeeds, because it is the way back in. If the new rules block access, remove the added line and apply the file again.
The flush ruleset line in the file clears every table when it is applied, including rules created by other services such as Docker, which puts its rules back when restarted.
$ sudo nft -f /etc/nftables.conf
A file without flush ruleset above the table block adds to the live rules instead of replacing them, so add that line if it is missing.
The file loads at boot only while the service is enabled, which sudo systemctl enable nftables turns on.
- Check that the new rule is active.
$ sudo nft list ruleset table inet filter { chain input { type filter hook input priority filter; policy drop; iif "lo" accept ct state established,related accept tcp dport 22 accept tcp dport 8080 accept } } - Test the port from another host, replacing server.example.net with the server's name or address.
$ nc -vz -w 5 server.example.net 8080 Connection to server.example.net (192.0.2.40) 8080 port [tcp/http-alt] succeeded!
A timed out result usually means a firewall on the path, such as a cloud firewall, still blocks the port, or the new line sits below a final drop rule. Connection refused usually means nothing is listening on it.