Home > Learn > Server > Server Security > How to Set Up a Firewall with firewalld on CentOS/AlmaLinux
In today’s digital landscape, securing your server is paramount, and a robust firewall is one of the first lines of defense against unauthorized access and potential threats. For users of CentOS and AlmaLinux, the firewalld service offers a dynamic and flexible solution to manage firewall rules. With its ability to support network zones and a wide range of configuration options, firewalld empowers administrators to tailor network traffic management to their specific needs. This article will guide you through the process of setting up and configuring firewalld on CentOS and AlmaLinux, ensuring that your server remains secure while maintaining optimal performance.
This article is part of Rackset’s Server Security guides — browse the rest of the category for more in-depth tutorials on this topic.
Throughout this guide, you’ll learn how to install firewalld, understand its core concepts, and configure essential settings to safeguard your server environment. We’ll cover how to manage zones, add and remove services, and implement custom rules to fine-tune your firewall’s behavior. Whether you’re a seasoned system administrator or new to firewalld, this comprehensive walkthrough will equip you with the knowledge and tools needed to effectively manage your server’s security on CentOS and AlmaLinux platforms.
Understanding firewalld and Zones
Firewalld is a dynamic firewall management tool that provides a user-friendly interface to manage firewall settings on CentOS and AlmaLinux systems. It uses the concept of zones to define the level of trust for network connections, allowing administrators to apply different rules and policies based on the zone assigned to a network interface.
What are Zones?
Zones in firewalld are predefined sets of rules that determine the security level of network connections. Each zone has a specific trust level, and you can assign network interfaces or sources to these zones. This allows for flexible and granular control over the network traffic.
- Drop: All incoming connections are dropped without any notification.
- Block: Similar to Drop, but sends an ICMP host-prohibited message.
- Public: For use in untrusted public areas. Only selected incoming connections are accepted.
- External: Used for external networks in routed environments. Similar to Public, but with masquerading enabled.
- Internal: For use on internal networks. Allows more services than Public.
- Home: For use in home areas. Allows more services than Internal.
- Work: For use in work areas. Similar to Home, but tailored for work environments.
- Trusted: All network connections are accepted.
Assigning Zones to Interfaces
Network interfaces can be assigned to different zones to control the traffic flow based on the security requirements. By default, interfaces are associated with the Public zone, but this can be changed according to your needs.
- Identify the network interface you wish to configure.
- Use the
firewall-cmdcommand to assign the desired zone to the interface. - Reload the firewalld configuration to apply the changes.
Understanding and configuring zones effectively allows you to enhance the security of your CentOS or AlmaLinux system by controlling the network traffic based on trust levels.
Installing firewalld on CentOS/AlmaLinux

