Course: Network and System Administration

In Class Lab: 14

Name: Managing the firewall on Red Hat

Objective: Install, configure and run the Red Hat firewall. Use firewall-cmd to read and change the rules, and learn the difference between runtime and permanent rules. Then use nft to see what firewalld builds in the kernel, forward a port to your web server to see how NAT works, and write a small firewall of your own.

In Lab 12 you saw that a firewall can let web traffic through and drop ping. In Lab 13 your laptop couldn't reach your web server until you ran a few firewall-cmd commands as a recipe. In this lab you learn what those commands did, and what is underneath them.

Note: This lab is done on your development server, NOT your production server. Connect with SSH from Terminal or PuTTY and log in as your own user. A few steps also use a command on your laptop.

Warning: A firewall mistake can lock you out of your own server. Your development server has a console in UTM or VirtualBox to recover with. Your production server doesn't.

How to use the hints

Some steps give you every command. Others give you a Goal and ask you to work out the commands yourself. If you get stuck, open the hint. Once you have an answer, or if you are really stuck, open the solution and compare it with your own.

Lab overview

  1. Part 1: Install and check the firewall - Task 1
  2. Part 2: Read the zones - Task 2
  3. Part 3: Services and ports - Task 3
  4. Part 4: Change the rules - Tasks 4 and 5
  5. Part 5: Look underneath with nftables - Task 6
  6. Part 6: Port forwarding and NAT - Task 7
  7. Part 7: Build your own firewall - Tasks 8 and 9
  8. Part 8: Save your notes in GitLab - Task 10

If you get stuck, check the Troubleshooting section at the end of this lab.

The examples in this lab come from a UTM server, so they use 192.168.64.7 and enp0s1. Always use your values.


Part 1: Install and check the firewall

A firewall decides which network traffic is allowed in and out of a server. The Red Hat firewall has three pieces:

Piece What it is
firewalld The daemon. It runs all the time, keeps your saved rules, and loads them into the kernel.
firewall-cmd The command line tool you use to talk to firewalld.
nftables The packet filter in the Linux kernel that checks each packet against the rules. You can see it with the nft command.

Tip: A daemon is a program that runs in the background and handles a task for the system, without a user at the keyboard. You have already worked with the sshd, NetworkManager and nginx daemons. Many daemon names end in d.

Note: Older guides on the internet use iptables, the tool that came before nftables. There is also a graphical tool for firewalld, firewall-config. This course uses neither.

Red Hat installs and turns on the firewall by default, but not every server was built the same way. You install a firewall as you did Nginx in Lab 13. If the packages are already there, dnf tells you, and changes nothing:

sudo dnf install -y firewalld nftables

Now check that the service is running, and that it starts after a reboot:

systemctl status firewalld systemctl is-enabled firewalld sudo firewall-cmd --state
Command What it does
systemctl status firewalld Shows enabled and active (running). Press q to quit if it opens a pager.
systemctl is-enabled Prints only enabled or disabled.
firewall-cmd --state Asks firewalld itself. It prints running.

If your server's firewall were off, this one command would do both the enable and the start from Lab 13:

sudo systemctl enable --now firewalld

What does enable do?

Turn off the setting for a moment, and read the messages. Then check whether the firewall is still running:

sudo systemctl disable firewalld systemctl is-enabled firewalld systemctl is-active firewalld sudo systemctl enable firewalld
Removed "/etc/systemd/system/multi-user.target.wants/firewalld.service". Removed "/etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service". disabled active Created symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service → /usr/lib/systemd/system/firewalld.service. Created symlink /etc/systemd/system/multi-user.target.wants/firewalld.service → /usr/lib/systemd/system/firewalld.service.

The firewall stayed active while it was disabled. As you saw in Lab 13, enable and disable only change what happens after a reboot. They do that by creating and removing the two files named in the messages. A symlink, or symbolic link, is a file that points to another file, like a shortcut.

Goal: Show that the two files named by enable are links, and find what they point to. Do you get two different configurations, or one? This is a preview. You look at service files in more detail in Lab 18.

Hint
Solution
ls -l /etc/systemd/system/multi-user.target.wants/firewalld.service ls -l /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service cat /etc/systemd/system/multi-user.target.wants/firewalld.service cat /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service
lrwxrwxrwx. 1 root root 41 Oct 31 14:02 /etc/systemd/system/multi-user.target.wants/firewalld.service -> /usr/lib/systemd/system/firewalld.service lrwxrwxrwx. 1 root root 41 Oct 31 14:02 /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service -> /usr/lib/systemd/system/firewalld.service

There is one configuration, /usr/lib/systemd/system/firewalld.service, with two links to it. The link in multi-user.target.wants is what starts the firewall at boot. The other gives the same service a second name, an alias, so that other programs can find it.


Part 2: Read the zones

firewalld groups its rules into zones. A zone is a named set of rules for one level of trust. Each network interface belongs to one zone, and its traffic follows that zone's rules.

Red Hat provides a set of zones, and you can add your own. Your server has one interface, so only one zone is active. A server with two interfaces could use a different zone on each:

a server with one interface in the public zone and one in the internal zone

List the zones, and find out which one you are using:

sudo firewall-cmd --get-zones sudo firewall-cmd --get-default-zone sudo firewall-cmd --get-active-zones

firewall-cmd showing that the active zone is public

Yours says public too, with your own interface name. Your list of zones may also include nm-shared, which comes from NetworkManager.

Command What it shows
--get-zones Every zone that exists.
--get-default-zone The zone a new interface joins, and the zone used when a command doesn't name one.
--get-active-zones The zones in use right now, and their interfaces.

Now read the rules in your zone:

sudo firewall-cmd --zone=public --list-all
public (active) target: default icmp-block-inversion: no interfaces: enp0s1 sources: services: cockpit dhcpv6-client http ssh ports: protocols: forward: yes masquerade: no forward-ports: source-ports: icmp-blocks: rich rules:
You see It means
public (active) The zone's name, and that an interface is using it.
target: default What happens to traffic that no rule allows. In public, it is refused.
interfaces: enp0s1 The interface in this zone.
services: What is allowed in, by name. You added http in Lab 13.
ports: What is allowed in, by port number. It is empty for now.

You use masquerade and forward-ports in Part 6. The other lines are for features this lab doesn't use. Because public is your default zone, sudo firewall-cmd --list-all gives you the same answer. To see every zone at once, use sudo firewall-cmd --list-all-zones | less.

Task 2: Compare the zones

Goal: Find the target of the drop, block and trusted zones, without changing anything. Going by the names and the targets, what do you think each zone does to traffic that no rule allows? Would you want your interface in the trusted zone on the internet?

Hint
Solution
sudo firewall-cmd --zone=drop --list-all | grep target sudo firewall-cmd --zone=block --list-all | grep target sudo firewall-cmd --zone=trusted --list-all | grep target
target: DROP target: %%REJECT%% target: ACCEPT
Zone Traffic no rule allows
drop Thrown away with no answer. The sender waits until it gives up.
block Refused. The sender gets an answer saying so, and fails right away.
trusted Accepted. All traffic is allowed in.

trusted is a poor choice on the internet: it allows everything in, so it is the same as having no firewall.


Part 3: Services and ports

The services line in your zone uses names, but a firewall works with ports. firewalld keeps a definition for each service name. Ask it about ssh:

sudo firewall-cmd --info-service=ssh
ssh ports: 22/tcp protocols: source-ports: modules: destination: includes: helpers:

So allowing the ssh service means allowing TCP port 22. The definitions are small XML files. See how many there are, and read this one:

ls /usr/lib/firewalld/services | wc -l cat /usr/lib/firewalld/services/ssh.xml

firewall-cmd --get-services prints all the names on one long line, so grep would match that whole line. Use tr from Lab 3 to put each name on its own line first:

sudo firewall-cmd --get-services sudo firewall-cmd --get-services | tr ' ' '\n' | grep ftp sudo firewall-cmd --get-services | tr ' ' '\n' | grep bitcoin

Having a definition doesn't allow anything. A service is only allowed once you add it to a zone, and bitcoin isn't in yours.

Note: /etc/services, from Lab 12, is a different list. It is a general list of names for well known ports, and firewalld doesn't use it for its services.

Task 3: Find the cockpit port

Your zone also allows cockpit, a web interface for managing a server. You haven't used it in this course.

Goal: Find which port the cockpit service opens, in two ways: by asking firewall-cmd, and by reading its definition file. Then search /etc/services for the word cockpit. Does it find anything? What name does /etc/services have for that port?

Hint
Solution
sudo firewall-cmd --info-service=cockpit cat /usr/lib/firewalld/services/cockpit.xml grep cockpit /etc/services grep -w 9090 /etc/services

Cockpit uses TCP port 9090. grep cockpit finds nothing, because /etc/services lists that port under the name websm, for web based service managers. This shows that the two lists are separate, and that firewalld gets its port from cockpit.xml.

You can check the other service in your zone with grep "^ssh \|^dhcpv6-client " /etc/services. The \| means "or", as in Lab 13.


Part 4: Change the rules

Most of the commands in this part name the zone. To save typing, keep the zone's name in a shell variable. You have read variables such as $USER, and in Lab 11 you made your own in a script. It is empty until you set it:

echo $DEFAULT_ZONE DEFAULT_ZONE=$(sudo firewall-cmd --get-default-zone) echo $DEFAULT_ZONE sudo firewall-cmd --zone=$DEFAULT_ZONE --list-all

$( ) is the command substitution from Lab 7: the shell runs the command, and uses its output as the value. The variable only exists in this shell, so it is gone when you log out. You learn more about variables in Lab 20.

Your output should look something like this, with the http service from Lab 13. This one is from a VirtualBox server, so it shows enp0s3:

firewall-cmd list-all output with the services line highlighted

Runtime and permanent rules

firewalld keeps two sets of rules:

Set Where it lives How you change it How long it lasts
Runtime In memory. These rules are active. firewall-cmd with no --permanent Until the next reload or reboot.
Permanent In files on the disk. These rules are saved. firewall-cmd --permanent Until you change them. They aren't active until you reload.

--reload throws away the runtime rules, and builds them again from the permanent rules.

You can see this with your web server. On your laptop, open Terminal or Command Prompt, and fetch your page using your own address. --max-time 5 makes curl give up after 5 seconds, instead of waiting:

Hypervisor Command on your laptop
UTM (Mac) curl -I --max-time 5 http://192.168.64.7
VirtualBox (Windows) curl -I --max-time 5 http://127.0.0.1:8000

You should get 200 OK. Now, on your server, remove http from the runtime rules only, and compare the two sets:

sudo firewall-cmd --zone=$DEFAULT_ZONE --remove-service=http sudo firewall-cmd --zone=$DEFAULT_ZONE --list-services sudo firewall-cmd --permanent --zone=$DEFAULT_ZONE --list-services
success cockpit dhcpv6-client ssh cockpit dhcpv6-client http ssh

The active rules no longer have http, and the saved rules still do. Run your curl command on your laptop again. It should fail. The error depends on your laptop and hypervisor, and you may see "No route to host", "Empty reply" or a timeout.

Note: Use curl for this test, not your browser. A browser can show you a saved copy of the page, or keep using a connection it already has open, and a firewall normally leaves those connections alone. You see why in Task 9.

Now reload, and run curl on your laptop once more:

sudo firewall-cmd --reload sudo firewall-cmd --zone=$DEFAULT_ZONE --list-services

http is back, because it was saved, and curl gets 200 OK again. This gives you a safe way to try a change. If a runtime change goes badly, --reload undoes it, and so does a reboot.

Allow a port by number

When there is no service definition for a program, you allow its port by number. Add TCP port 8080 to the permanent rules, and compare the two sets:

sudo firewall-cmd --permanent --zone=$DEFAULT_ZONE --add-port=8080/tcp sudo firewall-cmd --zone=$DEFAULT_ZONE --list-ports sudo firewall-cmd --permanent --zone=$DEFAULT_ZONE --list-ports sudo firewall-cmd --reload sudo firewall-cmd --zone=$DEFAULT_ZONE --list-ports
Command What you see
First --list-ports An empty line. The port isn't active yet.
--permanent --list-ports 8080/tcp. The port is saved.
--reload, then --list-ports 8080/tcp. The port is now active.

Question: Port 8080 is now open in your firewall. Would your laptop get a web page from it? Why or why not?

Answer

No. A firewall only decides whether traffic may reach the server. A program must also be listening on that port, and sudo ss -tlnp | grep 8080 shows that nothing is. Your laptop's connection would be refused by the server.

Task 4: Add a port twice, then clean up

Goal: Permanently add port 8080 for UDP, and run the same command a second time. What happens the second time? Then remove both 8080 rules, the TCP one and the UDP one, and show that no ports are left open, in either set of rules.

Hint
Solution
sudo firewall-cmd --permanent --zone=$DEFAULT_ZONE --add-port=8080/udp sudo firewall-cmd --permanent --zone=$DEFAULT_ZONE --add-port=8080/udp
success Warning: ALREADY_ENABLED: 8080:udp success

The second command warns you, and doesn't add a second rule. It is safe to run a command like this twice. Now clean up:

sudo firewall-cmd --permanent --zone=$DEFAULT_ZONE --remove-port=8080/udp sudo firewall-cmd --permanent --zone=$DEFAULT_ZONE --remove-port=8080/tcp sudo firewall-cmd --reload sudo firewall-cmd --zone=$DEFAULT_ZONE --list-ports sudo firewall-cmd --permanent --zone=$DEFAULT_ZONE --list-ports

Both --list-ports commands print an empty line.

Task 5: Add a service and find where it is saved

The permanent rules for your zone are saved in the file /etc/firewalld/zones/public.xml.

Goal: Permanently allow the https service, and make it active. Read the zone's file to see your change. Then remove https, and show that your services are back to the way they were. You allow https for real in Lab 17, when your web server is ready for it.

Hint
Solution
sudo firewall-cmd --permanent --zone=$DEFAULT_ZONE --add-service=https sudo firewall-cmd --reload sudo firewall-cmd --zone=$DEFAULT_ZONE --list-services sudo cat /etc/firewalld/zones/public.xml
<?xml version="1.0" encoding="utf-8"?> <zone> <short>Public</short> <description>For use in public areas. ...</description> <service name="ssh"/> <service name="dhcpv6-client"/> <service name="cockpit"/> <service name="http"/> <service name="https"/> <forward/> </zone>

Each permanent rule is one line in this file. Use firewall-cmd to change it, as it checks your work and keeps a backup, public.xml.old, in the same directory. Now remove the service:

sudo firewall-cmd --permanent --zone=$DEFAULT_ZONE --remove-service=https sudo firewall-cmd --reload sudo firewall-cmd --zone=$DEFAULT_ZONE --list-services
cockpit dhcpv6-client http ssh

Part 5: Look underneath with nftables

firewalld doesn't check packets itself. It turns your zones into nftables rules, and the kernel checks each packet against those. The nft command shows you what the kernel has:

sudo nft list tables sudo nft list ruleset | wc -l sudo nft list ruleset | less

Press q to leave less. nftables organizes its rules like this:

Term What it is
Table A container for chains. firewalld uses a table called firewalld.
Chain An ordered list of rules. Some chains are connected to a point in the kernel where packets pass by, such as packets arriving for this server.
Rule A test and an action, such as tcp dport 22 accept. dport means destination port.

A few lines in firewall-cmd become hundreds of lines here. This is why people use firewalld. As you scroll, look for things you recognize, such as your interface name and the names of zones.

Warning: Look, but don't change the firewalld table with nft. firewalld builds it again on the next reload, and your changes are lost.

Task 6: Find your rules in nftables

Goal: Find the nftables rules for ssh and http, using the port numbers you learned in Part 3. Which chain are they in? Then remove http from the runtime rules, as you did in Part 4, and show that the rule goes away in nftables. Reload to bring it back.

Hint
Solution
sudo nft list ruleset | grep "dport 22\|dport 80"
tcp dport 22 accept tcp dport 80 accept

Use a pipe and less to find them in a chain for your zone sudo nft list ruleset | less:

chain filter_IN_public_allow { tcp dport 22 accept ip6 daddr fe80::/64 udp dport 546 accept tcp dport 9090 accept tcp dport 80 accept }

These are your services: ssh, dhcpv6-client, cockpit and http. Now change the runtime rules, and look again:

