Complete Guide to Configuring Zones in firewalld

Learn how to configure firewalld zones for dynamic firewall management. Master zone types, trust levels, and network security configurations.

Configuring Zones in firewalld

Introduction

firewalld is a dynamic firewall daemon that provides a network/firewall zone abstraction for managing network connections and interfaces. Unlike traditional iptables configurations, firewalld uses zones to define trust levels for network connections, interfaces, and sources. This approach allows for more flexible and manageable firewall configurations, especially in environments where network conditions change frequently.

Zones in firewalld represent different trust levels for network connections. Each zone has a predefined set of rules that determine what traffic is allowed or blocked. Understanding and properly configuring zones is crucial for effective network security management using firewalld.

Understanding firewalld Zones

What are Zones

Zones in firewalld are predefined rule sets that define the trust level of network connections. Each zone contains rules for services, ports, protocols, and other firewall settings. When a connection, interface, or source is assigned to a zone, it inherits all the rules defined for that zone.

Zone Characteristics

Each zone in firewalld has specific characteristics that define its behavior:

- Trust Level: Determines how restrictive the zone is - Default Services: Predefined services that are allowed - Target: Defines the default action for packets not matching any rule - Interface Binding: Which network interfaces are assigned to the zone - Source Binding: Which IP addresses or networks are assigned to the zone

Default Zones in firewalld

firewalld comes with several predefined zones, each designed for specific use cases:

| Zone Name | Trust Level | Default Target | Description | Typical Use Case | |-----------|-------------|----------------|-------------|------------------| | drop | Minimal | DROP | All incoming connections dropped without reply | High-security environments | | block | Minimal | REJECT | All incoming connections rejected with icmp-host-prohibited | Secure networks with explicit rejection | | public | Low | default | For use in public areas, only selected incoming connections accepted | Default zone for most systems | | external | Low | default | For use on external networks with masquerading enabled | Router/gateway configurations | | dmz | Low | default | For computers in DMZ with limited access to internal network | DMZ servers and services | | work | Medium | default | For use in work areas, trust most computers in network | Office environments | | home | Medium | default | For use in home areas, trust most computers in network | Home networks | | internal | High | default | For use on internal networks when you trust other computers | Internal corporate networks | | trusted | Maximum | ACCEPT | All network connections accepted | Fully trusted environments |

Zone Configuration Commands

Basic Zone Management

#### Listing Zones

`bash

List all available zones

firewall-cmd --get-zones

List active zones (zones with assigned interfaces or sources)

firewall-cmd --get-active-zones

Get default zone

firewall-cmd --get-default-zone

List all zones with their configurations

firewall-cmd --list-all-zones `

#### Setting Default Zone

`bash

Set default zone temporarily

firewall-cmd --set-default-zone=public

The default zone change is automatically permanent

`

#### Zone Information

`bash

Get information about a specific zone

firewall-cmd --zone=public --list-all

List services in a zone

firewall-cmd --zone=public --list-services

List ports in a zone

firewall-cmd --zone=public --list-ports

List interfaces assigned to a zone

firewall-cmd --zone=public --list-interfaces

List sources assigned to a zone

firewall-cmd --zone=public --list-sources `

Creating Custom Zones

#### Creating a New Zone

`bash

Create a new custom zone

firewall-cmd --permanent --new-zone=custom-web

Reload firewall to apply changes

firewall-cmd --reload

Verify zone creation

firewall-cmd --get-zones `

#### Deleting a Zone

`bash

Delete a custom zone (must be permanent)

firewall-cmd --permanent --delete-zone=custom-web

Reload firewall

firewall-cmd --reload `

Assigning Interfaces and Sources to Zones

#### Interface Assignment

`bash

Assign interface to zone temporarily

firewall-cmd --zone=public --add-interface=eth0

Assign interface to zone permanently

firewall-cmd --permanent --zone=public --add-interface=eth0

Remove interface from zone

firewall-cmd --zone=public --remove-interface=eth0

Change interface zone

firewall-cmd --zone=dmz --change-interface=eth0 `

#### Source Assignment

`bash

Add source IP/network to zone

firewall-cmd --zone=trusted --add-source=192.168.1.100

Add source network to zone permanently

firewall-cmd --permanent --zone=internal --add-source=192.168.1.0/24

Remove source from zone

firewall-cmd --zone=trusted --remove-source=192.168.1.100

Change source zone

firewall-cmd --zone=work --change-source=192.168.1.100 `

Configuring Zone Rules

Adding Services to Zones

#### Service Management

