FirewallD is a complete firewall solution that manages the system’s iptables rules and provides a D-Bus interface for operating on them. Starting with CentOS 7, FirewallD replaces iptables as the default firewall management tool. In this tutorial, we show you how to set up a firewall with FirewallD on your CentOS 7 system and explain you the basic FirewallD concepts.
Before you start with this tutorial, make sure you are logged into your server with a user account with sudo privileges or with the root user. The best practice is to run administrative commands as a sudo user instead of root, if you don’t have a sudo user on your CentOS system you can create one by following these instructions.
FirewallD uses the concepts of zones and services, instead of iptables chain and rules. Based on the zones and services you’ll configure, you can control what traffic is allowed or disallowed to and from the system. FirewallD can be configured and managed using the firewall-cmd command line utility.
Zones are predefined sets of rules specifying what traffic should be allowed based on the level of trust on the networks your computer is connected to. You can assign network interfaces and sources to a zone. Bellow are the zones provided by FirewallD ordered according to the trust level of the zone from untrusted to trusted:
drop: All incoming connections are dropped without any notification. Only outgoing connections are allowed.
block: All incoming connections are rejected with an icmp-host-prohibited message for IPv4 and icmp6-adm-prohibited for IPv6n. Only outgoing connections are allowed.
public: For use in untrusted public areas. You do not trust other computers on the network but you can allow selected incoming connections.
external: For use on external networks with NAT masquerading enabled when your system acts as a gateway or router. Only selected incoming connections are allowed.
internal: For use on internal networks when your system acts as a gateway or router. Other systems on the network are generally trusted. Only selected incoming connections are allowed.
dmz: Used for computers located in your demilitarized zone that will have limited access to the rest of your network. Only selected incoming connections are allowed.
work: Used for work machines. Other computers on the network are generally trusted. Only selected incoming connections are allowed.
home: Used for home machines. Other computers on the network are generally trusted. Only selected incoming connections are allowed.
trusted: All network connections are accepted. Trust all of the computers in the network.
Firewalld uses two separated configuration sets, runtime and the permanent configuration. The runtime configuration is the actual running configuration and it is not persistent on reboots. When the Firewalld service starts it loads the permanent configuration which becomes the runtime configuration. By default, when making changes to the Firewalld configuration using the firewall-cmd utility the changes are applied to the runtime configuration, to make the changes permanent you need to use the --permanent flag.
After enabling the FirewallD service for the first time, the public zone is set as a default zone. You can view the default zone by typing:
sudo firewall-cmd --get-default-zone
Copy
public
Copy
To get a list of all available zones, type:
sudo firewall-cmd --get-zones
Copy
block dmz drop external home internal public trusted work
Copy
By default, all network interfaces are assigned the default zone. To check what zones are used by your network interface(s) type:
sudo firewall-cmd --get-active-zones
Copy
public interfaces: eth0 eth1
Copy
The output above tell us that the both interfaces eth0 and eth1 are assigned to the public zone. You can print the zone configuration settings with:
sudo firewall-cmd --zone=public --list-all
Copy
public (active) target: default icmp-block-inversion: no interfaces: eth0 eth1 sources: services: ssh dhcpv6-client ports: protocols: masquerade: no forward-ports: source-ports: icmp-blocks: rich rules:
Copy
From the output above, we can see that the public zone is active and set as default, used by both eth0 and eth1 interfaces Also the connections related to the DHCP client and SSH are allowed. If you want to check the configurations of all available zones type:
sudo firewall-cmd --list-all-zones
Copy
The command will print a huge list will the settings of all available zone.
You can easily change the Interface Zone by using the using --zone flag in combination with the --change-interface flag. The following command will assign the eth1 interface to the work zone :
To change the default zone use the --set-default-zone flag followed by the name of the zone you want to make default. For example to change the default zone to home you should run the following command:
With FirewallD you can allow traffic for specific ports based on predefined rules called services. To get a list of all default available services type:
sudo firewall-cmd --get-services
Copy
You can find more information about each service by opening the associated .xml file within the /usr/lib/firewalld/services directory. For example, the HTTP service is defined like this:
/usr/lib/firewalld/services/http.xml
WWW (HTTP)
HTTP is the protocol used to serve Web pages. If you plan to make your Web server publicly available, enable this option. This option is not required for viewing pages locally or developing Web pages.protocol="tcp"port="80"/>CopyTo allow incoming HTTP traffic (port 80) for interfaces in the public zone, only for the current session (runtime configuration) type:
The command above will remove the http service from the public zone permanent configuration. What if you are running an application such as Plex Media Server for which there is no appropriate service available? In cases like these you have two options. You can either open up the appropriate ports or define a new FirewallD service. For example, the Plex Server listens on port 32400 and uses TCP, to open the port in the public zone for the current session use the --add-port= flag:
To verify that the port was added successfully use the --list-ports flag:
sudo firewall-cmd --zone=public --list-ports
Copy
32400/tcp
Copy
To keep the port 32400 open after a reboot add the rule to the permanent settings by running the same command using the --permanent flag. The syntax for removing a port is same as when adding a port. Just use --remove-port instead of the --add-port flag.
As we have already mentioned, the default services are stored in the /usr/lib/firewalld/services directory. The easiest way to create a new service is to copy an existing service file to the /etc/firewalld/services directory which is the location for user created services and modify the file settings. For example, to create a service definition for the Plex Media Server we can use the HTTP service file:
Open the newly created plexmediaserver.xml file and change the short name and description for the service within the and tags. The most important tag you need to change is the port tag which define the port number and protocol you want to open. In the following example we are opening 1900 UDP and 32400 TCP ports.
/etc/firewalld/services/plexmediaserver.xml
version="1.0"> plexmediaserver
Plex is a streaming media server that brings all your video, music and photo collections together and stream them to your devices at anytime and from anywhere.protocol="udp"port="1900"/>protocol="tcp"port="32400"/>CopySave the file and reload the FirewallD service:
sudo firewall-cmd --reload
Copy
You can now use the plexmediaserver service in your zones same as any other service..
To forward traffic from one port to another port or address, first enable masquerading for the desired zone using the --add-masquerade switch. For example to enable masquerading for external zone type:
In the following example we will show you how to configure your firewall if you were running a web server. We are assuming that your server has only one interface eth0, and you want to allow incoming traffic only on SSH, HTTP and HTTPS ports.
Change the default zone to dmz We will use the dmz (demilitarized) zone because by default it only allows SSH traffic. To change the default zone to dmz and to assign it to the eth0 interface run the following commands:
You have learned how to configure and manage the FirewallD service on your CentOS system. Be sure to allow all incoming connections that are necessary for proper functioning of you system, while limiting all unnecessary connections. If you have questions feel free to leave a comment below.