Open ports with firewalld and ufw
firewalld (RHEL, Rocky, AlmaLinux) and ufw (Ubuntu) are friendly front ends to the kernel firewall. Allow SSH first, then open only the ports you need.
Not checked by a trusted editor yet Last edited Outdated?
Both tools manage the same kernel packet filter (nftables or iptables underneath) but think about it differently. firewalld groups rules into zones and separates the running (runtime) config from the saved (permanent) config. ufw, the “uncomplicated firewall” on Ubuntu, is a simple list of allow and deny rules. Use the one your distribution ships and do not run both.
Warning: when you work over SSH, make sure SSH stays allowed before you enable a firewall or change its default policy. Keep a second session or console access open until you have tested a new login.
firewalld (RHEL, Rocky, AlmaLinux)
firewalld is installed and running by default. See what is allowed now:
Check before you run this
- Runs with administrator rights. Make sure you understand every line.
sudo firewall-cmd --state
sudo firewall-cmd --get-active-zones # which zone each interface is in
sudo firewall-cmd --list-all # services and ports of the default zone
On a default install the active zone is usually public and already allows ssh. Open a web server:
- Add the services to the permanent config:
sudo firewall-cmd --permanent --add-service=http --add-service=https - Load the permanent config:
sudo firewall-cmd --reload - Check:
sudo firewall-cmd --list-services
Without --permanent, a change applies immediately but is lost on reload or reboot. That is useful for testing: change at runtime, test, then save the working state with sudo firewall-cmd --runtime-to-permanent. Note that --reload throws away runtime-only changes.
Check before you run this
- Runs with administrator rights. Make sure you understand every line.
# a port with no predefined service
sudo firewall-cmd --permanent --add-port=8080/tcp
# remove something again
sudo firewall-cmd --permanent --remove-service=http
# list the predefined service names
sudo firewall-cmd --get-services
sudo firewall-cmd --reload
To allow a port only from one network, use a rich rule:
Check before you run this
- Runs with administrator rights. Make sure you understand every line.
sudo firewall-cmd --permanent --zone=public \
--add-rich-rule='rule family="ipv4" source address="10.0.0.0/24" port port="5432" protocol="tcp" accept'
sudo firewall-cmd --reload
ufw (Ubuntu)
ufw is usually installed on Ubuntu but not enabled. Check, add the SSH rule first, then enable:
Check before you run this
- Runs with administrator rights. Make sure you understand every line.
sudo ufw status verbose
sudo ufw default deny incoming
sudo ufw default allow outgoing
sudo ufw allow 22/tcp
sudo ufw enable
ufw enable asks for confirmation because it may disrupt existing SSH connections. With the SSH rule in place, answer yes, then test a new login from another terminal.
Check before you run this
- Runs with administrator rights. Make sure you understand every line.
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw allow from 10.0.0.0/24 to any port 5432 proto tcp
sudo ufw limit 22/tcp # deny an address after 6+ connections in 30 seconds
sudo ufw status numbered
sudo ufw delete 3 # delete rule number 3 from the numbered list
sudo ufw delete allow 80/tcp # or delete by repeating the rule
ufw app list shows application profiles installed by packages; you can allow one by name with sudo ufw allow "Name".
Side by side
- Show rules:
firewall-cmd --list-all/ufw status verbose - Open a port:
firewall-cmd --permanent --add-port=8080/tcp+--reload/ufw allow 8080/tcp - Allow from one network: rich rule /
ufw allow from 10.0.0.0/24 to any port 5432 proto tcp - Close it again:
--remove-port+--reload/ufw delete allow 8080/tcp
Remember
- A port only answers if a program listens on it. Check with
sudo ss -tlnp. - Cloud servers usually have a provider firewall (security groups) in front of the server’s own firewall; both must allow the traffic.
- Containers that publish ports can add their own rules that bypass ufw’s view of the world. Check your container runtime’s documentation when exposing ports.
Official documentation
See also
- 0103 Proxmox VE networking: Linux bridges (vmbr0) and VLANs Guests reach the network through Linux bridges such as vmbr0, defined in /etc/network/interfaces. Learn the default setup, VLAN-aware bridges, VLAN tags for… Intermediate · How-to · Linux Article Installation & first steps · Networking & firewalls 15 Oct 2026
- 0167 Fix: Job for nginx.service failed because the control process exited with error code systemctl prints this when a service's start command exits with an error. The real reason is in journalctl; for nginx it is… Intermediate · Fix · Linux Article Networking & firewalls · Services & systemd 15 Oct 2026
- 0173 Set a static IP address: nmcli on RHEL vs netplan on Ubuntu RHEL, Rocky and AlmaLinux configure networking with NetworkManager (nmcli); Ubuntu Server uses netplan YAML. Here is the same static IP setup in… Intermediate · How-to · Linux Article Networking & firewalls 15 Oct 2026
- 0174 Network and DNS troubleshooting commands: ip, ss, dig, resolvectl A layer-by-layer cheat sheet for "the server cannot reach X": interfaces and routes with ip, listening ports with ss, and name resolution… Beginner · Reference · Linux Article Networking & firewalls 15 Oct 2026
- 0249 Set up and harden a fresh Linux server in one afternoon Take a new Ubuntu, Debian or Rocky/Alma server from first login to a safer baseline: sudo admin, SSH keys only, firewall, automatic… Beginner · Tutorial · Linux Article Networking & firewalls · Packages & updates · Users, sudo & hardening 15 Oct 2026
Repos in the wild
- firewalld: zone-based firewall daemon firewalld/firewalld Upstream source of firewalld, the default firewall manager on RHEL, Rocky Linux, AlmaLinux and Fedora, including firewall-cmd and its service definitions. GitHub
History
Every saved edit is kept. The plot on the sleeve draws one line per edit. Open any edit to see exactly what changed.
- Commitpedia +3,703 chars