sudo firewall-cmd --zone=$DEFAULT_ZONE --remove-service=http sudo nft list ruleset | grep "dport 22\|dport 80" sudo firewall-cmd --reload sudo nft list ruleset | grep "dport 22\|dport 80"

The dport 80 line disappears, then comes back. The runtime rules are the ones in the kernel.


Part 6: Port forwarding and NAT

You have used port forwarding since Lab 1, if you are on VirtualBox: connections to port 2222 on your laptop arrive at port 22 on your server. In Lab 12 you saw NAT, network address translation, when every device in your home shared one public address. Both work the same way: a router or firewall rewrites the addresses or ports in a packet as it passes through, and remembers the change so that it can undo it on the answer.

Kind of NAT What gets rewritten Where you have seen it
Source NAT The source address of packets going out, so the answers come back to the router. firewalld calls this masquerade. Your home router, and your hypervisor's network, in Lab 12.
Destination NAT The destination address or port of packets coming in. This is port forwarding. VirtualBox's rules, 2222 → 22 and 8000 → 80.

firewalld can do both. They are the masquerade and forward-ports lines in --list-all, from Part 2. Your server has only one network, so you only try port forwarding.

Forward a port to your web server

Nginx listens on port 80 only. Ask firewalld to forward port 8080 to it. This is a runtime change, so a reload removes it:

sudo ss -tlnp | grep nginx sudo firewall-cmd --zone=$DEFAULT_ZONE --add-forward-port=port=8080:proto=tcp:toport=80 sudo firewall-cmd --zone=$DEFAULT_ZONE --list-forward-ports
port=8080:proto=tcp:toport=80:toaddr=
Part What it means
port=8080 The port that traffic arrives on.
proto=tcp Only TCP traffic.
toport=80 The port to send it to instead.
toaddr= Empty, so the traffic stays on this server. With an address here, firewalld would send it on to another computer, which also needs masquerade.

You don't need to open port 8080. The firewall checks the packet after the port has been changed to 80, which http already allows.

VirtualBox users need one more VirtualBox rule first, as in Lab 13: VM Settings, Network, Port Forwarding. Name http-alt, protocol tcp, host port 8080, guest port 8080. Leave the IP fields blank. Now try port 8080 from your laptop:

Hypervisor Command on your laptop
UTM (Mac) curl -I --max-time 5 http://192.168.64.7:8080
VirtualBox (Windows) curl -I --max-time 5 http://127.0.0.1:8080

You get 200 OK and your own page, even though nothing on your server listens on port 8080. On VirtualBox, your request was forwarded twice: VirtualBox sent laptop port 8080 to server port 8080, then firewalld sent it on to port 80.

Task 7: Follow the forwarded request

Goal: Show that Nginx never knew about port 8080. Find your request in the Nginx access log, and check which ports Nginx listens on. Then try curl -I http://localhost:8080 on your server. Does it work? Why or why not? Find the forwarding rule in nftables, and finally remove it with a reload.

Hint
Solution
sudo tail -n 2 /var/log/nginx/access.log sudo ss -tlnp | grep nginx curl -I --max-time 5 http://localhost:8080 sudo nft list ruleset | grep 8080
192.168.64.1 - - [09/Nov/2026:10:12:41 -0800] "HEAD / HTTP/1.1" 200 0 "-" "curl/8.7.1" LISTEN 0 511 0.0.0.0:80 0.0.0.0:* users:(("nginx",pid=2212,fd=6),("nginx",pid=2211,fd=6)) curl: (7) Failed to connect to localhost port 8080: Connection refused meta nfproto ipv4 tcp dport 8080 redirect to :80
You see It means
The access log line Nginx saw an ordinary request. HEAD is the method curl -I uses. The log doesn't show port 8080, because the port was changed before the packet reached Nginx.
ss shows only :80 Nothing listens on 8080. The forward is in the firewall, not in Nginx.
localhost:8080 fails Your server talking to itself uses lo, and doesn't pass through the forwarding rule for your network interface.
redirect to :80 The kernel rule that firewalld built. redirect is destination NAT to this same server. Your output may also have a line for IPv6.

Now remove the forward, and check that it is gone:

