Explains firewalls, common ports, and basic UFW and FirewallD commands plus tools to inspect listening sockets and secure remote access
So what’s a firewall?A firewall is a security barrier—either software or hardware—that sits between your systems and the network (including the internet) and enforces policies about which traffic is allowed or denied. It inspects, monitors, and controls network traffic entering and leaving a host or network according to configured rules.
Think of a firewall like a home security system that watches doors and windows: keep most entry points closed and open only what you need. Firewalls come in two common forms:
Host-based software firewalls (typical for desktops and individual servers).
Dedicated hardware appliances (common in enterprise and datacenter networks).
Debian- and Ubuntu-based distributions commonly use UFW (Uncomplicated Firewall) to configure host firewall rules. UFW is a simpler front end to the kernel-level packet filters (historically iptables; many modern systems use nftables underneath). By default UFW is disabled, so enabling and tightening it is a basic hardening step—allowing only required traffic.
Red Hat family distributions (RHEL, CentOS, Fedora) typically use FirewallD as the management tool. FirewallD also sits on top of the kernel packet filtering stack (iptables/nftables). If FirewallD is not already installed, you can add it through your package manager.
Host firewalls are also available on Windows and macOS. Windows Firewall is usually enabled by default; on macOS you can enable the firewall in System Settings. Regardless of platform, enabling a host firewall and restricting unnecessary ports is a fundamental security practice.Ports — what are they?A port is a network communication endpoint used by services to send and receive data. If a computer is a house, ports are the doors or windows: keep most closed and open only the ones needed.When you browse HTTPS websites, your browser typically connects to port 443. SSH commonly uses port 22, RDP uses port 3389, and so on. You don’t need to memorize every port—focus on the ones you use.
Quick reference table — common ports to know:
Service
Port
Use
HTTP
80
Unencrypted web traffic
HTTPS
443
Encrypted web traffic (TLS/SSL)
SSH
22
Secure shell remote access
RDP
3389
Remote Desktop (Windows)
MySQL
3306
MySQL database
PostgreSQL
5432
PostgreSQL database
Redis
6379
Redis key-value store
DNS
53
Domain name resolution
FTP
21
File Transfer Protocol
Kubernetes API
6443
Kubernetes control plane API
To host a website to the public, you generally need only HTTP (80) and HTTPS (443) open. If you must open other ports, reduce risk by:
Restricting access by client IP (whitelisting).
Placing services behind a VPN, reverse proxy, or load balancer.
Using a Web Application Firewall (WAF) to block malicious requests and allow legitimate traffic.
Important: always open your remote access port before enabling a firewall on a remote server—otherwise you may lock yourself out.
Before enabling a firewall on a remote system, ensure your remote access port (for example SSH on port 22) is allowed. If you enable the firewall without allowing your remote access, you could lose remote access to the machine.
UFW (Debian/Ubuntu) — basic commandsRun these on Debian/Ubuntu-based systems to allow the most common access and enable the firewall. Always allow remote access first on remote machines.
Allow SSH (important to run first on remote servers):
sudo ufw allow 22/tcp
Allow HTTP and HTTPS:
sudo ufw allow 80/tcpsudo ufw allow 443/tcp
Enable the firewall:
sudo ufw enable
Check status (human-readable):
sudo ufw status verbose
Show rules with numbering (useful for deleting a rule):
sudo ufw status numbered
Delete a rule by its number (example deleting rule number 3):
sudo ufw delete 3
Tip: you can also delete a rule by specifying it, for example:
sudo ufw delete allow 22/tcp
FirewallD (RHEL/CentOS/Fedora) — basic commandsInstall, start, and enable FirewallD:
Query runtime or permanent rules and the default zone:
sudo firewall-cmd --list-all# or for a specific zone:sudo firewall-cmd --zone=public --list-ports
Combined quick example for FirewallD:
# Install and enable FirewallDsudo yum update && sudo yum install -y firewalldsudo systemctl start firewalldsudo systemctl enable firewalld# Open HTTP and HTTPS permanently, then reloadsudo firewall-cmd --permanent --add-port=80/tcpsudo firewall-cmd --permanent --add-port=443/tcpsudo firewall-cmd --reload# Remove HTTP and reloadsudo firewall-cmd --permanent --remove-port=80/tcpsudo firewall-cmd --reload
Inspecting listening ports and connectionsFirewalls control what traffic can get to your host, but you also need to know which services are actually listening on which ports. Tools to inspect sockets:
ss (modern, recommended): ss -tulnp shows TCP/UDP listening sockets with process info.
netstat (older, from net-tools): sudo netstat -nltup shows listening sockets and owning processes.
Remember: a firewall may permit a port, but if no service is listening that port is effectively closed. Conversely, a service can be listening but unreachable because a firewall blocks it. When troubleshooting connectivity, combine checks: