Managing Server Firewalls¶
Objectives¶
- Accept or reject network connections to system services with
firewalld
rules.
Firewall Architecture Concepts¶
- Netfilter Framework: Provides packet filtering, network address translation, and port translation. Includes hooks for kernel modules to interact with network packets.
- nftables Framework: Builds on the
netfilter
framework, replacing the deprecatediptables
in Red Hat Enterprise Linux 9, offering improved usability and efficiency.
You can read more here
Netilter Examples¶
Example 1: Incoming Packet to Local Process¶
- Prerouting: A packet arrives at the network device.
- Routing Decision: The kernel decides it is for a local process.
- Input: The packet moves through the Input hook.
- Local Process: The packet is handed over to the local process.
Example 2: Packet to be Forwarded¶
- Prerouting: A packet arrives at the network device.
- Routing Decision: The kernel decides it needs to be forwarded.
- Forward: The packet moves through the Forward hook.
- Postrouting: The packet traverses the Postrouting hook.
- Outgoing Device: The packet is sent out to another network device.
Example 3: Outgoing Packet from Local Process¶
- Output: A packet is generated by a local process.
- Postrouting: The packet traverses the Postrouting hook.
- Outgoing Device: The packet is sent out to the network.
In the context of Netfilter, a "local process" refers to any application or service running on the same system that is generating or receiving network traffic. Examples of local processes include:
- Web Server (e.g., Apache, Nginx): Serving web pages to clients.
- Database Server (e.g., MySQL, PostgreSQL): Handling database queries.
- SSH Server: Providing remote command-line access to the system.
- Email Server (e.g., Postfix, Sendmail): Handling incoming and outgoing emails.
- Client Applications: Any application that makes outgoing network requests, like a web browser or email client.
Example Flow: Incoming Packet to Web Server¶
- Prerouting: Packet received on network device.
- Routing Decision: Determined to be for the local web server.
- Input: Packet passed through Input hook.
- Web Server Process: Packet handed to the web server process to handle the request.
Example Flow: Outgoing Packet from Web Browser¶
- Web Browser: Generates a network request.
- Output: Packet passed through Output hook.
- Postrouting: Packet processed through Postrouting hook.
- Network Device: Packet sent out to the network.
The firewalld
Service¶
- Dynamic Firewall Manager: Recommended front end to the nftables framework.
- Zones: Classifies network traffic into zones based on criteria like source IP address or network interface.
- NetworkManager Integration: Automatically sets the firewall zone for a connection, useful for devices frequently changing networks.
firewalld
vsnetfilter
¶
netfilter
is the core kernel framework for packet processing, whilefirewalld
provides a high-level, dynamic, and user-friendly interface to manage these rules.
netfilter
:¶
- Framework: Provides the underlying packet filtering, network address translation, and port translation capabilities within the Linux kernel.
- Hooks: Offers hooks where various kernel modules can register callback functions to process packets.
- Low-Level Control: Allows direct manipulation of packet processing rules.
firewalld
:¶
- Frontend: Acts as a user-friendly front end to the netfilter framework.
- Dynamic Management: Enables dynamic management of firewall rules without restarting.
- Zones: Organizes network traffic into zones with predefined rules, making configuration easier.
- User Interface: Provides both command-line (
firewall-cmd
) and graphical interfaces.
Refer to here for more on firewalld
Predefined Zones¶
- Default Zones Configuration:
Zone Name | Default Configuration |
---|---|
trusted | Allow all incoming traffic. |
home | Reject incoming traffic unless matching predefined services (e.g., ssh, mdns). |
internal | Same as home zone initially. |
work | Reject incoming traffic unless matching ssh, ipp-client, or dhcpv6-client. |
public | Reject incoming traffic unless matching ssh or dhcpv6-client. |
external | Same as public, with outgoing IPv4 traffic masqueraded. |
dmz | Reject incoming traffic unless matching ssh. |
block | Reject all incoming traffic unless related to outgoing traffic. |
drop | Drop all incoming traffic without responding with ICMP errors. |
Predefined Services¶
- Famous network ports
Service Name | Configuration |
---|---|
ssh | Local SSH server. Traffic to 22/tcp. |
dhcpv6-client | Local DHCPv6 client. Traffic to 546/udp on fe80::/64 IPv6 network. |
ipp-client | Local IPP printing. Traffic to 631/udp. |
samba-client | Windows file and print sharing. Traffic to 137/udp and 138/udp. |
mdns | Multicast DNS local-link name resolution. Traffic to 5353/udp. |
cockpit | Web-based interface for system management. Traffic to 9090 port. |
Configuring firewalld¶
Web Console¶
- Log In: Escalate privileges to administrative mode.
- Navigate: Go to
Networking > Firewall
. - Manage Zones and Services: Add services to zones using the graphical interface.
Command Line (firewall-cmd)¶
- Common Commands:
Command | Explanation |
---|---|
--get-default-zone | Query the current default zone. |
--set-default-zone=ZONE | Set the default zone. |
--get-zones | List all available zones. |
--get-active-zones | List currently active zones. |
--add-source=CIDR [--zone=ZONE] | Route traffic from CIDR to the specified zone. |
--remove-source=CIDR [--zone=ZONE] | Remove the rule routing traffic from CIDR. |
--add-interface=INTERFACE [--zone=ZONE] | Route traffic from INTERFACE to the specified zone. |
--change-interface=INTERFACE [--zone=ZONE] | Change the zone for INTERFACE. |
--list-all [--zone=ZONE] | List all configured interfaces, sources, services, and ports for ZONE. |
--list-all-zones | Retrieve information for all zones. |
--add-service=SERVICE [--zone=ZONE] | Allow traffic to SERVICE. |
--add-port=PORT/PROTOCOL [--zone=ZONE] | Allow traffic to PORT/PROTOCOL. |
--remove-service=SERVICE [--zone=ZONE] | Remove SERVICE from allowed list. |
--remove-port=PORT/PROTOCOL [--zone=ZONE] | Remove PORT/PROTOCOL from allowed list. |
--reload | Apply persistent configuration. |
Examples:¶
- Set default zone to dmz:
- Add incoming traffic from 172.25.25.11 to public zone:
Questions and Answers¶
- What is the primary objective of managing server firewalls?
-
To accept or reject network connections to system services using
firewalld
rules. -
What framework does the Linux kernel provide for network traffic operations?
-
The netfilter framework.
-
What replaced the iptables framework in Red Hat Enterprise Linux 9?
-
The nftables framework.
-
What are the advantages of nftables over iptables?
-
Improved usability and more efficient rule sets.
-
How does nftables handle both IPv4 and IPv6 traffic?
-
It uses a single rule set to manage both IPv4 and IPv6 traffic simultaneously.
-
What service is recommended as the front end to the nftables framework?
-
The firewalld service.
-
How does firewalld simplify firewall management?
-
By classifying network traffic into zones.
-
What factors determine a network packet's assigned zone in firewalld?
-
The source IP address or the incoming network interface.
-
How does firewalld handle packets from devices that frequently change networks?
-
NetworkManager can automatically set the firewall zone for a connection.
-
What happens to incoming packets if the source address is not assigned to a specific zone?
- firewalld associates the packet with the zone for the incoming network interface.
-
What is the default zone in firewalld?
- The public zone.
-
What zone is the lo loopback interface mapped to in firewalld?
- The trusted zone.
-
What kind of traffic is allowed in the trusted zone by default?
- All incoming traffic.
-
What command lists all available zones in firewalld?
firewall-cmd --get-zones
-
How do you set the default zone in firewalld?
firewall-cmd --set-default-zone=ZONE
-
How do you list all configured interfaces, sources, services, and ports for a specific zone?
firewall-cmd --list-all --zone=ZONE
-
What command is used to allow traffic to a specific service in a specific zone?
firewall-cmd --add-service=SERVICE --zone=ZONE
-
How do you permanently allow traffic to a specific port in firewalld?
firewall-cmd --permanent --add-port=PORT/PROTOCOL
-
What must be done after adding a permanent rule to make it active?
- Run
firewall-cmd --reload
.
- Run
-
How do you list the current default zone?
firewall-cmd --get-default-zone
-
What is a key benefit of using predefined services in firewalld?
- Simplifies setting firewall rules without needing to research port numbers.
-
How can you manually specify required ports and protocols in firewalld?
- Using the
firewall-cmd
command-line tool or the web console graphical interface.
- Using the
-
How can you review the predefined services in firewalld?
firewall-cmd --get-services
-
What is the command to remove a service from the allowed list in a specific zone?
firewall-cmd --remove-service=SERVICE --zone=ZONE
-
What command is used to add all incoming traffic from a specific IP address to a specific zone?
firewall-cmd --permanent --zone=ZONE --add-source=CIDR