sudo firewall-cmd --reload sudo firewall-cmd --zone=$DEFAULT_ZONE --list-forward-ports

--list-forward-ports prints an empty line, and curl to port 8080 from your laptop fails again. VirtualBox users can delete the http-alt rule.

Question: Your server's request to ifconfig.io in Lab 12 went through at least two NAT devices on its way to the internet. Which ones? What source address did each one put on the packet?

Answer

Your hypervisor, UTM or VirtualBox, replaced your server's private address with your laptop's address on your home network. Then your home router replaced that address with your home's public address, which is what ifconfig.io showed you. Both are source NAT, the same as firewalld's masquerade.


Part 7: Build your own firewall

firewall-cmd is the preferred tool on Red Hat 9, and you use it for the rest of this course. Writing a small firewall with nft shows you how a firewall really works.

Only one tool should manage the firewall at a time, so stop firewalld first. Then start the nftables service:

sudo systemctl stop firewalld sudo systemctl start nftables systemctl status nftables sudo nft list ruleset

nft list ruleset prints nothing. Stopping firewalld removed all of its rules, and your server currently has no firewall. Run your curl command from your laptop to confirm that it still gets 200 OK.

systemctl status says active (exited), not running. nftables isn't a daemon. The service loads a rules file into the kernel and exits, and the kernel does the rest. The rules file is empty for now.

Note: systemctl stop firewalld doesn't change the setting from Part 1. If anything goes wrong in this part, a reboot gives you back your firewalld rules.

Create a table and chains

Create a table called itfdnfw, with a chain for each direction of traffic:

sudo nft add table inet itfdnfw sudo nft list tables sudo nft add chain inet itfdnfw input '{ type filter hook input priority 0; policy accept; }' sudo nft add chain inet itfdnfw forward '{ type filter hook forward priority 0; policy accept; }' sudo nft add chain inet itfdnfw output '{ type filter hook output priority 0; policy accept; }' sudo nft list ruleset

Note: The single quotes stop the shell from reading the braces and semicolons itself, so that they reach nft.

Part What it does
inet The table handles both IPv4 and IPv6.
itfdnfw The table's name, which you choose.
input The chain's name, which you choose. Using the same name as the hook keeps it readable.
type filter The chain decides whether to allow packets.
hook input Packets arriving for this server. forward is for packets passing through to somewhere else, and output is for packets this server sends.
priority 0 When this chain runs compared to other chains on the same hook.
policy accept What happens to a packet when no rule in the chain decides.

Add rules

With a policy of accept, this firewall allows everything. You change that soon, but it needs rules first, or you would lock yourself out. add puts a rule at the end of a chain, and insert puts it at the start. Rules are checked in order, and the first one to accept a packet ends the check.

sudo nft add rule inet itfdnfw input tcp dport 22 accept sudo nft insert rule inet itfdnfw input ct state established,related accept sudo nft insert rule inet itfdnfw input iifname lo accept sudo nft list ruleset
table inet itfdnfw { chain input { type filter hook input priority filter; policy accept; iifname "lo" accept ct state established,related accept tcp dport 22 accept } chain forward { type filter hook forward priority filter; policy accept; } chain output { type filter hook output priority filter; policy accept; } }
Rule What it allows
iifname "lo" accept Everything arriving on the loopback interface, lo, from Lab 9. This is the server talking to itself.
ct state established,related accept Packets that belong to a connection the firewall has already seen. ct stands for connection tracking. This makes it a stateful firewall.
tcp dport 22 accept New SSH connections.

Now change the input policy to drop. Add a chain with the same name, and nft updates its policy:

Warning: Check that your tcp dport 22 accept rule is in the list before you run this. Without it, you lose SSH, and you need your console. See Troubleshooting.

sudo nft add chain inet itfdnfw input '{ type filter hook input priority 0; policy drop; }' sudo nft list chain inet itfdnfw input

Your SSH session is still working, because the rules allow it. This is a real firewall now: anything the rules don't allow is thrown away. Run your curl command from your laptop. It fails after 5 seconds with a timeout, because drop gives no answer at all.

Task 8: Let the web back in

Goal: Add a rule to your input chain that allows web traffic to your server. Show the rule in the chain, and use curl on your laptop to prove that it works.

