Managing Firewall Ports on Linux with iptables, firewalld, and ufw
Managing Ports on CentOS
Control the firewalld service daemon:
# Activate the firewall service
systemctl start firewalld.service
# Deactivate the firewall service
systemctl stop firewalld.service
# Check current service status
systemctl status firewalld
Using iptables (CentOS/RHEL 6)
To permit traffic on ports 80 and 443:
iptables -I INPUT -p tcp --dport 80 -j ACCEPT
iptables -I INPUT -p tcp --dport 443 -j ACCEPT
service iptables save
List the current filterign rules:
iptables -L -n
Using firewalld (CentOS/RHEL 7+)
Add permanent rules for specific ports and reload the configuration:
# Open port 80
firewall-cmd --zone=public --permanent --add-port=80/tcp
# Open port 443
firewall-cmd --zone=public --permanent --add-port=443/tcp
# Apply changes
firewall-cmd --reload
Utility commands for firewalld:
# Check if the firewall is active
firewall-cmd --state
# List all currently open ports
firewall-cmd --list-ports
# List available zones
firewall-cmd --get-zones
# Manage services (example with ftp)
firewall-cmd --query-service ftp
firewall-cmd --add-service=ftp --permanent
firewall-cmd --remove-service=ftp --permanent
Managing Ports on Ubuntu and Debian
Using ufw (Uncomplicated Firewall)
Check the current status of the firewall:
sudo ufw status verbose
Alow traffic on ports 80 and 443:
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw reload
Common ufw operational commands:
# Enable the firewall
sudo ufw enable
# Disable the firewall
sudo ufw disable
# Reset all rules to default
sudo ufw reset
# Deny specific traffic
sudo ufw deny 22/tcp