Complete Guide to Allowing Services in firewalld
Table of Contents
1. [Introduction to firewalld](#introduction-to-firewalld) 2. [Understanding firewalld Services](#understanding-firewalld-services) 3. [Basic Commands and Operations](#basic-commands-and-operations) 4. [Service Management](#service-management) 5. [Zones and Service Configuration](#zones-and-service-configuration) 6. [Practical Examples](#practical-examples) 7. [Advanced Configuration](#advanced-configuration) 8. [Troubleshooting](#troubleshooting) 9. [Best Practices](#best-practices) 10. [Reference Tables](#reference-tables)Introduction to firewalld
firewalld is a dynamic firewall management tool that provides a flexible and feature-rich interface for managing Linux firewall rules. Unlike traditional iptables configurations, firewalld uses zones and services to organize firewall rules, making it easier to manage complex network security policies.
Key Features
- Dynamic Management: Rules can be changed without restarting the firewall service - Zone-based Configuration: Different security levels for different network interfaces - Service Definitions: Predefined service configurations for common applications - Runtime and Permanent Configuration: Separate handling of temporary and persistent rules - D-Bus Interface: Integration with system management tools - Rich Rules: Advanced rule syntax for complex scenarios
Architecture Overview
firewalld operates with two main configuration layers:
1. Runtime Configuration: Active rules that are immediately applied but lost on restart 2. Permanent Configuration: Persistent rules that survive system reboots
Understanding firewalld Services
Services in firewalld are predefined configurations that specify which ports and protocols should be opened for specific applications or services. Each service definition includes:
- Service name and description - TCP and UDP ports - Destination addresses (if applicable) - Helper modules (for complex protocols)
Service Definition Structure
Services are defined in XML files located in:
- System services: /usr/lib/firewalld/services/
- User-defined services: /etc/firewalld/services/
Example service definition structure:
`xml
`
Basic Commands and Operations
Installation and Setup
Most modern Linux distributions include firewalld by default. If not installed:
`bash
Red Hat/CentOS/Fedora
sudo dnf install firewalldDebian/Ubuntu
sudo apt install firewalldEnable and start firewalld
sudo systemctl enable firewalld sudo systemctl start firewalld`Essential Commands
#### Checking firewalld Status
`bash
Check if firewalld is running
sudo firewall-cmd --stateGet detailed status information
sudo systemctl status firewalldCheck firewalld version
sudo firewall-cmd --version`#### Basic Information Commands
`bash
List all available zones
sudo firewall-cmd --get-zonesGet default zone
sudo firewall-cmd --get-default-zoneList active zones
sudo firewall-cmd --get-active-zonesList all available services
sudo firewall-cmd --get-services`Service Management
Listing Services
#### Current Zone Services
`bash
List services in default zone
sudo firewall-cmd --list-servicesList services in specific zone
sudo firewall-cmd --zone=public --list-servicesList all configuration for default zone
sudo firewall-cmd --list-allList all configuration for specific zone
sudo firewall-cmd --zone=public --list-all`Adding Services
#### Runtime Configuration (Temporary)
`bash
Add service to default zone (temporary)
sudo firewall-cmd --add-service=httpAdd service to specific zone (temporary)
sudo firewall-cmd --zone=public --add-service=httpsAdd multiple services at once
sudo firewall-cmd --add-service=http --add-service=https --add-service=ssh`#### Permanent Configuration
`bash
Add service permanently to default zone
sudo firewall-cmd --permanent --add-service=httpAdd service permanently to specific zone
sudo firewall-cmd --permanent --zone=public --add-service=httpsAdd multiple services permanently
sudo firewall-cmd --permanent --add-service=http --add-service=httpsReload to apply permanent changes
sudo firewall-cmd --reload`#### Combined Runtime and Permanent
`bash
Add service both runtime and permanent
sudo firewall-cmd --add-service=http sudo firewall-cmd --permanent --add-service=httpAlternative single command approach
sudo firewall-cmd --permanent --add-service=http sudo firewall-cmd --reload`Removing Services
#### Runtime Removal
`bash
Remove service from default zone (temporary)
sudo firewall-cmd --remove-service=httpRemove service from specific zone (temporary)
sudo firewall-cmd --zone=public --remove-service=https`#### Permanent Removal
`bash
Remove service permanently
sudo firewall-cmd --permanent --remove-service=httpRemove from specific zone permanently
sudo firewall-cmd --permanent --zone=public --remove-service=httpsReload to apply changes
sudo firewall-cmd --reload`Verification Commands
`bash
Check if service is enabled in zone
sudo firewall-cmd --zone=public --query-service=httpList enabled services with details
sudo firewall-cmd --zone=public --list-services --verboseShow complete zone configuration
sudo firewall-cmd --zone=public --list-all`Zones and Service Configuration
Understanding Zones
Zones define different trust levels for network connections. Each zone has a predefined set of rules and can be assigned to network interfaces.
#### Default Zones
| Zone Name | Trust Level | Description | Default Services | |-----------|-------------|-------------|------------------| | drop | Lowest | All incoming connections dropped | None | | block | Very Low | Incoming connections rejected | None | | public | Low | Public networks, untrusted | ssh, dhcpv6-client | | external | Low-Medium | External networks with masquerading | ssh | | dmz | Medium | DMZ networks, limited access | ssh | | work | Medium-High | Work networks, trusted | ssh, dhcpv6-client | | home | High | Home networks, mostly trusted | ssh, mdns, dhcpv6-client | | internal | High | Internal networks, trusted | ssh, mdns, dhcpv6-client | | trusted | Highest | All connections accepted | All |
Zone Management
#### Setting Default Zone
`bash
Set default zone
sudo firewall-cmd --set-default-zone=publicVerify default zone
sudo firewall-cmd --get-default-zone`#### Interface Assignment
`bash
Assign interface to zone (runtime)
sudo firewall-cmd --zone=public --change-interface=eth0Assign interface to zone (permanent)
sudo firewall-cmd --permanent --zone=public --change-interface=eth0List interfaces in zone
sudo firewall-cmd --zone=public --list-interfaces`Service Configuration in Different Zones
#### Web Server Configuration Example
`bash
Configure web server in public zone
sudo firewall-cmd --zone=public --permanent --add-service=http sudo firewall-cmd --zone=public --permanent --add-service=httpsConfigure SSH access in internal zone only
sudo firewall-cmd --zone=internal --permanent --add-service=ssh sudo firewall-cmd --zone=public --permanent --remove-service=sshApply changes
sudo firewall-cmd --reload`#### Database Server Configuration Example
`bash
Allow database access only from internal zone
sudo firewall-cmd --zone=internal --permanent --add-service=mysql sudo firewall-cmd --zone=internal --permanent --add-service=postgresqlVerify configuration
sudo firewall-cmd --zone=internal --list-services`Practical Examples
Example 1: Web Server Setup
Setting up a web server with HTTP and HTTPS access:
`bash
Step 1: Check current configuration
sudo firewall-cmd --list-allStep 2: Add HTTP service (temporary for testing)
sudo firewall-cmd --add-service=http echo "HTTP service added temporarily"Step 3: Test web server accessibility
(Test your web server here)
Step 4: Make changes permanent if working correctly
sudo firewall-cmd --permanent --add-service=http sudo firewall-cmd --permanent --add-service=httpsStep 5: Reload to apply permanent configuration
sudo firewall-cmd --reloadStep 6: Verify final configuration
sudo firewall-cmd --list-services`Example 2: Mail Server Configuration
Setting up a complete mail server:
`bash
Add mail services permanently
sudo firewall-cmd --permanent --add-service=smtp sudo firewall-cmd --permanent --add-service=smtps sudo firewall-cmd --permanent --add-service=submission sudo firewall-cmd --permanent --add-service=imap sudo firewall-cmd --permanent --add-service=imaps sudo firewall-cmd --permanent --add-service=pop3 sudo firewall-cmd --permanent --add-service=pop3sReload configuration
sudo firewall-cmd --reloadVerify mail services
sudo firewall-cmd --list-services | grep -E "(smtp|imap|pop3)"`Example 3: Database Server with Restricted Access
Setting up database access only from internal networks:
`bash
Remove database service from public zone if present
sudo firewall-cmd --permanent --zone=public --remove-service=mysqlAdd database service to internal zone only
sudo firewall-cmd --permanent --zone=internal --add-service=mysqlAdd specific source networks to internal zone
sudo firewall-cmd --permanent --zone=internal --add-source=192.168.1.0/24 sudo firewall-cmd --permanent --zone=internal --add-source=10.0.0.0/8Reload configuration
sudo firewall-cmd --reloadVerify configuration
sudo firewall-cmd --zone=internal --list-all`Example 4: FTP Server Configuration
Setting up FTP server with both standard and secure FTP:
`bash
Add FTP services
sudo firewall-cmd --permanent --add-service=ftp sudo firewall-cmd --permanent --add-service=tftpFor passive FTP, you might need additional ports
This is typically handled by the ftp service definition
but can be customized if needed
Reload and verify
sudo firewall-cmd --reload sudo firewall-cmd --list-services`Advanced Configuration
Creating Custom Services
#### Custom Service Definition
Create a custom service file at /etc/firewalld/services/myapp.xml:
`xml
`
#### Using Custom Services
`bash
Reload firewalld to recognize new service
sudo firewall-cmd --reloadAdd custom service
sudo firewall-cmd --permanent --add-service=myappVerify custom service is available
sudo firewall-cmd --get-services | grep myapp`Rich Rules for Advanced Service Control
Rich rules provide more granular control over service access:
`bash
Allow HTTP only from specific subnet
sudo firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="192.168.1.0/24" service name="http" accept'Allow SSH with rate limiting
sudo firewall-cmd --permanent --add-rich-rule='rule service name="ssh" accept limit value="3/m"'Allow HTTPS with logging
sudo firewall-cmd --permanent --add-rich-rule='rule service name="https" log prefix="HTTPS Access" level="info" accept'Reload to apply rich rules
sudo firewall-cmd --reload`Port Forwarding with Services
`bash
Forward external port to internal service
sudo firewall-cmd --permanent --add-forward-port=port=80:proto=tcp:toport=8080:toaddr=192.168.1.100Enable masquerading for forwarding to work
sudo firewall-cmd --permanent --add-masqueradeReload configuration
sudo firewall-cmd --reload`Time-based Service Access
Using rich rules for time-based access:
`bash
Allow web access only during business hours (requires additional scripting)
This is a conceptual example - actual implementation requires cron jobs or systemd timers
Create scripts to enable/disable services at specific times
echo '#!/bin/bash firewall-cmd --add-service=http firewall-cmd --add-service=https' > /usr/local/bin/enable-web.shecho '#!/bin/bash firewall-cmd --remove-service=http firewall-cmd --remove-service=https' > /usr/local/bin/disable-web.sh
chmod +x /usr/local/bin/enable-web.sh /usr/local/bin/disable-web.sh
`
Troubleshooting
Common Issues and Solutions
#### Service Not Working After Adding
Problem: Service added to firewalld but still not accessible
Diagnosis Steps:
`bash
Check if service is actually enabled
sudo firewall-cmd --list-servicesCheck if service is in correct zone
sudo firewall-cmd --get-active-zones sudo firewall-cmd --zone=public --list-servicesVerify service definition
sudo firewall-cmd --info-service=httpCheck if permanent rules are loaded
sudo firewall-cmd --permanent --list-services`Solutions:
`bash
Reload firewalld
sudo firewall-cmd --reloadMake sure service is added to correct zone
sudo firewall-cmd --zone=public --add-service=httpRestart firewalld if needed
sudo systemctl restart firewalld`#### Permanent Changes Not Applied
Problem: Permanent changes not taking effect
Diagnosis:
`bash
Compare runtime vs permanent configuration
sudo firewall-cmd --list-services sudo firewall-cmd --permanent --list-services`Solution:
`bash
Reload to apply permanent changes
sudo firewall-cmd --reloadOr restart firewalld service
sudo systemctl restart firewalld`#### Service Conflicts
Problem: Custom service conflicts with existing services
Diagnosis:
`bash
Check all services using specific port
sudo firewall-cmd --get-services | xargs -I {} firewall-cmd --info-service={} | grep -B5 "port.*80"List all active ports
sudo firewall-cmd --list-ports`Debugging Commands
#### Verbose Output
`bash
Enable verbose logging
sudo firewall-cmd --set-log-denied=allCheck logs
sudo journalctl -u firewalld -fView detailed zone information
sudo firewall-cmd --list-all-zones`#### Testing Connectivity
`bash
Test from client machine
telnet server_ip service_portCheck if port is listening on server
sudo ss -tlnp | grep :80 sudo netstat -tlnp | grep :80Use nmap to scan ports
nmap -p 80,443 server_ip`Best Practices
Security Best Practices
#### Principle of Least Privilege
`bash
Only enable services that are actually needed
Review enabled services regularly
sudo firewall-cmd --list-servicesRemove unused services
sudo firewall-cmd --permanent --remove-service=unused_service`#### Zone-based Security
`bash
Use appropriate zones for different network segments
Public zone for internet-facing services
sudo firewall-cmd --zone=public --add-service=httpInternal zone for administrative access
sudo firewall-cmd --zone=internal --add-service=sshAssign interfaces to appropriate zones
sudo firewall-cmd --permanent --zone=internal --change-interface=eth1`#### Regular Auditing
`bash
Create audit script
cat > /usr/local/bin/firewall-audit.sh << 'EOF' #!/bin/bash echo "=== Firewall Audit Report ===" echo "Date: $(date)" echo "Default Zone: $(firewall-cmd --get-default-zone)" echo "Active Zones:" firewall-cmd --get-active-zones echo "Services by Zone:" for zone in $(firewall-cmd --get-zones); do echo "Zone: $zone" firewall-cmd --zone=$zone --list-services echo "---" done EOFchmod +x /usr/local/bin/firewall-audit.sh
`
Operational Best Practices
#### Change Management
`bash
Always test changes in runtime before making permanent
sudo firewall-cmd --add-service=new_serviceTest functionality
sudo firewall-cmd --permanent --add-service=new_service sudo firewall-cmd --reload`#### Backup and Recovery
`bash
Backup firewalld configuration
sudo cp -r /etc/firewalld /etc/firewalld.backup.$(date +%Y%m%d)Export zone configuration
sudo firewall-cmd --permanent --zone=public --list-all > public-zone-backup.txtCreate restoration script
cat > restore-firewall.sh << 'EOF' #!/bin/bashRestore firewalld configuration
sudo systemctl stop firewalld sudo cp -r /etc/firewalld.backup.YYYYMMDD/* /etc/firewalld/ sudo systemctl start firewalld EOF`#### Documentation
Maintain documentation of firewall changes:
`bash
Create change log template
cat > firewall-changes.log << 'EOF' Date: $(date) Change: Added HTTP service to public zone Command: firewall-cmd --permanent --zone=public --add-service=http Reason: New web server deployment Tested: Yes Approved by: Administrator EOF`Reference Tables
Common Services and Ports
| Service Name | Protocol | Port(s) | Description | |--------------|----------|---------|-------------| | http | TCP | 80 | HTTP Web Server | | https | TCP | 443 | HTTPS Web Server | | ssh | TCP | 22 | Secure Shell | | ftp | TCP | 21 | File Transfer Protocol | | smtp | TCP | 25 | Simple Mail Transfer Protocol | | smtps | TCP | 465 | SMTP over SSL | | submission | TCP | 587 | Mail Submission | | imap | TCP | 143 | Internet Message Access Protocol | | imaps | TCP | 993 | IMAP over SSL | | pop3 | TCP | 110 | Post Office Protocol v3 | | pop3s | TCP | 995 | POP3 over SSL | | mysql | TCP | 3306 | MySQL Database | | postgresql | TCP | 5432 | PostgreSQL Database | | dns | TCP/UDP | 53 | Domain Name System | | ntp | UDP | 123 | Network Time Protocol | | snmp | UDP | 161 | Simple Network Management Protocol | | ldap | TCP | 389 | Lightweight Directory Access Protocol | | ldaps | TCP | 636 | LDAP over SSL | | samba | TCP/UDP | 137-139, 445 | Samba File Sharing | | nfs | TCP/UDP | 2049 | Network File System |
firewall-cmd Command Reference
| Command Category | Command | Description |
|------------------|---------|-------------|
| Status | --state | Check firewalld state |
| | --get-default-zone | Get default zone |
| | --get-active-zones | List active zones |
| | --get-zones | List all zones |
| Services | --get-services | List all available services |
| | --list-services | List enabled services |
| | --add-service=SERVICE | Add service |
| | --remove-service=SERVICE | Remove service |
| | --query-service=SERVICE | Check if service is enabled |
| Information | --list-all | Show complete zone configuration |
| | --info-service=SERVICE | Show service details |
| | --list-all-zones | Show all zones configuration |
| Configuration | --reload | Reload permanent configuration |
| | --complete-reload | Complete reload (breaks connections) |
| | --permanent | Make changes permanent |
| Zones | --zone=ZONE | Specify zone for command |
| | --set-default-zone=ZONE | Set default zone |
| | --change-interface=INTERFACE | Assign interface to zone |
Zone Trust Levels and Use Cases
| Zone | Trust Level | Network Type | Typical Use Cases | |------|-------------|--------------|-------------------| | trusted | Maximum | Fully trusted | Internal management networks | | internal | High | Internal networks | Corporate LANs, private networks | | home | High | Home networks | Home LANs, trusted devices | | work | Medium-High | Work networks | Office networks, business LANs | | dmz | Medium | DMZ networks | Public-facing servers, isolated services | | external | Low-Medium | External networks | VPN connections, external access | | public | Low | Public networks | Internet connections, WiFi hotspots | | block | Very Low | Untrusted | Blocked networks, rejected connections | | drop | Minimum | Hostile | Completely untrusted, dropped packets |
Service Definition XML Elements
| Element | Description | Example |
|---------|-------------|---------|
| | Short service name | |
| | Service description | |
| | Port specification | |
| | Protocol specification | |
| | Source port | |
| | Netfilter helper module | |
| | Destination address | |
This comprehensive guide provides detailed information about managing services in firewalld, from basic operations to advanced configurations. The examples and reference tables serve as practical resources for implementing and maintaining firewall security policies effectively.