Based on the Linux Master 1 exam syllabusReport an error
INPUT (incoming), OUTPUT (outgoing), and FORWARD (passing through).DROP discards silently (no response), while REJECT sends back a rejection response. -A appends to the end and -I inserts at the front — the order changes the result.firewall-cmd is the distinction between runtime vs --permanent (+ --reload).| Chain | Applies to |
|---|---|
INPUT | Packets coming into this system (destination is me) |
OUTPUT | Packets going out of this system (source is me) |
FORWARD | Packets passing through this system (when acting as a router) |
The chains split by direction. Blocking or allowing connections coming from outside into our server (web, SSH requests, etc.) is the job of the INPUT chain. On the exam, "block external SSH access" → an INPUT chain rule. "Control traffic going out from this host to elsewhere" → the OUTPUT chain. "Traffic passing through a gateway" → the FORWARD chain.
| Target | Meaning |
|---|---|
ACCEPT | Allows the packet |
DROP | Silently discards the packet (no response — the other side just sees silence) |
REJECT | Rejects the packet and sends back a rejection response (the other side immediately knows it was refused) |
LOG | Records the packet information in the log and moves on to the next rule |
DROP vs REJECT: both block, but the response differs. DROP discards the packet quietly, so the other side has to wait until a timeout (which has the effect of hindering port scans). REJECT immediately sends an ICMP response saying "refused", so the other side knows right away. For security, some setups use DROP for ports exposed externally and REJECT on the internal network for user convenience.
# Set the chain default policy to DROP (-P = policy)
iptables -P INPUT DROP
iptables -P FORWARD DROP
iptables -P OUTPUT ACCEPT⚠️ The remote work trap: if you run
iptables -P INPUT DROPfirst while connected over remote SSH, you cut yourself off before adding the SSH allow rule. You must add the SSH allow rule (-A INPUT -p tcp --dport 22 -j ACCEPT) first, and only then change the default policy to DROP. It is a trap both in practice and on the exam.
| Option | Meaning |
|---|---|
-A chain | Append a rule to the end of the chain |
-I chain [number] | Insert at the very front (or the specified position) of the chain |
-D chain | Delete a rule |
-R chain number | Replace a rule |
-L | List the rules |
-F | Flush (delete) all rules in the chain |
-P chain target | Set the default policy |
-j target | Jump: specify the action to take on a match |
| Option | Meaning |
|---|---|
-p | Protocol (tcp, udp, icmp) |
-s | Source IP/network |
-d | Destination IP/network |
--sport | Source port |
--dport | Destination port |
-i | Input interface (e.g., eth0) |
-o | Output interface |
# Allow TCP connections to port 22 (SSH)
iptables -A INPUT -p tcp --dport 22 -j ACCEPT
# Allow port 80 (HTTP)
iptables -A INPUT -p tcp --dport 80 -j ACCEPT
# Block all packets coming from a specific IP (192.168.1.50)
iptables -A INPUT -s 192.168.1.50 -j DROP
# Allow loopback (self) traffic
iptables -A INPUT -i lo -j ACCEPT
# Allow response packets of already established sessions (state tracking)
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPTThe order difference between -A and -I: since a chain matches from top to bottom, the position of a rule changes the result. -A attaches to the very end, and -I inserts at the very front. If an "everything DROP" rule is already at the top, adding an ACCEPT after it with -A is pointless because packets are already caught by the DROP and never reach it. In that case, you must insert the ACCEPT in front with -I.
# Show the rule list (-n: show IPs numerically without name resolution, -v: verbose, --line-numbers: line numbers)
iptables -L -n -v --line-numbers
# Show only a specific chain
iptables -L INPUT -n
# Reset all rules
iptables -F
# Save the rules (RHEL family) — persists after reboot
iptables-save > /etc/sysconfig/iptables
service iptables save⚠️ Beware of volatility: iptables rules exist only in memory. If you reboot without saving, they all disappear. On the RHEL family, you must persist them with
service iptables saveoriptables-save. It is common to panic about "my rules vanished" without knowing this.
| zone | Trust level |
|---|---|
drop | Blocks all incoming connections with no response (most closed) |
block | Rejects incoming connections (REJECT response) |
public | (Default) An untrusted public network. Only selected connections are allowed |
home / internal | A trusted internal network. Mostly allowed |
trusted | Allows all traffic (most open) |
The idea behind zones: if iptables deals with "rules line by line", firewalld applies situation-specific rule bundles, like "this network card is on a public network (public), and that card is on an internal network (internal)". The key is the flexibility, such as switching a laptop to the public zone when it connects to airport Wi-Fi and to the home zone when you get home.
firewall-cmd command.firewall-cmd are applied to the runtime only and disappear after a reboot.--permanent. However, it is not reflected immediately, so you need to reload with --reload.# Check the currently active zones and settings
firewall-cmd --get-active-zones
firewall-cmd --list-all
# Check/change the default zone
firewall-cmd --get-default-zone
firewall-cmd --set-default-zone=public
# Allow a service (runtime — disappears on reboot)
firewall-cmd --add-service=http
# Allow a service permanently (persists after reboot)
firewall-cmd --add-service=http --permanent
# Allow a port directly (permanent)
firewall-cmd --add-port=8080/tcp --permanent
# Apply the permanent settings immediately
firewall-cmd --reload| Command/Option | Meaning |
|---|---|
--list-all | Show the full settings of the current zone |
--add-service=name | Allow a service (http, ssh, etc.) |
--remove-service=name | Remove the service permission |
--add-port=port/protocol | Allow a specific port |
--permanent | Apply permanently (persists after reboot) |
--reload | Reload the permanent settings into the runtime |
--get-services | Show the list of predefined services |
The --permanent trap: if you only run firewall-cmd --add-service=http --permanent, it is written to the configuration file but not applied right now. You must follow it with firewall-cmd --reload for it to take effect in the current session too. Conversely, if you add without --permanent, it works now but disappears on reboot. To get both, either run it twice, once for runtime and once with permanent, or use permanent and then reload.
| Item | iptables | firewalld |
|---|---|---|
| Abstraction level | Low-level (controls rules directly) | High-level (managed per zone) |
| Core concepts | Tables, chains, rules | zone, service |
| Control command | iptables | firewall-cmd |
| Applying changes | Full re-apply (connections may be cut) | Dynamic apply (existing connections kept) |
| Persistence | service iptables save | --permanent + --reload |
| Relationship | Controls netfilter directly | Uses netfilter (nftables) internally |
The two must not be used together: enabling the firewalld and iptables services at the same time causes a conflict. On RHEL 7+, firewalld is the default, and to use iptables directly you must turn off firewalld (systemctl disable firewalld) and enable the iptables service. Since both ultimately control the kernel's netfilter, only one of them should be used as the manager.
Try it yourself: on a test VM, run firewall-cmd --add-service=http (runtime only), check with firewall-cmd --list-services, and confirm it disappears after a reboot. Then do it again with --permanent and also go through --reload to confirm that it persists, and the difference between runtime and permanent will sink in. On the iptables side, look at the line numbers with iptables -L -n --line-numbers and then experiment with the positional difference between -I and -A.
Tomorrow we widen the scope of security with TCP Wrapper, SELinux, and log management.
INPUT (incoming), OUTPUT (outgoing), FORWARD (passing through).-P.public (default), home, trusted, and drop.Click a choice to reveal the answer and explanation.
Question 1
In the iptables filter table, which chain handles packets coming into this system from outside?
Question 2
Among the iptables targets, which one discards the packet without sending any response to the other side?
Question 3
Which is the correct iptables command to allow incoming TCP connections on port 22 (SSH)?
Question 4
Which correctly pairs the iptables option that inserts a rule at the very front of a chain with the option that appends one to the very end?
Question 5
In firewalld, which option applies a change permanently so that it persists after a reboot?
Question 6
Among firewalld zones, which has the highest trust level and allows all traffic?
Question 7
What must you do on the RHEL family to keep iptables rules after a reboot?