CertNote/ Commute Study Notes
Roadmap
KOEN
CLF-C02 · FoundationalCloud Practitioner - Foundational
DVA-C02 · AssociateDeveloper - Associate
SAA-C03 · AssociateSolutions Architect - Associate
SOA-C02 · AssociateCloudOps Engineer - Associate
SAP-C02 · ProfessionalSolutions Architect - Professional
DOP-C02 · ProfessionalDevOps Engineer - Professional
Linux Master 2 · 2급KAIT Linux Master - Level 2
SCS-C03 · SpecialtySecurity - Specialty
Linux Master 1 · 1급KAIT Linux Master - Level 1
  • Week 1
    • 1.Operating Systems and Linux: What the Kernel Does
    • 2.Free Software and Distributions: The Ecosystem Built by Licenses
    • 3.Installation and Booting: From the Moment You Power On
    • 4.Filesystem Structure and File Types: Everything Is a File
    • 5.Week 1 Comprehensive Review: Time to See Through the Concepts
  • Week 2
  • Week 3
  • Week 4
  • Week 5
  • Week 6
  • Week 7
  • Week 8
  • Week 9
  • Week 10
  • Week 11
    • 1.Samba: File and Printer Sharing Between Windows and Linux
    • 2.NFS: The Standard File Sharing of the Unix World
    • 3.Firewalls: iptables and firewalld
    • 4.Access Control and Security: TCP Wrapper, SELinux, Log Management
    • 5.Week 11 Comprehensive Review: The Big Picture of Sharing, Firewalls, and Security
  • Week 12
  • Week 13
  • Week 14
MLA-C01 · AssociateMachine Learning Engineer - Associate
AIF-C01 · FoundationalAI Practitioner - Foundational
DEA-C01 · AssociateData Engineer - Associate
MLS-C01 · SpecialtyMachine Learning - Specialty
SQLD · 개발자SQL Developer (SQLD)

© CertNote · 주간랩스

  • About
  • Changelog
  • Contact
  • Terms
  • Privacy
← Linux Master 1/Week 11/Day 3

Based on the Linux Master 1 exam syllabus·Report an error

Linux Master 1· AssociateWeek 11 · Day 3~23 min read

Day 3 - Firewalls: iptables and firewalld

📌 Key Points

  • The engine of the Linux firewall is the kernel's netfilter. The low-level tool that controls it directly is iptables, and the high-level manager on top of it is firewalld.
  • The iptables structure is table → chain → rule. The chains of the filter table are INPUT (incoming), OUTPUT (outgoing), and FORWARD (passing through).
  • Rules are matched in order from top to bottom, and on a match the packet is handled immediately. If nothing matches, the default policy (-P) applies.
  • 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.
  • firewalld is managed with zones (bundles of trust levels). The biggest trap of firewall-cmd is the distinction between runtime vs --permanent (+ --reload).

netfilter and iptables: The Structure of Tables and Chains

  • iptables controls the kernel's netfilter to inspect and process packets.
  • The structure has three layers: table → chain → rule.
  • The most used table is filter (decides whether to allow or block packets). If you use iptables without options, this table is the default target.
  • Other tables: nat for NAT processing, and mangle for packet alteration.
  • The filter table has three built-in chains, and each chain applies according to the direction of the packet flow.
ChainApplies to
INPUTPackets coming into this system (destination is me)
OUTPUTPackets going out of this system (source is me)
FORWARDPackets 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.

  • The rules in each chain are applied in order from top to bottom.
  • When a packet matches a rule, that rule's target (the handling action) is executed.
  • If it matches no rule, the default policy applies.
  • This "match in order, handle immediately on match" is the most important principle for understanding iptables.

Default Policy and Targets: ACCEPT, DROP, REJECT

  • Each chain has a default policy. It is the last-resort rule that decides the fate of packets that match no rule.
TargetMeaning
ACCEPTAllows the packet
DROPSilently discards the packet (no response — the other side just sees silence)
REJECTRejects the packet and sends back a rejection response (the other side immediately knows it was refused)
LOGRecords 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 DROP first 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.

Writing Rules: -A, -j, and Matching Conditions

  • The key to the practical exam is distinguishing precisely the core options that add and delete rules.