Hint
Solution
sudo nft add rule inet itfdnfw input tcp dport 80 accept sudo nft list chain inet itfdnfw input
table inet itfdnfw { chain input { type filter hook input priority filter; policy drop; iifname "lo" accept ct state established,related accept tcp dport 22 accept tcp dport 80 accept } }

curl on your laptop gets 200 OK. This is what firewall-cmd --add-service=http did for you in Lab 13, as you saw in Task 6.

Task 9: What does the established rule do?

Your server can't accept new connections except for SSH and the web, but it can still make its own. The output chain allows them. Check that with curl -I --max-time 5 https://www.washington.edu.

To remove one rule, you need its handle, a number that nft gives each rule. sudo nft -a list chain inet itfdnfw input shows them at the end of each line, and sudo nft delete rule inet itfdnfw input handle N removes one.

Goal: Predict what happens to your server's own curl and ping without the ct state established,related accept rule. Then delete the rule and test it. Does your SSH session keep working? Why? Put the rule back with insert, and test again.

Hint
Solution
sudo nft -a list chain inet itfdnfw input
table inet itfdnfw { # handle 3 chain input { # handle 1 type filter hook input priority filter; policy drop; iifname "lo" accept # handle 6 ct state established,related accept # handle 5 tcp dport 22 accept # handle 4 tcp dport 80 accept # handle 7 } }

Use the handle from your output, not the number here:

sudo nft delete rule inet itfdnfw input handle 5 curl -I --max-time 5 https://www.washington.edu ping -c 2 8.8.8.8
curl: (28) Resolving timed out after 5000 milliseconds 2 packets transmitted, 0 received, 100% packet loss, time 1024ms

The requests go out, but the answers are dropped on the way back in. To the input chain, an answer from a DNS server looks like any other packet, as nothing allows it. The established rule is what lets answers in, without opening any ports. This is why a firewall leaves open connections alone, and why you used curl to test in Part 4.

Your SSH session keeps working, because the tcp dport 22 accept rule still matches its packets. Now put the rule back:

sudo nft insert rule inet itfdnfw input ct state established,related accept curl -I --max-time 5 https://www.washington.edu

Go back to firewalld

Your rules exist only in the kernel, so they would be gone after a reboot. You don't need them after this lab. Clear everything, then go back to firewalld:

sudo nft flush ruleset sudo nft list ruleset sudo systemctl stop nftables sudo systemctl start firewalld sudo firewall-cmd --state sudo firewall-cmd --zone=$DEFAULT_ZONE --list-services sudo nft list tables

nft flush ruleset removes every rule, chain and table. firewalld then builds its rules again from its saved files, so your services are back, including http. Run curl on your laptop one last time to check that you get 200 OK.

Note: To keep nft rules across reboots, you would save them to a file, include that file in /etc/sysconfig/nftables.conf, disable firewalld and enable the nftables service. Don't do this in this course. If you try it on another server, remember from Lab 5 that sudo nft list ruleset > file fails, because your own shell does the redirect. Use sudo nft list ruleset | sudo tee file.


Part 8: Save your notes in GitLab

Add what you have learned to your GitLab repository from Lab 10:

cd ~/git/gitlab/itfdn-140-inclass-$USER git pull echo "# Firewalld and firewall-cmd The following are notes and tips related to **firewalld**, **firewall-cmd** and **nft**. " >> firewalld-tips.md cat firewalld-tips.md

Goal: Add at least three tips or commands to firewalld-tips.md that you would want next time. Add a link to the file at the end of README.md. Commit both files in one commit, and push. In GitLab, click the link in your README and check that it opens your notes.

Hint
Solution
vim firewalld-tips.md

Add your own tips, then save with :wq. For example:

## Permanent and runtime rules `--permanent` only changes the saved rules. Run `sudo firewall-cmd --reload` to make them active. ## See what a zone allows `sudo firewall-cmd --zone=public --list-all` ## Allow a service, or a port `sudo firewall-cmd --permanent --zone=public --add-service=http` `sudo firewall-cmd --permanent --zone=public --add-port=8080/tcp`

Add the link, then check, commit and push. The empty echo "" keeps the link on its own line:

echo "" >> README.md echo "[firewalld and firewall-cmd](firewalld-tips.md)" >> README.md git status git diff git add README.md firewalld-tips.md git commit -m "docs: add firewalld tips and link from README" git push git log --oneline

Lab summary

You have now:

Your firewall ends this lab the way it started: firewalld is running and enabled, and the public zone allows cockpit, dhcpv6-client, http and ssh.

Questions to consider

  1. What is the difference between firewalld, firewall-cmd and nftables?
  2. You add a rule with --permanent, but --list-all doesn't show it. Why? What do you run next?
  3. You add a rule without --permanent, and it works. What happens to it after a reboot?
  4. Why is it safer to test a change without --permanent when you are connected over SSH?
  5. What does your laptop notice when a firewall drops a packet? What about when it rejects one?
  6. Your laptop reaches your page on port 8080, but ss shows nothing listening there. How is that possible? Why doesn't curl http://localhost:8080 work on the server?
  7. Why does a server with a drop policy on its input chain need an established rule, even though it only makes connections going out?
  8. Your production server has no console. What could happen if you made a mistake with its firewall? What would you check before you pressed Enter?

Troubleshooting

Q. firewall-cmd says "FirewallD is not running".

A. You stopped it in Part 7, or it wasn't started. Run sudo systemctl start firewalld, then sudo firewall-cmd --state.

Q. firewall-cmd says "Authorization failed".

A. You left out sudo. Run the command again with sudo !!.

Q. I get a zone error from a command that uses $DEFAULT_ZONE, or the command seems to have no zone.

A. The variable only exists in the shell where you set it. If you logged out or opened a second session, set it again with DEFAULT_ZONE=$(sudo firewall-cmd --get-default-zone). Check it with echo $DEFAULT_ZONE.

Q. I added a rule, but it doesn't appear in --list-all.

A. You used --permanent, and those rules aren't active until you run sudo firewall-cmd --reload. To see the saved rules, add --permanent to the list command.

Q. My rule disappeared after a reload or reboot.

A. You added it without --permanent. Add it again with --permanent, then run sudo firewall-cmd --reload.

Q. I removed http, but my browser still shows my page.

A. Your browser saved a copy, or kept its connection open. Use curl -I --max-time 5 on your laptop, which makes a new connection each time.

Q. curl on my laptop fails, even with http allowed.

A. Check these in order:

  1. On your server, run curl -I http://localhost. If this fails, Nginx is the problem. See Lab 13.
  2. Run sudo firewall-cmd --zone=public --list-services, and look for http.
  3. Check that you are using your own address, with ip -br address.
  4. VirtualBox: check your port forwarding rule for host port 8000 and guest port 80, and use http://127.0.0.1:8000.

Q. curl to port 8080 from my laptop fails in Part 6.

A. Check that the forward is there with sudo firewall-cmd --zone=$DEFAULT_ZONE --list-forward-ports. It is a runtime rule, so a reload removes it. Check that curl -I http://localhost works on your server, since the forward only reaches Nginx on port 80. VirtualBox: check your http-alt rule for host port 8080 and guest port 8080. curl http://localhost:8080 on the server itself is expected to fail.

Q. nft says "No such file or directory" when I add a rule or chain.

A. The table or chain name doesn't exist, or it is misspelled. Check with sudo nft list ruleset. The table must exist before its chains, and the chain before its rules.

Q. nft says "syntax error", or the shell gives an error about a token.

A. Check the single quotes around the braces, and the semicolons inside them. Copy the command from the lab, and paste it again.

Q. I lost my SSH session after changing the policy to drop, and I can't reconnect.

A. Your SSH rule is missing or wrong. Log in on your UTM or VirtualBox console, then run sudo nft flush ruleset to remove your rules. You can then reconnect with SSH. A reboot also clears your nft rules and starts firewalld again.

Q. After Part 7, my server has no firewall, or nft list tables still shows itfdnfw.

A. Run the commands in Go back to firewalld again.

Q. git push is rejected.

A. Your GitLab repository has a change you don't have yet. Run git pull, then git push again, as in Lab 10.

If you can't solve a problem, post it in this week's Teams channel, or reach out by email for help.


Lab complete