Firewalld is a dynamic firewall management tool with support for network zones, offering a more flexible solution than traditional iptables. Installing firewalld on CentOS and AlmaLinux is straightforward. This section will guide you through the installation process.
Step 1: Update Your System
Before installing any new software, it’s a good practice to update your system to ensure all existing packages are up to date. Run the following command:
sudo dnf update -y
This command will update all installed packages to their latest versions.
Step 2: Install firewalld
Once your system is up to date, you can install firewalld using the DNF package manager. Execute the following command:
sudo dnf install firewalld -y
This command will download and install firewalld along with any necessary dependencies.
Step 3: Start and Enable firewalld
After installation, you need to start the firewalld service and enable it to start automatically on boot. Use the following commands:
sudo systemctl start firewalld
sudo systemctl enable firewalld
- Start firewalld: This command initiates the firewalld service.
- Enable firewalld: This command ensures that firewalld starts automatically when the system boots.
Step 4: Verify firewalld Status
Finally, verify that firewalld is running by checking its status:
Read more: How to Set Up a UFW Firewall on a Linux VPS
If you need this service, Rackset’s team provides professional Linux VPS with full support so you can rely on the quality and get set up quickly.
sudo systemctl status firewalld
If firewalld is active and running, you should see an output indicating that the service is “active (running).” This confirms that firewalld is installed and functioning correctly on your CentOS or AlmaLinux system.
Configuring firewalld Zones
Firewalld organizes its rules into zones, each defining a set of rules for network traffic. By default, firewalld comes with several predefined zones, including public, home, dmz, and block. Understanding and configuring these zones is crucial for effective firewall management on CentOS or AlmaLinux.
Viewing Available Zones
To see all available zones, use the following command:
firewall-cmd --get-zones
This command will list all zones, allowing you to choose the most appropriate one for your network interface.
Checking Active Zones
To determine which zone is currently active and applied to your network interfaces, run:
firewall-cmd --get-active-zones
This output will show the active zones and the interfaces associated with them.
Assigning a Zone to an Interface
To assign a specific zone to a network interface, use the following command:
sudo firewall-cmd --zone=ZONE_NAME --change-interface=INTERFACE_NAME --permanent
Replace ZONE_NAME with the desired zone and INTERFACE_NAME with your network interface. Use the --permanent flag to make the changes persistent across reboots.
Reloading firewalld
After making changes to your firewall configuration, it’s important to reload firewalld to apply them:
sudo firewall-cmd --reload
This command ensures that all your modifications are active and effective.
Customizing Zone Rules
You can customize the rules within a zone by adding or removing services and ports. For example, to allow HTTP traffic in the public zone:
sudo firewall-cmd --zone=public --add-service=http --permanent
Always remember to reload firewalld after making changes to ensure they take effect.
Read more: How to Change Root Password on CentOS
For projects that need guaranteed resources and full root control, a VPS is the right step up from shared hosting. With VPS hosting you get dedicated CPU/RAM, NVMe storage, a dedicated IP and high uptime, deployed in minutes.
Managing Services with firewalld