OptionMeaning
-A chainAppend a rule to the end of the chain
-I chain [number]Insert at the very front (or the specified position) of the chain
-D chainDelete a rule
-R chain numberReplace a rule
-LList the rules
-FFlush (delete) all rules in the chain
-P chain targetSet the default policy
-j targetJump: specify the action to take on a match
  • You also need to memorize the options that specify matching conditions.
OptionMeaning
-pProtocol (tcp, udp, icmp)
-sSource IP/network
-dDestination IP/network
--sportSource port
--dportDestination port
-iInput interface (e.g., eth0)
-oOutput interface
  • The following is a typical rule set.
# 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 ACCEPT

The 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.

Checking and Saving Rules

  • You need to know how to check the rules you wrote and how to save them so they persist after a reboot.
# 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 save or iptables-save. It is common to panic about "my rules vanished" without knowing this.

firewalld: A Zone-Based Dynamic Firewall

  • Drawbacks of iptables: it handles rules one by one, so it is not intuitive, and every time you change rules it re-applies everything, which can cut existing connections.
  • firewalld, the default since RHEL 7, is a high-level management tool that improves on this.
  • The core concept of firewalld is the zone — a bundle of rules that pre-defines "the trust level of a network connection".
  • When you assign an interface or a source to a particular zone, that zone's rules apply.
zoneTrust level
dropBlocks all incoming connections with no response (most closed)
blockRejects incoming connections (REJECT response)
public(Default) An untrusted public network. Only selected connections are allowed
home / internalA trusted internal network. Mostly allowed
trustedAllows 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: The Command for Managing firewalld

  • firewalld is controlled with the firewall-cmd command.
  • The most important concept is the distinction between runtime and permanent.
  • By default, changes made with firewall-cmd are applied to the runtime only and disappear after a reboot.
  • To apply permanently, add --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/OptionMeaning
--list-allShow the full settings of the current zone
--add-service=nameAllow a service (http, ssh, etc.)
--remove-service=nameRemove the service permission
--add-port=port/protocolAllow a specific port
--permanentApply permanently (persists after reboot)
--reloadReload the permanent settings into the runtime
--get-servicesShow 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.

iptables vs firewalld Summary

Itemiptablesfirewalld
Abstraction levelLow-level (controls rules directly)High-level (managed per zone)
Core conceptsTables, chains, ruleszone, service
Control commandiptablesfirewall-cmd
Applying changesFull re-apply (connections may be cut)Dynamic apply (existing connections kept)
Persistenceservice iptables save--permanent + --reload
RelationshipControls netfilter directlyUses 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.

📖 Glossary

  • netfilter : The packet filtering framework built into the Linux kernel. Both iptables and firewalld control it.
  • iptables : The traditional command that controls netfilter directly rule by rule. It works in three layers: table → chain → rule.
  • chain : A bundle of rules per packet flow direction. INPUT (incoming), OUTPUT (outgoing), FORWARD (passing through).
  • default policy : The last-resort rule that decides what to do with packets that match no rule. Specified with -P.
  • DROP : A blocking method that silently discards the packet so the other side waits until a timeout.
  • REJECT : A blocking method that immediately sends a rejection response so the other side knows right away it was blocked.
  • -A / -I : Append a rule to the very end of the chain / Insert it at the very front. The order changes the result.
  • firewalld : The default tool on RHEL 7+ that manages the firewall per zone. It does not cut existing connections when rules change.
  • zone : A bundle of rules per network trust level. Examples include public (default), home, trusted, and drop.
  • --permanent / --reload : The option that keeps a setting after reboot / the command that immediately applies that permanent setting to the current session.

📝 Practice Questions

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?

PreviousNFS: The Standard File Sharing of the Unix WorldWeek 11 · Day 2Next Access Control and Security: TCP Wrapper, SELinux, Log ManagementWeek 11 · Day 4

Official references

  • Linux Master 1 official exam syllabus(opens in a new tab)

On this page

  • Key Points
  • netfilter and iptables: The Structure of Tables and Chains
  • Default Policy and Targets: ACCEPT, DROP, REJECT
  • Writing Rules: -A, -j, and Matching Conditions
  • Checking and Saving Rules
  • firewalld: A Zone-Based Dynamic Firewall
  • firewall-cmd: The Command for Managing firewalld
  • iptables vs firewalld Summary
  • Glossary
  • Practice Questions