`bash

Add service to zone temporarily

firewall-cmd --zone=public --add-service=http

Add service permanently

firewall-cmd --permanent --zone=public --add-service=https

Remove service from zone

firewall-cmd --zone=public --remove-service=http

Add multiple services

firewall-cmd --zone=public --add-service=http --add-service=https --add-service=ssh `

Adding Ports to Zones

#### Port Management

`bash

Add single port

firewall-cmd --zone=public --add-port=8080/tcp

Add port permanently

firewall-cmd --permanent --zone=public --add-port=3306/tcp

Add port range

firewall-cmd --zone=public --add-port=8000-8100/tcp

Add UDP port

firewall-cmd --zone=public --add-port=53/udp

Remove port

firewall-cmd --zone=public --remove-port=8080/tcp `

Rich Rules in Zones

Rich rules provide more granular control over firewall rules within zones:

`bash

Allow specific IP to specific port

firewall-cmd --zone=public --add-rich-rule='rule family="ipv4" source address="192.168.1.100" port protocol="tcp" port="22" accept'

Block specific IP

firewall-cmd --zone=public --add-rich-rule='rule family="ipv4" source address="192.168.1.200" reject'

Allow service for specific subnet

firewall-cmd --zone=public --add-rich-rule='rule family="ipv4" source address="192.168.1.0/24" service name="ssh" accept'

Rate limiting

firewall-cmd --zone=public --add-rich-rule='rule service name="ssh" accept limit value="10/m"' `

Advanced Zone Configuration

Zone Targets

Zone targets define the default action for packets that don't match any specific rule:

`bash

Set zone target to DROP

firewall-cmd --permanent --zone=public --set-target=DROP

Set zone target to ACCEPT

firewall-cmd --permanent --zone=trusted --set-target=ACCEPT

Set zone target to REJECT

firewall-cmd --permanent --zone=block --set-target=REJECT

Reset to default target

firewall-cmd --permanent --zone=public --set-target=default `

Zone Target Options

| Target | Description | Behavior | |--------|-------------|----------| | default | Use system default | Typically REJECT with exceptions for allowed services | | ACCEPT | Accept all packets | All traffic allowed unless explicitly blocked | | REJECT | Reject packets | Send rejection message back to sender | | DROP | Drop packets | Silently discard packets |

Masquerading in Zones

Masquerading allows the firewall to act as a router by modifying source addresses:

`bash

Enable masquerading in zone

firewall-cmd --zone=external --add-masquerade

Enable masquerading permanently

firewall-cmd --permanent --zone=external --add-masquerade

Disable masquerading

firewall-cmd --zone=external --remove-masquerade

Check masquerading status

firewall-cmd --zone=external --query-masquerade `

Port Forwarding in Zones

`bash

Forward port 80 to 8080

firewall-cmd --zone=external --add-forward-port=port=80:proto=tcp:toport=8080

Forward to different host

firewall-cmd --zone=external --add-forward-port=port=80:proto=tcp:toaddr=192.168.1.100:toport=80

Remove port forwarding

firewall-cmd --zone=external --remove-forward-port=port=80:proto=tcp:toport=8080 `

Practical Zone Configuration Examples

Example 1: Web Server Configuration

Setting up a zone for a web server that needs to serve HTTP and HTTPS traffic:

`bash

Create custom web server zone

firewall-cmd --permanent --new-zone=webserver

Set description

firewall-cmd --permanent --zone=webserver --set-description="Web Server Zone"

Add HTTP and HTTPS services

firewall-cmd --permanent --zone=webserver --add-service=http firewall-cmd --permanent --zone=webserver --add-service=https

Add SSH for administration

firewall-cmd --permanent --zone=webserver --add-service=ssh

Add custom application port

firewall-cmd --permanent --zone=webserver --add-port=8080/tcp

Assign interface to zone

firewall-cmd --permanent --zone=webserver --add-interface=eth0

Reload configuration

firewall-cmd --reload

Verify configuration

firewall-cmd --zone=webserver --list-all `

Example 2: Database Server Zone

Creating a zone for a database server accessible only from application servers:

`bash

Create database zone

firewall-cmd --permanent --new-zone=database

Add SSH for administration

firewall-cmd --permanent --zone=database --add-service=ssh

Add MySQL port

firewall-cmd --permanent --zone=database --add-port=3306/tcp

Allow only application servers subnet

firewall-cmd --permanent --zone=database --add-source=192.168.100.0/24

Set restrictive target

firewall-cmd --permanent --zone=database --set-target=DROP

Reload and verify

firewall-cmd --reload firewall-cmd --zone=database --list-all `

Example 3: DMZ Configuration

Setting up a DMZ zone for publicly accessible services:

`bash

Configure DMZ zone (already exists)

Add web services

firewall-cmd --permanent --zone=dmz --add-service=http firewall-cmd --permanent --zone=dmz --add-service=https

Add mail services

firewall-cmd --permanent --zone=dmz --add-service=smtp firewall-cmd --permanent --zone=dmz --add-service=pop3 firewall-cmd --permanent --zone=dmz --add-service=imap

Add DNS

firewall-cmd --permanent --zone=dmz --add-service=dns

Add SSH with rate limiting using rich rules

firewall-cmd --permanent --zone=dmz --add-rich-rule='rule service name="ssh" accept limit value="5/m"'

Assign DMZ interface

firewall-cmd --permanent --zone=dmz --add-interface=eth1

Reload configuration

firewall-cmd --reload `

Zone Priority and Rule Processing

Understanding Zone Priority

When multiple zones could match a connection, firewalld uses the following priority order:

1. Source-based zones: Zones assigned to specific IP addresses or networks 2. Interface-based zones: Zones assigned to specific network interfaces 3. Default zone: The system's default zone

Rule Processing Order

Within a zone, rules are processed in this order:

1. Rich rules (processed in the order they were added) 2. Services 3. Ports 4. Protocols 5. Source ports 6. Masquerading 7. Port forwarding 8. ICMP blocks 9. Target action

Troubleshooting Zone Configuration

Common Issues and Solutions

#### Zone Assignment Problems

`bash

Check which zone an interface belongs to

firewall-cmd --get-zone-of-interface=eth0

Check which zone a source belongs to

firewall-cmd --get-zone-of-source=192.168.1.100

List all active zones and their assignments

firewall-cmd --get-active-zones `

#### Service and Port Issues

`bash

Verify service is available

firewall-cmd --get-services | grep servicename

Check if port is open in zone

firewall-cmd --zone=public --query-port=80/tcp

Check if service is enabled in zone

firewall-cmd --zone=public --query-service=http `

#### Testing Zone Configuration

`bash

Test from another system

telnet target-ip port-number

Use nmap to scan ports

nmap -p 80,443 target-ip

Check firewall logs

journalctl -f -u firewalld

Enable logging for denied packets

firewall-cmd --set-log-denied=all `

Debugging Commands

`bash

Get detailed zone information

firewall-cmd --info-zone=public

Check firewall state

firewall-cmd --state

Reload firewall (keeps runtime changes)

firewall-cmd --reload

Complete restart (loses runtime changes)

firewall-cmd --complete-reload

Check for configuration errors

firewall-cmd --check-config `

Best Practices for Zone Configuration

Security Considerations

1. Principle of Least Privilege: Only open ports and services that are absolutely necessary 2. Use Specific Zones: Create custom zones for specific purposes rather than modifying default zones 3. Regular Auditing: Regularly review zone configurations and remove unused rules 4. Source-based Rules: Use source-based zone assignments for better security control

Configuration Management

1. Always Use Permanent Rules: Use the --permanent flag for production configurations 2. Document Changes: Keep track of why specific rules were added 3. Test Before Deployment: Test zone configurations in a non-production environment 4. Backup Configurations: Regularly backup firewalld configurations

Performance Optimization

1. Minimize Rich Rules: Use services and ports instead of rich rules when possible 2. Order Rich Rules: Place most frequently matched rules first 3. Use Appropriate Targets: Set appropriate zone targets to minimize rule processing

Configuration File Locations

firewalld zone configurations are stored in XML files:

System Zones

` /usr/lib/firewalld/zones/ `

Custom Zones

` /etc/firewalld/zones/ `

Zone File Structure

Example zone file (/etc/firewalld/zones/custom-web.xml):

`xml Custom Web Custom zone for web servers `

Monitoring and Logging

Enable Logging

`bash

Enable logging for denied packets

firewall-cmd --set-log-denied=all

Log only unicast packets

firewall-cmd --set-log-denied=unicast

Disable logging

firewall-cmd --set-log-denied=off `

View Logs

`bash

View firewalld logs

journalctl -u firewalld

Follow logs in real-time

journalctl -f -u firewalld

View kernel messages for dropped packets

dmesg | grep -i "dropped"

Check system logs for firewall messages

tail -f /var/log/messages | grep kernel `

This comprehensive guide covers all aspects of configuring zones in firewalld, from basic concepts to advanced configurations and troubleshooting. Understanding these concepts and commands will enable you to effectively manage network security using firewalld's zone-based approach.

Tags

  • Network Security
  • firewall
  • firewalld
  • iptables
  • zones

Related Articles

Popular Technical Articles & Tutorials

Explore our comprehensive collection of technical articles, programming tutorials, and IT guides written by industry experts:

Browse all 8+ technical articles | Read our IT blog

Complete Guide to Configuring Zones in firewalld