Firewalld simplifies the management of firewall rules through the use of predefined services. Each service defines a set of ports and protocols that can be easily enabled or disabled. This section will guide you through listing, adding, and removing services using firewalld on CentOS and AlmaLinux.
Listing Available Services
To view all services that firewalld can manage, use the following command:
firewall-cmd --get-services
This command provides a list of all available services that can be managed by firewalld, allowing you to quickly identify which services are currently supported.
Adding a Service
To allow a specific service through the firewall, you need to add it to the active zone. Follow these steps to add a service:
- Identify the active zone using the command:
- Add the desired service to the active zone with the following command:
- To make this change permanent, use:
firewall-cmd --get-active-zones
firewall-cmd --zone=zone_name --add-service=service_name
firewall-cmd --zone=zone_name --add-service=service_name --permanent
Removing a Service
If you need to remove a service, follow these steps:
- Ensure you know the active zone using:
- Remove the service from the active zone with the command:
- To ensure this change is permanent, execute:
firewall-cmd --get-active-zones
firewall-cmd --zone=zone_name --remove-service=service_name
firewall-cmd --zone=zone_name --remove-service=service_name --permanent
Managing services with firewalld allows you to control which network services can pass through your firewall, providing a flexible and secure environment for your CentOS or AlmaLinux system.
Opening and Closing Ports with firewalld
Managing ports is a critical aspect of server security. With firewalld, you can easily open and close ports to control access to services on your CentOS or AlmaLinux system. Below, we will guide you through the steps to manage ports using firewalld.
Opening a Port
To allow traffic through a specific port, use the firewalld command-line interface. Follow these steps to open a port:
- Identify the port number and protocol (TCP or UDP) that the service uses. For example, to open port 80 for HTTP traffic:
- Execute the following command to open the port:
sudo firewall-cmd --zone=public --add-port=80/tcp --permanent
--zone=publicspecifies the zone for which you are opening the port.--add-port=80/tcpindicates the port and protocol.--permanentensures the change persists across reboots.
After adding the port, reload the firewall to apply the changes:
sudo firewall-cmd --reload
Closing a Port
If you need to close a port that is no longer required, you can do so easily. Follow these steps:
- Determine the port number and protocol you wish to close. For example, to close port 80:
- Run the following command to remove the port:
sudo firewall-cmd --zone=public --remove-port=80/tcp --permanent
Again, reload the firewall to ensure the changes take effect:
Read more: How to Set Up Cron Jobs on a Linux Server
sudo firewall-cmd --reload
By carefully managing open and closed ports with firewalld, you can enhance the security of your CentOS or AlmaLinux server, allowing only necessary traffic to reach your services.
Testing and Verifying firewalld Configuration
Once you have configured firewalld on your CentOS or AlmaLinux server, it’s crucial to test and verify that the firewall rules are working as expected. This ensures your server is protected and only the intended network traffic is allowed.
For heavy workloads and maximum performance, a dedicated bare-metal server gives you an entire physical machine. Rackset’s dedicated server deliver full hardware, root access and configurable port speeds for the most demanding sites and apps.
Check firewalld Status
Firstly, confirm that firewalld is active and running. Use the following command:
sudo systemctl status firewalld
If firewalld is running, you should see an ‘active (running)’ status. If it’s not running, start it with:
sudo systemctl start firewalld
List Active Zones
To see which zones are active and their configurations, use:
sudo firewall-cmd --get-active-zones
This command will list active zones and the interfaces associated with them. Understanding active zones helps in verifying the rules applied to each network interface.
Test Specific Rules
Verify that your specific firewall rules are functioning by checking the active zone configurations:
sudo firewall-cmd --list-all
Review the output to ensure all intended services and ports are correctly listed. You can test connectivity to specific ports using tools like telnet or nc (netcat) from a remote machine:
telnet your-server-ip port-number
Log and Monitor Traffic
Enable logging for dropped packets to monitor unauthorized access attempts:
sudo firewall-cmd --set-log-denied=all
Check the logs at /var/log/firewalld to identify any denied connections, which can indicate misconfigurations or attempted breaches.
Utilizing Rackset’s Hosting for Enhanced Security
With Rackset’s hosting solutions, you benefit from a robust infrastructure that complements your firewalld setup. Our dedicated servers provide enhanced security features and reliable performance, ensuring that your firewall configurations are both effective and efficient.
Need expert help from Rackset?
Want this done fast, professionally and hassle-free? Rackset’s experienced team and full support are here to help you get the best result.
Conclusion
Setting up a firewall using firewalld on CentOS or AlmaLinux is a vital step in securing your server environment. By understanding how to manage zones and services, you can efficiently control traffic and protect your system from unauthorized access. The flexibility and ease of use provided by firewalld make it an excellent choice for both beginners and experienced administrators alike.
Whether you’re managing a small personal server or a large enterprise network, having a robust firewall configuration is crucial. As you continue to explore advanced configurations and security measures, remember that choosing a reliable hosting provider can make a significant difference. Consider Rackset for your hosting needs, where we prioritize security and performance to ensure your server runs smoothly and safely.
Frequently Asked Questions
What is firewalld in CentOS?
Firewalld is a dynamic firewall management tool in CentOS that provides a way to manage firewall rules without needing to restart the firewall. It uses zones and services to control the traffic flow and offers a more flexible and easier approach than traditional iptables.
How do I check the status of firewalld on CentOS?
You can check the status of firewalld by running the command ‘systemctl status firewalld’. This will show whether the service is active and running, along with other relevant details.
How can I enable firewalld on CentOS?
To enable firewalld on CentOS, use the command ‘systemctl enable firewalld’. This ensures that firewalld starts automatically at boot time.
What are zones in firewalld?
Zones in firewalld represent different levels of trust for network connections. Each zone has a set of rules that define what traffic is allowed or denied, allowing you to apply different security levels to different network interfaces.
Can I use firewalld with other firewall tools on CentOS?
While it’s technically possible to use firewalld alongside other firewall tools like iptables, it is not recommended. Using multiple firewall management tools can lead to conflicts and unexpected behavior, so it’s best to use one tool consistently.
Leave a Reply