Filters
Zo zoek je
  • Gewone woorden vinden namen, teksten en voorbeelden.
  • "twee woorden" vindt precies die zin.
  • --naam of -x vindt een optie.
  • #woord zoekt alleen in commando's.
  • @grep vindt lessen en labs die grep gebruiken.
  • Filters: cat:network · in:labs · in:notes · level:rhcsa · ch:8 · root:yes. Zet ! voor een woord om het weg te laten. OR tussen woorden betekent één ervan.
  • ↑ ↓ bewegen · Enter openen · Esc sluiten · / zoeken

Hoofdstuk 23Naam in het boek: Configuring a Firewall

Open and close ports with firewalld

A firewall decides which network traffic may reach the server. RHEL uses firewalld on top of the kernel's netfilter (nftables). This chapter explains zones and services, and shows how to open services and ports, make changes permanent, and check what is open.

  • 10 Lessen
  • 12 Commando's
  • 1 Labs

Dit deel is nog niet vertaald. Je ziet de Engelse tekst.

$ als gewone gebruiker# als root (of met sudo)

Examendoelen in dit hoofdstuk

  • Netwerktoegang beperken met firewalld en firewall-cmd
  • Firewallregels instellen met firewall-cmd en firewalld

23.1

Understand Linux firewalling

The kernel part is netfilter. Tools write rules into it: first iptables, now nftables. On RHEL you normally do not write nftables rules yourself — firewalld does it for you with zones and services.

ToolRole on RHEL 10
netfilterKernel framework that filters packets
nftables (nft)Rule engine and low-level tool (firewalld backend)
iptablesOld tool; replaced by nftables
firewalld (firewall-cmd)The service and tool you use
systemctl status firewalld
nft list ruleset | head -20

Let op

Do not mix your own nft or iptables rules with firewalld; firewalld may overwrite them at reload.

23.2

Understand zones

A zone is a trust level with its own rules. Every network interface (or source address) belongs to one zone. Traffic is checked by the rules of that zone.

ZoneDefault behaviour
publicDefault on RHEL: only selected services (ssh, cockpit, dhcpv6-client)
dropDrop everything incoming, no answer
blockReject incoming with an ICMP message
external, dmz, work, home, internalDifferent preset services
trustedAllow everything
firewall-cmd --get-default-zone
firewall-cmd --get-active-zones
firewall-cmd --get-zones
firewall-cmd --list-all
firewall-cmd --list-all --zone=internal

23.3

Understand services

A firewalld service is a name for one or more ports (and helpers). http = 80/tcp, ssh = 22/tcp, nfs = 2049/tcp … Using names keeps rules readable.

firewall-cmd --get-services | tr ' ' '\n' | grep -E '^(http|https|ssh|nfs|samba)$'
firewall-cmd --info-service=http
ls /usr/lib/firewalld/services/ | head

You can define your own service in /etc/firewalld/services/NAME.xml.

23.4

Open services and ports

Add a service or port to the zone. Without --permanent it is lost at reload or reboot; with --permanent it is not active until a reload.

  1. Open a service and a port permanently, then reload.
    firewall-cmd --add-service=http --permanent
    firewall-cmd --add-port=8443/tcp --permanent
    firewall-cmd --reload
  2. Check.
    firewall-cmd --list-services
    firewall-cmd --list-ports
  3. Close them again.
    firewall-cmd --remove-service=http --permanent
    firewall-cmd --remove-port=8443/tcp --permanent
    firewall-cmd --reload
WayEffect
without --permanentActive now, gone after reload/reboot
--permanent + --reloadSaved and active
--runtime-to-permanentSave all current runtime rules

Examentip

Use --permanent and --reload for every firewall task, and check with firewall-cmd --list-all. Test from another machine.

23.5

Work with zones and sources

Change the default zone, move an interface to another zone, or give a source network its own zone.

firewall-cmd --set-default-zone=public
firewall-cmd --zone=internal --change-interface=enp2s0 --permanent
firewall-cmd --zone=trusted --add-source=192.168.122.0/24 --permanent
firewall-cmd --reload
firewall-cmd --get-active-zones

Notitie

NetworkManager can store the zone of a connection: nmcli con mod enp2s0 connection.zone internal.

23.6

Use rich rules and forwarding

Rich rules allow more detail: a service only for one network, logging, rejecting one IP. Port forwarding sends traffic to another port or host.

firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="192.168.122.0/24" service name="mysql" accept'
firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="203.0.113.50" drop'
firewall-cmd --permanent --add-forward-port=port=80:proto=tcp:toport=8080
firewall-cmd --reload
firewall-cmd --list-rich-rules

Op het werk

Open only what you need, and limit admin services (ssh, cockpit, databases) to admin networks with rich rules or a separate zone.

23.7

Test the firewall

Check the rules on the server, then test from outside.

firewall-cmd --list-all
firewall-cmd --query-service=http
yes
curl -I http://server1
nc -zv server1 8443
firewall-cmd --set-log-denied=all
journalctl -k | grep -i 'filter_IN_public_REJECT'

23.8

Remember the main points

The most important facts of this chapter, in short.

  • firewalld manages the firewall (nftables underneath); the tool is firewall-cmd.
  • Zones set trust levels; every interface or source belongs to one zone; the default zone is public.
  • Open a service (--add-service=http) or a port (--add-port=8080/tcp).
  • Changes without --permanent are lost at reload or reboot; with --permanent they wait until --reload. Do both, or use --runtime-to-permanent.
  • firewall-cmd --list-all shows the active zone; --get-services lists known services.
  • Rich rules allow more detail, for example one source network for one service.
  • Test from another host with curl, nc or nmap.

23.9

Learn the key terms

The words of this chapter in simple English.

TermMeaning
firewalldThe firewall service of RHEL.
ZoneA set of rules with one trust level.
Default zoneThe zone for interfaces that have no other zone.
ServiceA name for one or more ports, like ssh or http.
Runtime configThe rules that are active now.
Permanent configThe rules saved on disk for the next start.
Rich ruleA detailed rule with source, service, port and action.
nftablesThe kernel firewall that firewalld uses.

Commando's in deze les

23.10

Check what you learned

Think about the answer first, then open it.

1How do you open the https service now and for good?
2How do you open port 2022/tcp for good?
3How do you see all rules of the active zone?
4How do you see which zone is the default?
5How do you put interface enp2s0 in the zone internal?

firewall-cmd --permanent --zone=internal --change-interface=enp2s0, then --reload (or set the zone in the NetworkManager profile).

6How do you allow SSH only from 192.168.122.0/24?
7You added a rule without --permanent. What happens at reboot?

It is gone.

8Which file holds the ports of a known service?

The service XML file in /usr/lib/firewalld/services/ (your own in /etc/firewalld/services/).

Oefenen

Alle commando's in dit hoofdstuk