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-zonesList active zones (zones with assigned interfaces or sources)
firewall-cmd --get-active-zonesGet default zone
firewall-cmd --get-default-zoneList all zones with their configurations
firewall-cmd --list-all-zones`#### Setting Default Zone
`bash
Set default zone temporarily
firewall-cmd --set-default-zone=publicThe default zone change is automatically permanent
`#### Zone Information
`bash
Get information about a specific zone
firewall-cmd --zone=public --list-allList services in a zone
firewall-cmd --zone=public --list-servicesList ports in a zone
firewall-cmd --zone=public --list-portsList interfaces assigned to a zone
firewall-cmd --zone=public --list-interfacesList 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-webReload firewall to apply changes
firewall-cmd --reloadVerify zone creation
firewall-cmd --get-zones`#### Deleting a Zone
`bash
Delete a custom zone (must be permanent)
firewall-cmd --permanent --delete-zone=custom-webReload firewall
firewall-cmd --reload`Assigning Interfaces and Sources to Zones
#### Interface Assignment
`bash
Assign interface to zone temporarily
firewall-cmd --zone=public --add-interface=eth0Assign interface to zone permanently
firewall-cmd --permanent --zone=public --add-interface=eth0Remove interface from zone
firewall-cmd --zone=public --remove-interface=eth0Change 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.100Add source network to zone permanently
firewall-cmd --permanent --zone=internal --add-source=192.168.1.0/24Remove source from zone
firewall-cmd --zone=trusted --remove-source=192.168.1.100Change 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=httpAdd service permanently
firewall-cmd --permanent --zone=public --add-service=httpsRemove service from zone
firewall-cmd --zone=public --remove-service=httpAdd 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/tcpAdd port permanently
firewall-cmd --permanent --zone=public --add-port=3306/tcpAdd port range
firewall-cmd --zone=public --add-port=8000-8100/tcpAdd UDP port
firewall-cmd --zone=public --add-port=53/udpRemove 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=DROPSet zone target to ACCEPT
firewall-cmd --permanent --zone=trusted --set-target=ACCEPTSet zone target to REJECT
firewall-cmd --permanent --zone=block --set-target=REJECTReset 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-masqueradeEnable masquerading permanently
firewall-cmd --permanent --zone=external --add-masqueradeDisable masquerading
firewall-cmd --zone=external --remove-masqueradeCheck 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=8080Forward to different host
firewall-cmd --zone=external --add-forward-port=port=80:proto=tcp:toaddr=192.168.1.100:toport=80Remove 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=webserverSet 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=httpsAdd SSH for administration
firewall-cmd --permanent --zone=webserver --add-service=sshAdd custom application port
firewall-cmd --permanent --zone=webserver --add-port=8080/tcpAssign interface to zone
firewall-cmd --permanent --zone=webserver --add-interface=eth0Reload configuration
firewall-cmd --reloadVerify 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=databaseAdd SSH for administration
firewall-cmd --permanent --zone=database --add-service=sshAdd MySQL port
firewall-cmd --permanent --zone=database --add-port=3306/tcpAllow only application servers subnet
firewall-cmd --permanent --zone=database --add-source=192.168.100.0/24Set restrictive target
firewall-cmd --permanent --zone=database --set-target=DROPReload 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=httpsAdd 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=imapAdd DNS
firewall-cmd --permanent --zone=dmz --add-service=dnsAdd 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=eth1Reload 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=eth0Check which zone a source belongs to
firewall-cmd --get-zone-of-source=192.168.1.100List 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 servicenameCheck if port is open in zone
firewall-cmd --zone=public --query-port=80/tcpCheck 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-numberUse nmap to scan ports
nmap -p 80,443 target-ipCheck firewall logs
journalctl -f -u firewalldEnable logging for denied packets
firewall-cmd --set-log-denied=all`Debugging Commands
`bash
Get detailed zone information
firewall-cmd --info-zone=publicCheck firewall state
firewall-cmd --stateReload firewall (keeps runtime changes)
firewall-cmd --reloadComplete restart (loses runtime changes)
firewall-cmd --complete-reloadCheck 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
`
Monitoring and Logging
Enable Logging
`bash
Enable logging for denied packets
firewall-cmd --set-log-denied=allLog only unicast packets
firewall-cmd --set-log-denied=unicastDisable logging
firewall-cmd --set-log-denied=off`View Logs
`bash
View firewalld logs
journalctl -u firewalldFollow logs in real-time
journalctl -f -u firewalldView 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.