How-to

How to Open a Port on a Linux Server

Opening a port takes two parts: a program that listens and a firewall rule that admits traffic. Add the rule with the tool your distro already uses, then confirm from another network.

Do I need a firewall rule at all?

Only if a firewall is active. A fresh Ubuntu or Debian image has ufw installed but switched off, so any program that listens on a public address is already reachable. AlmaLinux, Rocky Linux and CentOS Stream images usually run firewalld with only SSH and a few basics allowed. Check first:

sudo ufw status
sudo systemctl is-active firewalld
sudo nft list ruleset | head -40

If nothing is active, the port opens the moment a service listens on it. That is also why we suggest turning a firewall on early and opening only what you use.

How do I open a port with ufw on Ubuntu or Debian?

sudo ufw allow 22/tcp comment 'ssh'
sudo ufw allow 8443/tcp comment 'admin api'
sudo ufw allow 27015:27030/udp
sudo ufw enable
sudo ufw status numbered

Always allow your SSH port before ufw enable, or the running session drops and you will need a reinstall from the panel to get back in. If you moved SSH with Change the SSH port, allow the new number. Ranges use a colon and need a protocol. The comment shows up in status, which helps when you return to the rules months later.

How do I open a port with firewalld on AlmaLinux or Rocky?

sudo firewall-cmd --permanent --add-port={8443,8444}/tcp     # admin api and its websocket
sudo firewall-cmd --add-service=https --permanent --zone=public   # named port set
sudo firewall-cmd --reload; sudo firewall-cmd --list-ports --list-services

--permanent writes the rule to disk, and --reload applies it. Without --permanent the rule works at once but disappears at the next reload or reboot. Services such as https or postgresql are named port sets defined in /usr/lib/firewalld/services/. A full walkthrough lives in Enable and use firewalld.

Can I open a port only to certain addresses?

Yes, and for anything administrative you should. A database or monitoring agent rarely needs the whole internet.

# ufw
sudo ufw allow from 198.51.100.24 to any port 5432 proto tcp
# firewalld
sudo firewall-cmd --permanent --zone=public --add-rich-rule='rule priority="-5" family="ipv4" source address="198.51.100.0/28" port port="9100" protocol="tcp" log prefix="node-exp " level="info" accept'
sudo firewall-cmd --reload && sudo firewall-cmd --list-rich-rules

Replace the addresses with your office or app server IPs. A rich rule in firewalld combines source, port and action in one line.

How do I open a port with plain nftables?

If you prefer raw rules, Debian and the RHEL family ship nftables. A minimal input chain that allows SSH and HTTPS and drops the rest looks like this:

sudo tee /etc/nftables.conf >/dev/null <<'EOF'
#!/usr/sbin/nft -f
flush ruleset
table inet filter {
  chain inbound {
    type filter hook input priority filter    # every inbound packet passes here
    policy drop
    ct state vmap { established : accept, related : accept, invalid : drop }
    iifname "lo" accept
    meta l4proto icmp accept
    tcp dport { ssh, https } accept comment "admin and web"
  }
}
EOF
sudo nft -c -f /etc/nftables.conf && sudo systemctl enable --now nftables

nft -c checks the file without applying it. Do not mix this with ufw or firewalld on the same box; pick one tool. If you are used to the older syntax, iptables basics covers it.

How do I make the program listen?

A firewall rule alone opens nothing if no service listens. Check the bind address in the app config: listen_addresses = '*' for PostgreSQL, bind-address = 0.0.0.0 for MySQL, bind 0.0.0.0 for Redis. Restart the service, then confirm with sudo ss -ltnp. For a quick test before the real app exists, start a throwaway listener:

python3 -m http.server 8443 --bind 0.0.0.0

How do I confirm the port is open from outside?

nc -zv -w 4 203.0.113.10 8443
curl -m 5 -sI http://203.0.113.10:8443/

Run these from your laptop or another server, never from the box itself, because local tests skip the firewall path. A timeout points at a firewall; "connection refused" means traffic arrives but nothing listens on that address. Check open ports in Linux has the full checklist.

Are there ports I should avoid opening?

Keep database, cache and message queue ports closed to the world: 3306, 5432, 6379, 11211, 27017 and 9200 are scanned constantly. Put them behind source rules or an SSH tunnel. Outgoing mail on port 25 is also a special case on any host, so test it before you build a mail service around it. Everything else is your call; it is your machine.

Machines You Can Firewall Your Own Way

ServerProcessorCores / threadsMemoryStorageNetworkPer monthSmallest order
E2Intel Xeon E3-1270 v34 / 832 GB DDR31x 480 GB SSD2x 1 Gbps$38.32$114.96 (3 months)
S1Intel Xeon E-2276G6 / 1264 GB DDR41x 1 TB NVMe2x 10 Gbps$53.32$53.32 (1 month)
S11Intel Core i9-14900K24 / 32128 GB DDR52x 2 TB NVMe2x 10 Gbps$149.39$149.39 (1 month)

Keep Reading

Check open ports in Linux

Find which ports your Linux machine listens on, which program owns each one, and what the internet can really reach from outside.

Open

Enable and use firewalld

Turn on firewalld on CentOS Stream, AlmaLinux or Rocky Linux, learn zones and services, and keep runtime and permanent rules in sync.

Open

iptables basics

How iptables tables, chains and targets fit together, plus a minimal default-deny rule set for a new bare metal server.

Open

Firewall

What a firewall does, packet filters versus stateful and application firewalls, and the Linux tools that set the rules on a dedicated machine.

Open

FAQ

Quick Answers

Check that ufw is enabled and that the service listens on 0.0.0.0 rather than 127.0.0.1.

Only rules added with --permanent and then loaded with --reload or a restart.

Yes. Use 6000:6100/tcp in ufw or --add-port=6000-6100/tcp in firewalld.

Use the one your distro ships: ufw on Ubuntu and Debian, firewalld on AlmaLinux, Rocky and CentOS Stream.

No. Edge filtering absorbs floods, but which ports are open is set by your own firewall.