Operations 4 min read

Master Linux Firewall: firewalld and iptables Commands Explained

This guide walks you through using Linux's two main firewall tools—firewalld for CentOS 7+ and iptables for older versions—covering installation, service control, rule management, port handling, and useful commands with clear examples and screenshots.

macrozheng
macrozheng
macrozheng
Master Linux Firewall: firewalld and iptables Commands Explained

firewalld (CentOS 7+)

Start the firewall service systemctl start firewalld Stop the firewall service systemctl stop firewalld Check firewall status systemctl status firewalld Enable firewall at boot systemctl enable firewalld Disable firewall at boot systemctl disable firewalld Reload firewall rules firewall-cmd --reload Open a port (requires reload to take effect)

firewall-cmd --zone=public --add-port=8080/tcp --permanent

List opened ports firewall-cmd --list-ports Close a port

firewall-cmd --zone=public --remove-port=8080/tcp --permanent
firewalld screenshot
firewalld screenshot

iptables (CentOS < 7)

Installation

Install iptables package yum install iptables Install iptables services

yum install iptables-services

Service Control

Start iptables systemctl start iptables.service Stop iptables systemctl stop iptables.service Check iptables status systemctl status iptables.service Enable iptables at boot systemctl enable iptables.service Disable iptables at boot

systemctl disable iptables.service

Rule Management

View filter table chains (e.g., INPUT) iptables -L -n View NAT table chains iptables -t nat -L -n Flush all rules

iptables -F
iptables -X
iptables -Z

Add rule to open port 8080 on INPUT chain iptables -I INPUT -p tcp --dport 8080 -j ACCEPT List rule line numbers for INPUT chain iptables -L INPUT --line-numbers -n Delete rule by line number (e.g., close port 8080)

iptables -D INPUT 1
iptables filter table screenshot
iptables filter table screenshot
iptables NAT table screenshot
iptables NAT table screenshot
iptables add rule screenshot
iptables add rule screenshot
iptables line numbers screenshot
iptables line numbers screenshot
iptables delete rule screenshot
iptables delete rule screenshot
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Linuxnetwork securityiptablessystemctlfirewalld
macrozheng
Written by

macrozheng

Dedicated to Java tech sharing and dissecting top open-source projects. Topics include Spring Boot, Spring Cloud, Docker, Kubernetes and more. Author’s GitHub project “mall” has 50K+ stars.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.