Chapter 44. Using and configuring firewalld
A firewall is a way to protect machines from any unwanted traffic from outside. It enables users to control incoming network traffic on host machines by defining a set of firewall rules. These rules are used to sort the incoming traffic and either block it or allow through.
firewalld
is a firewall service daemon that provides a dynamic customizable host-based firewall with a D-Bus interface. Being dynamic, it enables creating, changing, and deleting the rules without the necessity to restart the firewall daemon each time the rules are changed.
firewalld
uses the concepts of zones and services, that simplify the traffic management. Zones are predefined sets of rules. Network interfaces and sources can be assigned to a zone. The traffic allowed depends on the network your computer is connected to and the security level this network is assigned. Firewall services are predefined rules that cover all necessary settings to allow incoming traffic for a specific service and they apply within a zone.
Services use one or more ports or addresses for network communication. Firewalls filter communication based on ports. To allow network traffic for a service, its ports must be open. firewalld
blocks all traffic on ports that are not explicitly set as open. Some zones, such as trusted, allow all traffic by default.
Note that firewalld
with nftables
backend does not support passing custom nftables
rules to firewalld
, using the --direct
option.
44.1. Getting started with firewalld
44.1.1. When to use firewalld, nftables, or iptables
The following is a brief overview in which scenario you should use one of the following utilities:
-
firewalld
: Use thefirewalld
utility for simple firewall use cases. The utility is easy to use and covers the typical use cases for these scenarios. -
nftables
: Use thenftables
utility to set up complex and performance critical firewalls, such as for a whole network. -
iptables
: Theiptables
utility on Red Hat Enterprise Linux 8 uses thenf_tables
kernel API instead of thelegacy
back end. Thenf_tables
API provides backward compatibility so that scripts that useiptables
commands still work on Red Hat Enterprise Linux 8. For new firewall scripts, Red Hat recommends to usenftables
.
To avoid that the different firewall services influence each other, run only one of them on a RHEL host, and disable the other services.
44.1.2. Zones
firewalld
can be used to separate networks into different zones according to the level of trust that the user has decided to place on the interfaces and traffic within that network. A connection can only be part of one zone, but a zone can be used for many network connections.
NetworkManager
notifies firewalld
of the zone of an interface. You can assign zones to interfaces with:
-
NetworkManager
-
firewall-config
tool -
firewall-cmd
command-line tool - The RHEL web console
The latter three can only edit the appropriate NetworkManager
configuration files. If you change the zone of the interface using the web console, firewall-cmd
or firewall-config
, the request is forwarded to NetworkManager
and is not handled by firewalld
.
The predefined zones are stored in the /usr/lib/firewalld/zones/
directory and can be instantly applied to any available network interface. These files are copied to the /etc/firewalld/zones/
directory only after they are modified. The default settings of the predefined zones are as follows:
block
-
Any incoming network connections are rejected with an icmp-host-prohibited message for
IPv4
and icmp6-adm-prohibited forIPv6
. Only network connections initiated from within the system are possible. dmz
- For computers in your demilitarized zone that are publicly-accessible with limited access to your internal network. Only selected incoming connections are accepted.
drop
- Any incoming network packets are dropped without any notification. Only outgoing network connections are possible.
external
- For use on external networks with masquerading enabled, especially for routers. You do not trust the other computers on the network to not harm your computer. Only selected incoming connections are accepted.
home
- For use at home when you mostly trust the other computers on the network. Only selected incoming connections are accepted.
internal
- For use on internal networks when you mostly trust the other computers on the network. Only selected incoming connections are accepted.
public
- For use in public areas where you do not trust other computers on the network. Only selected incoming connections are accepted.
trusted
- All network connections are accepted.
work
- For use at work where you mostly trust the other computers on the network. Only selected incoming connections are accepted.
One of these zones is set as the default zone. When interface connections are added to NetworkManager
, they are assigned to the default zone. On installation, the default zone in firewalld
is set to be the public
zone. The default zone can be changed.
The network zone names should be self-explanatory and to allow users to quickly make a reasonable decision. To avoid any security problems, review the default zone configuration and disable any unnecessary services according to your needs and risk assessments.
Additional resources
-
The
firewalld.zone(5)
man page.
44.1.3. Predefined services
A service can be a list of local ports, protocols, source ports, and destinations, as well as a list of firewall helper modules automatically loaded if a service is enabled. Using services saves users time because they can achieve several tasks, such as opening ports, defining protocols, enabling packet forwarding and more, in a single step, rather than setting up everything one after another.
Service configuration options and generic file information are described in the firewalld.service(5)
man page. The services are specified by means of individual XML configuration files, which are named in the following format: service-name.xml
. Protocol names are preferred over service or application names in firewalld
.
Services can be added and removed using the graphical firewall-config
tool, firewall-cmd
, and firewall-offline-cmd
.
Alternatively, you can edit the XML files in the /etc/firewalld/services/
directory. If a service is not added or changed by the user, then no corresponding XML file is found in /etc/firewalld/services/
. The files in the /usr/lib/firewalld/services/
directory can be used as templates if you want to add or change a service.
Additional resources
-
The
firewalld.service(5)
man page
44.2. Viewing the current status and settings of firewalld
44.2.1. Viewing the current status of firewalld
The firewall service, firewalld
, is installed on the system by default. Use the firewalld
CLI interface to check that the service is running.
Procedure
To see the status of the service:
# firewall-cmd --state
For more information about the service status, use the
systemctl status
sub-command:# systemctl status firewalld firewalld.service - firewalld - dynamic firewall daemon Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor pr Active: active (running) since Mon 2017-12-18 16:05:15 CET; 50min ago Docs: man:firewalld(1) Main PID: 705 (firewalld) Tasks: 2 (limit: 4915) CGroup: /system.slice/firewalld.service └─705 /usr/bin/python3 -Es /usr/sbin/firewalld --nofork --nopid
Additional resources
It is important to know how firewalld
is set up and which rules are in force before you try to edit the settings. To display the firewall settings, use the firewall-cmd --list-all
as the root
user.
44.2.2. Viewing allowed services using GUI
To view the list of services using the graphical firewall-config tool, press the Super key to enter the Activities Overview, type firewall
, and press Enter. The firewall-config tool appears. You can now view the list of services under the Services
tab.
You can start the graphical firewall configuration tool using the command-line.
Procedure
To start the graphical firewall configuration tool using the command-line:
$
firewall-config
The Firewall Configuration
window opens. Note that this command can be run as a normal user, but you are prompted for an administrator password occasionally.
44.2.3. Viewing firewalld settings using CLI
With the CLI client, it is possible to get different views of the current firewall settings. The --list-all
option shows a complete overview of the firewalld
settings.
firewalld
uses zones to manage the traffic. If a zone is not specified by the --zone
option, the command is effective in the default zone assigned to the active network interface and connection.
Procedure
To list all the relevant information for the default zone:
#
firewall-cmd --list-all
public target: default icmp-block-inversion: no interfaces: sources: services: ssh dhcpv6-client ports: protocols: masquerade: no forward-ports: source-ports: icmp-blocks: rich rules:To specify the zone for which to display the settings, add the
--zone=zone-name
argument to thefirewall-cmd --list-all
command, for example:#
firewall-cmd --list-all --zone=home
home target: default icmp-block-inversion: no interfaces: sources: services: ssh mdns samba-client dhcpv6-client ... [trimmed for clarity]To see the settings for particular information, such as services or ports, use a specific option. See the
firewalld
manual pages or get a list of the options using the command help:#
firewall-cmd --help
To see which services are allowed in the current zone:
#
firewall-cmd --list-services
ssh dhcpv6-client
Listing the settings for a certain subpart using the CLI tool can sometimes be difficult to interpret. For example, you allow the SSH
service and firewalld
opens the necessary port (22) for the service. Later, if you list the allowed services, the list shows the SSH
service, but if you list open ports, it does not show any. Therefore, it is recommended to use the --list-all
option to make sure you receive a complete information.
44.3. Controlling network traffic using firewalld
44.3.1. Disabling all traffic in case of emergency using CLI
In an emergency situation, such as a system attack, it is possible to disable all network traffic and cut off the attacker.
Procedure
To immediately disable networking traffic, switch panic mode on:
# firewall-cmd --panic-on
ImportantEnabling panic mode stops all networking traffic. For this reason, it should be used only when you have the physical access to the machine or if you are logged in using a serial console.
Switching off panic mode reverts the firewall to its permanent settings. To switch panic mode off, enter:
# firewall-cmd --panic-off
Verification step
To see whether panic mode is switched on or off, use:
# firewall-cmd --query-panic
44.3.2. Controlling traffic with predefined services using CLI
The most straightforward method to control traffic is to add a predefined service to firewalld
. This opens all necessary ports and modifies other settings according to the service definition file.
Procedure
Check that the service is not already allowed:
# firewall-cmd --list-services ssh dhcpv6-client
List all predefined services:
# firewall-cmd --get-services RH-Satellite-6 amanda-client amanda-k5-client bacula bacula-client bitcoin bitcoin-rpc bitcoin-testnet bitcoin-testnet-rpc ceph ceph-mon cfengine condor-collector ctdb dhcp dhcpv6 dhcpv6-client dns docker-registry ... [trimmed for clarity]
Add the service to the allowed services:
# firewall-cmd --add-service=<service-name>
Make the new settings persistent:
# firewall-cmd --runtime-to-permanent
44.3.3. Controlling traffic with predefined services using GUI
This procedure describes how to control the network traffic with predefined services using graphical user interface.
Procedure
To enable or disable a predefined or custom service:
- Start the firewall-config tool and select the network zone whose services are to be configured.
-
Select the
Services
tab. - Select the check box for each type of service you want to trust or clear the check box to block a service.
To edit a service:
- Start the firewall-config tool.
-
Select
Permanent
from the menu labeledConfiguration
. Additional icons and menu buttons appear at the bottom of the window. - Select the service you want to configure.
The Ports
, Protocols
, and Source Port
tabs enable adding, changing, and removing of ports, protocols, and source port for the selected service. The modules tab is for configuring Netfilter helper modules. The Destination
tab enables limiting traffic to a particular destination address and Internet Protocol (IPv4
or IPv6
).
It is not possible to alter service settings in the Runtime
mode.
44.3.4. Adding new services
Services can be added and removed using the graphical firewall-config tool, firewall-cmd
, and firewall-offline-cmd
. Alternatively, you can edit the XML files in /etc/firewalld/services/
. If a service is not added or changed by the user, then no corresponding XML file are found in /etc/firewalld/services/
. The files /usr/lib/firewalld/services/
can be used as templates if you want to add or change a service.
Service names must be alphanumeric and can, additionally, include only _
(underscore) and -
(dash) characters.
Procedure
To add a new service in a terminal, use firewall-cmd
, or firewall-offline-cmd
in case of not active firewalld
.
Enter the following command to add a new and empty service:
$
firewall-cmd --new-service=service-name --permanent
To add a new service using a local file, use the following command:
$
firewall-cmd --new-service-from-file=service-name.xml --permanent
You can change the service name with the additional
--name=service-name
option.As soon as service settings are changed, an updated copy of the service is placed into
/etc/firewalld/services/
.As
root
, you can enter the following command to copy a service manually:# cp /usr/lib/firewalld/services/service-name.xml /etc/firewalld/services/service-name.xml
firewalld
loads files from /usr/lib/firewalld/services
in the first place. If files are placed in /etc/firewalld/services
and they are valid, then these will override the matching files from /usr/lib/firewalld/services
. The overridden files in /usr/lib/firewalld/services
are used as soon as the matching files in /etc/firewalld/services
have been removed or if firewalld
has been asked to load the defaults of the services. This applies to the permanent environment only. A reload is needed to get these fallbacks also in the runtime environment.
44.3.5. Opening ports using GUI
To permit traffic through the firewall to a certain port:
- Start the firewall-config tool and select the network zone whose settings you want to change.
-
Select the
Ports
tab and click the button on the right-hand side. ThePort and Protocol
window opens. - Enter the port number or range of ports to permit.
-
Select
tcp
orudp
from the list.
44.3.6. Controlling traffic with protocols using GUI
To permit traffic through the firewall using a certain protocol:
- Start the firewall-config tool and select the network zone whose settings you want to change.
-
Select the
Protocols
tab and click theAdd
button on the right-hand side. TheProtocol
window opens. -
Either select a protocol from the list or select the
Other Protocol
check box and enter the protocol in the field.
44.3.7. Opening source ports using GUI
To permit traffic through the firewall from a certain port:
- Start the firewall-config tool and select the network zone whose settings you want to change.
-
Select the
Source Port
tab and click theAdd
button on the right-hand side. TheSource Port
window opens. -
Enter the port number or range of ports to permit. Select
tcp
orudp
from the list.
44.4. Controlling ports using CLI
Ports are logical devices that enable an operating system to receive and distinguish network traffic and forward it accordingly to system services. These are usually represented by a daemon that listens on the port, that is it waits for any traffic coming to this port.
Normally, system services listen on standard ports that are reserved for them. The httpd
daemon, for example, listens on port 80. However, system administrators by default configure daemons to listen on different ports to enhance security or for other reasons.
44.4.1. Opening a port
Through open ports, the system is accessible from the outside, which represents a security risk. Generally, keep ports closed and only open them if they are required for certain services.
Procedure
To get a list of open ports in the current zone:
List all allowed ports:
# firewall-cmd --list-ports
Add a port to the allowed ports to open it for incoming traffic:
# firewall-cmd --add-port=port-number/port-type
The port types are either
tcp
,udp
,sctp
, ordccp
. The type must match the type of network communication.Make the new settings persistent:
# firewall-cmd --runtime-to-permanent
The port types are either
tcp
,udp
,sctp
, ordccp
. The type must match the type of network communication.
44.4.2. Closing a port
When an open port is no longer needed, close that port in firewalld
. It is highly recommended to close all unnecessary ports as soon as they are not used because leaving a port open represents a security risk.
Procedure
To close a port, remove it from the list of allowed ports:
List all allowed ports:
# firewall-cmd --list-ports
WarningThis command will only give you a list of ports that have been opened as ports. You will not be able to see any open ports that have been opened as a service. Therefore, you should consider using the
--list-all
option instead of--list-ports
.Remove the port from the allowed ports to close it for the incoming traffic:
# firewall-cmd --remove-port=port-number/port-type
Make the new settings persistent:
# firewall-cmd --runtime-to-permanent
44.5. Working with firewalld zones
Zones represent a concept to manage incoming traffic more transparently. The zones are connected to networking interfaces or assigned a range of source addresses. You manage firewall rules for each zone independently, which enables you to define complex firewall settings and apply them to the traffic.
44.5.1. Listing zones
This procedure describes how to list zones using the command line.
Procedure
To see which zones are available on your system:
# firewall-cmd --get-zones
The
firewall-cmd --get-zones
command displays all zones that are available on the system, but it does not show any details for particular zones.To see detailed information for all zones:
# firewall-cmd --list-all-zones
To see detailed information for a specific zone:
# firewall-cmd --zone=zone-name --list-all
44.5.2. Modifying firewalld settings for a certain zone
The Section 44.3.2, “Controlling traffic with predefined services using CLI” and Section 44.4, “Controlling ports using CLI” explain how to add services or modify ports in the scope of the current working zone. Sometimes, it is required to set up rules in a different zone.
Procedure
To work in a different zone, use the
--zone=zone-name
option. For example, to allow theSSH
service in the zone public:# firewall-cmd --add-service=ssh --zone=public
44.5.3. Changing the default zone
System administrators assign a zone to a networking interface in its configuration files. If an interface is not assigned to a specific zone, it is assigned to the default zone. After each restart of the firewalld
service, firewalld
loads the settings for the default zone and makes it active.
Procedure
To set up the default zone:
Display the current default zone:
# firewall-cmd --get-default-zone
Set the new default zone:
# firewall-cmd --set-default-zone zone-name
NoteFollowing this procedure, the setting is a permanent setting, even without the
--permanent
option.
44.5.4. Assigning a network interface to a zone
It is possible to define different sets of rules for different zones and then change the settings quickly by changing the zone for the interface that is being used. With multiple interfaces, a specific zone can be set for each of them to distinguish traffic that is coming through them.
Procedure
To assign the zone to a specific interface:
List the active zones and the interfaces assigned to them:
# firewall-cmd --get-active-zones
Assign the interface to a different zone:
# firewall-cmd --zone=zone_name --change-interface=interface_name --permanent
44.5.5. Assigning a zone to a connection using nmcli
This procedure describes how to add a firewalld
zone to a NetworkManager
connection using the nmcli
utility.
Procedure
Assign the zone to the
NetworkManager
connection profile:# nmcli connection modify profile connection.zone zone_name
Reload the connection:
# nmcli connection up profile
44.5.6. Manually assigning a zone to a network connection in an ifcfg file
When the connection is managed by NetworkManager, it must be aware of a zone that it uses. For every network connection, a zone can be specified, which provides the flexibility of various firewall settings according to the location of the computer with portable devices. Thus, zones and settings can be specified for different locations, such as company or home.
Procedure
To set a zone for a connection, edit the
/etc/sysconfig/network-scripts/ifcfg-connection_name
file and add a line that assigns a zone to this connection:ZONE=zone_name
44.5.7. Creating a new zone
To use custom zones, create a new zone and use it just like a predefined zone. New zones require the --permanent
option, otherwise the command does not work.
Procedure
Create a new zone:
# firewall-cmd --new-zone=zone-name
Check if the new zone is added to your permanent settings:
# firewall-cmd --get-zones
Make the new settings persistent:
# firewall-cmd --runtime-to-permanent
44.5.8. Zone configuration files
Zones can also be created using a zone configuration file. This approach can be helpful when you need to create a new zone, but want to reuse the settings from a different zone and only alter them a little.
A firewalld
zone configuration file contains the information for a zone. These are the zone description, services, ports, protocols, icmp-blocks, masquerade, forward-ports and rich language rules in an XML file format. The file name has to be zone-name.xml
where the length of zone-name is currently limited to 17 chars. The zone configuration files are located in the /usr/lib/firewalld/zones/
and /etc/firewalld/zones/
directories.
The following example shows a configuration that allows one service (SSH
) and one port range, for both the TCP
and UDP
protocols:
<?xml version="1.0" encoding="utf-8"?> <zone> <short>My zone</short> <description>Here you can describe the characteristic features of the zone.</description> <service name="ssh"/> <port port="1025-65535" protocol="tcp"/> <port port="1025-65535" protocol="udp"/> </zone>
To change settings for that zone, add or remove sections to add ports, forward ports, services, and so on.
Additional resources
-
For more information, see the
firewalld.zone
manual pages.
44.5.9. Using zone targets to set default behavior for incoming traffic
For every zone, you can set a default behavior that handles incoming traffic that is not further specified. Such behaviour is defined by setting the target of the zone. There are four options - default
, ACCEPT
, REJECT
, and DROP
. By setting the target to ACCEPT
, you accept all incoming packets except those disabled by a specific rule. If you set the target to REJECT
or DROP
, you disable all incoming packets except those that you have allowed in specific rules. When packets are rejected, the source machine is informed about the rejection, while there is no information sent when the packets are dropped.
Procedure
To set a target for a zone:
List the information for the specific zone to see the default target:
$ firewall-cmd --zone=zone-name --list-all
Set a new target in the zone:
# firewall-cmd --permanent --zone=zone-name --set-target=<default|ACCEPT|REJECT|DROP>
44.6. Using zones to manage incoming traffic depending on a source
44.6.1. Using zones to manage incoming traffic depending on a source
You can use zones to manage incoming traffic based on its source. That enables you to sort incoming traffic and route it through different zones to allow or disallow services that can be reached by that traffic.
If you add a source to a zone, the zone becomes active and any incoming traffic from that source will be directed through it. You can specify different settings for each zone, which is applied to the traffic from the given sources accordingly. You can use more zones even if you only have one network interface.
44.6.2. Adding a source
To route incoming traffic into a specific source, add the source to that zone. The source can be an IP address or an IP mask in the classless inter-domain routing (CIDR) notation.
In case you add multiple zones with an overlapping network range, they are ordered alphanumerically by zone name and only the first one is considered.
To set the source in the current zone:
# firewall-cmd --add-source=<source>
To set the source IP address for a specific zone:
# firewall-cmd --zone=zone-name --add-source=<source>
The following procedure allows all incoming traffic from 192.168.2.15 in the trusted
zone:
Procedure
List all available zones:
# firewall-cmd --get-zones
Add the source IP to the trusted zone in the permanent mode:
# firewall-cmd --zone=trusted --add-source=192.168.2.15
Make the new settings persistent:
# firewall-cmd --runtime-to-permanent
44.6.3. Removing a source
Removing a source from the zone cuts off the traffic coming from it.
Procedure
List allowed sources for the required zone:
# firewall-cmd --zone=zone-name --list-sources
Remove the source from the zone permanently:
# firewall-cmd --zone=zone-name --remove-source=<source>
Make the new settings persistent:
# firewall-cmd --runtime-to-permanent
44.6.4. Adding a source port
To enable sorting the traffic based on a port of origin, specify a source port using the --add-source-port
option. You can also combine this with the --add-source
option to limit the traffic to a certain IP address or IP range.
Procedure
To add a source port:
# firewall-cmd --zone=zone-name --add-source-port=<port-name>/<tcp|udp|sctp|dccp>
44.6.5. Removing a source port
By removing a source port you disable sorting the traffic based on a port of origin.
Procedure
To remove a source port:
# firewall-cmd --zone=zone-name --remove-source-port=<port-name>/<tcp|udp|sctp|dccp>
44.6.6. Using zones and sources to allow a service for only a specific domain
To allow traffic from a specific network to use a service on a machine, use zones and source. The following procedure allows traffic from 192.168.1.0/24 to be able to reach the HTTP service while any other traffic is blocked.
Procedure
List all available zones:
# firewall-cmd --get-zones block dmz drop external home internal public trusted work
Add the source to the trusted zone to route the traffic originating from the source through the zone:
# firewall-cmd --zone=trusted --add-source=192.168.1.0/24
Add the http service in the trusted zone:
# firewall-cmd --zone=trusted --add-service=http
Make the new settings persistent:
# firewall-cmd --runtime-to-permanent
Check that the trusted zone is active and that the service is allowed in it:
# firewall-cmd --zone=trusted --list-all trusted (active) target: ACCEPT sources: 192.168.1.0/24 services: http
44.7. Configuring NAT using firewalld
With firewalld
, you can configure the following network address translation (NAT) types:
- Masquerading
- Source NAT (SNAT)
- Destination NAT (DNAT)
- Redirect
44.7.1. The different NAT types: masquerading, source NAT, destination NAT, and redirect
These are the different network address translation (NAT) types:
- Masquerading and source NAT (SNAT)
Use one of these NAT types to change the source IP address of packets. For example, Internet Service Providers do not route private IP ranges, such as
10.0.0.0/8
. If you use private IP ranges in your network and users should be able to reach servers on the Internet, map the source IP address of packets from these ranges to a public IP address.Both masquerading and SNAT are very similar. The differences are:
- Masquerading automatically uses the IP address of the outgoing interface. Therefore, use masquerading if the outgoing interface uses a dynamic IP address.
- SNAT sets the source IP address of packets to a specified IP and does not dynamically look up the IP of the outgoing interface. Therefore, SNAT is faster than masquerading. Use SNAT if the outgoing interface uses a fixed IP address.
- Destination NAT (DNAT)
- Use this NAT type to rewrite the destination address and port of incoming packets. For example, if your web server uses an IP address from a private IP range and is, therefore, not directly accessible from the Internet, you can set a DNAT rule on the router to redirect incoming traffic to this server.
- Redirect
- This type is a special case of DNAT that redirects packets to the local machine depending on the chain hook. For example, if a service runs on a different port than its standard port, you can redirect incoming traffic from the standard port to this specific port.
44.7.2. Configuring IP address masquerading
The following procedure describes how to enable IP masquerading on your system. IP masquerading hides individual machines behind a gateway when accessing the Internet.
Procedure
To check if IP masquerading is enabled (for example, for the
external
zone), enter the following command asroot
:# firewall-cmd --zone=external --query-masquerade
The command prints
yes
with exit status0
if enabled. It printsno
with exit status1
otherwise. Ifzone
is omitted, the default zone will be used.To enable IP masquerading, enter the following command as
root
:# firewall-cmd --zone=external --add-masquerade
-
To make this setting persistent, repeat the command adding the
--permanent
option.
To disable IP masquerading, enter the following command as root
:
# firewall-cmd --zone=external --remove-masquerade --permanent
44.8. Port forwarding
Redirecting ports using this method only works for IPv4-based traffic. For IPv6 redirecting setup, you must use rich rules.
To redirect to an external system, it is necessary to enable masquerading. For more information, see Configuring IP address masquerading.
44.8.1. Adding a port to redirect
Using firewalld
, you can set up ports redirection so that any incoming traffic that reaches a certain port on your system is delivered to another internal port of your choice or to an external port on another machine.
Prerequisites
- Before you redirect traffic from one port to another port, or another address, you have to know three things: which port the packets arrive at, what protocol is used, and where you want to redirect them.
Procedure
To redirect a port to another port:
# firewall-cmd --add-forward-port=port=port-number:proto=tcp|udp|sctp|dccp:toport=port-number
To redirect a port to another port at a different IP address:
Add the port to be forwarded:
# firewall-cmd --add-forward-port=port=port-number:proto=tcp|udp:toport=port-number:toaddr=IP
Enable masquerade:
# firewall-cmd --add-masquerade
44.8.2. Redirecting TCP port 80 to port 88 on the same machine
Follow the steps to redirect the TCP port 80 to port 88.
Procedure
Redirect the port 80 to port 88 for TCP traffic:
# firewall-cmd --add-forward-port=port=80:proto=tcp:toport=88
Make the new settings persistent:
# firewall-cmd --runtime-to-permanent
Check that the port is redirected:
# firewall-cmd --list-all
44.8.3. Removing a redirected port
This procedure describes how to remove the redirected port.
Procedure
To remove a redirected port:
# firewall-cmd --remove-forward-port=port=port-number:proto=<tcp|udp>:toport=port-number:toaddr=<IP>
To remove a forwarded port redirected to a different address:
Remove the forwarded port:
# firewall-cmd --remove-forward-port=port=port-number:proto=<tcp|udp>:toport=port-number:toaddr=<IP>
Disable masquerade:
# firewall-cmd --remove-masquerade
44.8.4. Removing TCP port 80 forwarded to port 88 on the same machine
This procedure describes how to remove the port redirection.
Procedure
List redirected ports:
~]# firewall-cmd --list-forward-ports port=80:proto=tcp:toport=88:toaddr=
Remove the redirected port from the firewall::
~]# firewall-cmd --remove-forward-port=port=80:proto=tcp:toport=88:toaddr=
Make the new settings persistent:
~]# firewall-cmd --runtime-to-permanent
44.9. Managing ICMP requests
The Internet Control Message Protocol
(ICMP
) is a supporting protocol that is used by various network devices to send error messages and operational information indicating a connection problem, for example, that a requested service is not available. ICMP
differs from transport protocols such as TCP and UDP because it is not used to exchange data between systems.
Unfortunately, it is possible to use the ICMP
messages, especially echo-request
and echo-reply
, to reveal information about your network and misuse such information for various kinds of fraudulent activities. Therefore, firewalld
enables blocking the ICMP
requests to protect your network information.
44.9.1. Listing and blocking ICMP requests
Listing ICMP
requests
The ICMP
requests are described in individual XML files that are located in the /usr/lib/firewalld/icmptypes/
directory. You can read these files to see a description of the request. The firewall-cmd
command controls the ICMP
requests manipulation.
To list all available
ICMP
types:# firewall-cmd --get-icmptypes
The
ICMP
request can be used by IPv4, IPv6, or by both protocols. To see for which protocol theICMP
request has used:# firewall-cmd --info-icmptype=<icmptype>
The status of an
ICMP
request showsyes
if the request is currently blocked orno
if it is not. To see if anICMP
request is currently blocked:# firewall-cmd --query-icmp-block=<icmptype>
Blocking or unblocking ICMP
requests
When your server blocks ICMP
requests, it does not provide the information that it normally would. However, that does not mean that no information is given at all. The clients receive information that the particular ICMP
request is being blocked (rejected). Blocking the ICMP
requests should be considered carefully, because it can cause communication problems, especially with IPv6 traffic.
To see if an
ICMP
request is currently blocked:# firewall-cmd --query-icmp-block=<icmptype>
To block an
ICMP
request:# firewall-cmd --add-icmp-block=<icmptype>
To remove the block for an
ICMP
request:# firewall-cmd --remove-icmp-block=<icmptype>
Blocking ICMP
requests without providing any information at all
Normally, if you block ICMP
requests, clients know that you are blocking it. So, a potential attacker who is sniffing for live IP addresses is still able to see that your IP address is online. To hide this information completely, you have to drop all ICMP
requests.
-
To block and drop all
ICMP
requests: Set the target of your zone to
DROP
:# firewall-cmd --permanent --set-target=DROP
Now, all traffic, including ICMP
requests, is dropped, except traffic which you have explicitly allowed.
To block and drop certain ICMP
requests and allow others:
Set the target of your zone to
DROP
:# firewall-cmd --permanent --set-target=DROP
Add the ICMP block inversion to block all
ICMP
requests at once:# firewall-cmd --add-icmp-block-inversion
Add the ICMP block for those
ICMP
requests that you want to allow:# firewall-cmd --add-icmp-block=<icmptype>
Make the new settings persistent:
# firewall-cmd --runtime-to-permanent
The block inversion inverts the setting of the ICMP
requests blocks, so all requests, that were not previously blocked, are blocked because of the target of your zone changes to DROP
. The requests that were blocked are not blocked. This means that if you want to unblock a request, you must use the blocking command.
To revert the block inversion to a fully permissive setting:
Set the target of your zone to
default
orACCEPT
:# firewall-cmd --permanent --set-target=default
Remove all added blocks for
ICMP
requests:# firewall-cmd --remove-icmp-block=<icmptype>
Remove the
ICMP
block inversion:# firewall-cmd --remove-icmp-block-inversion
Make the new settings persistent:
# firewall-cmd --runtime-to-permanent
44.9.2. Configuring the ICMP filter using GUI
-
To enable or disable an
ICMP
filter, start the firewall-config tool and select the network zone whose messages are to be filtered. Select theICMP Filter
tab and select the check box for each type ofICMP
message you want to filter. Clear the check box to disable a filter. This setting is per direction and the default allows everything. -
To edit an
ICMP
type, start the firewall-config tool and selectPermanent
mode from the menu labeledConfiguration
. Additional icons appear at the bottom of the window. Select in the following dialog to enable masquerading and to make forwarding to another machine working. -
To enable inverting the
ICMP Filter
, click theInvert Filter
check box on the right. Only markedICMP
types are now accepted, all other are rejected. In a zone using the DROP target, they are dropped.
44.10. Setting and controlling IP sets using firewalld
To see the list of IP set types supported by firewalld
, enter the following command as root.
~]# firewall-cmd --get-ipset-types hash:ip hash:ip,mark hash:ip,port hash:ip,port,ip hash:ip,port,net hash:mac hash:net hash:net,iface hash:net,net hash:net,port hash:net,port,net
44.10.1. Configuring IP set options using CLI
IP sets can be used in firewalld
zones as sources and also as sources in rich rules. In Red Hat Enterprise Linux, the preferred method is to use the IP sets created with firewalld
in a direct rule.
To list the IP sets known to
firewalld
in the permanent environment, use the following command asroot
:# firewall-cmd --permanent --get-ipsets
To add a new IP set, use the following command using the permanent environment as
root
:# firewall-cmd --permanent --new-ipset=test --type=hash:net success
The previous command creates a new IP set with the name test and the
hash:net
type forIPv4
. To create an IP set for use withIPv6
, add the--option=family=inet6
option. To make the new setting effective in the runtime environment, reloadfirewalld
.List the new IP set with the following command as
root
:# firewall-cmd --permanent --get-ipsets test
To get more information about the IP set, use the following command as
root
:# firewall-cmd --permanent --info-ipset=test test type: hash:net options: entries:
Note that the IP set does not have any entries at the moment.
To add an entry to the test IP set, use the following command as
root
:# firewall-cmd --permanent --ipset=test --add-entry=192.168.0.1 success
The previous command adds the IP address 192.168.0.1 to the IP set.
To get the list of current entries in the IP set, use the following command as
root
:# firewall-cmd --permanent --ipset=test --get-entries 192.168.0.1
Generate a file containing a list of IP addresses, for example:
# cat > iplist.txt <<EOL 192.168.0.2 192.168.0.3 192.168.1.0/24 192.168.2.254 EOL
The file with the list of IP addresses for an IP set should contain an entry per line. Lines starting with a hash, a semi-colon, or empty lines are ignored.
To add the addresses from the iplist.txt file, use the following command as
root
:# firewall-cmd --permanent --ipset=test --add-entries-from-file=iplist.txt success
To see the extended entries list of the IP set, use the following command as
root
:# firewall-cmd --permanent --ipset=test --get-entries 192.168.0.1 192.168.0.2 192.168.0.3 192.168.1.0/24 192.168.2.254
To remove the addresses from the IP set and to check the updated entries list, use the following commands as
root
:# firewall-cmd --permanent --ipset=test --remove-entries-from-file=iplist.txt success # firewall-cmd --permanent --ipset=test --get-entries 192.168.0.1
You can add the IP set as a source to a zone to handle all traffic coming in from any of the addresses listed in the IP set with a zone. For example, to add the test IP set as a source to the drop zone to drop all packets coming from all entries listed in the test IP set, use the following command as
root
:# firewall-cmd --permanent --zone=drop --add-source=ipset:test success
The
ipset:
prefix in the source showsfirewalld
that the source is an IP set and not an IP address or an address range.
Only the creation and removal of IP sets is limited to the permanent environment, all other IP set options can be used also in the runtime environment without the --permanent
option.
Red Hat does not recommend using IP sets that are not managed through firewalld
. To use such IP sets, a permanent direct rule is required to reference the set, and a custom service must be added to create these IP sets. This service needs to be started before firewalld
starts, otherwise firewalld
is not able to add the direct rules using these sets. You can add permanent direct rules with the /etc/firewalld/direct.xml
file.
44.11. Prioritizing rich rules
By default, rich rules are organized based on their rule action. For example, deny
rules have precedence over allow
rules. The priority
parameter in rich rules provides administrators fine-grained control over rich rules and their execution order.
44.11.1. How the priority parameter organizes rules into different chains
You can set the priority
parameter in a rich rule to any number between -32768
and 32767
, and lower values have higher precedence.
The firewalld
service organizes rules based on their priority value into different chains:
-
Priority lower than 0: the rule is redirected into a chain with the
_pre
suffix. -
Priority higher than 0: the rule is redirected into a chain with the
_post
suffix. -
Priority equals 0: based on the action, the rule is redirected into a chain with the
_log
,_deny
, or_allow
the action.
Inside these sub-chains, firewalld
sorts the rules based on their priority value.
44.11.2. Setting the priority of a rich rule
The procedure describes an example of how to create a rich rule that uses the priority
parameter to log all traffic that is not allowed or denied by other rules. You can use this rule to flag unexpected traffic.
Procedure
Add a rich rule with a very low precedence to log all traffic that has not been matched by other rules:
# firewall-cmd --add-rich-rule='rule priority=32767 log prefix="UNEXPECTED: " limit value="5/m"'
The command additionally limits the number of log entries to
5
per minute.Optionally, display the
nftables
rule that the command in the previous step created:# nft list chain inet firewalld filter_IN_public_post table inet firewalld { chain filter_IN_public_post { log prefix "UNEXPECTED: " limit rate 5/minute } }
44.12. Configuring firewall lockdown
Local applications or services are able to change the firewall configuration if they are running as root
(for example, libvirt). With this feature, the administrator can lock the firewall configuration so that either no applications or only applications that are added to the lockdown allow list are able to request firewall changes. The lockdown settings default to disabled. If enabled, the user can be sure that there are no unwanted configuration changes made to the firewall by local applications or services.
44.12.1. Configuring lockdown using CLI
This procedure describes how to enable or disable lockdown using the command line.
To query whether lockdown is enabled, use the following command as
root
:# firewall-cmd --query-lockdown
The command prints
yes
with exit status0
if lockdown is enabled. It printsno
with exit status1
otherwise.To enable lockdown, enter the following command as
root
:# firewall-cmd --lockdown-on
To disable lockdown, use the following command as
root
:# firewall-cmd --lockdown-off
44.12.2. Configuring lockdown allowlist options using CLI
The lockdown allowlist can contain commands, security contexts, users and user IDs. If a command entry on the allowlist ends with an asterisk "*", then all command lines starting with that command will match. If the "*" is not there then the absolute command including arguments must match.
The context is the security (SELinux) context of a running application or service. To get the context of a running application use the following command:
$
ps -e --context
That command returns all running applications. Pipe the output through the grep tool to get the application of interest. For example:
$ ps -e --context | grep example_program
To list all command lines that are in the allowlist, enter the following command as
root
:# firewall-cmd --list-lockdown-whitelist-commands
To add a command command to the allowlist, enter the following command as
root
:# firewall-cmd --add-lockdown-whitelist-command='/usr/bin/python3 -Es /usr/bin/command'
To remove a command command from the allowlist, enter the following command as
root
:# firewall-cmd --remove-lockdown-whitelist-command='/usr/bin/python3 -Es /usr/bin/command'
To query whether the command command is in the allowlist, enter the following command as
root
:# firewall-cmd --query-lockdown-whitelist-command='/usr/bin/python3 -Es /usr/bin/command'
The command prints
yes
with exit status0
if true. It printsno
with exit status1
otherwise.To list all security contexts that are in the allowlist, enter the following command as
root
:# firewall-cmd --list-lockdown-whitelist-contexts
To add a context context to the allowlist, enter the following command as
root
:# firewall-cmd --add-lockdown-whitelist-context=context
To remove a context context from the allowlist, enter the following command as
root
:# firewall-cmd --remove-lockdown-whitelist-context=context
To query whether the context context is in the allowlist, enter the following command as
root
:# firewall-cmd --query-lockdown-whitelist-context=context
Prints
yes
with exit status0
, if true, printsno
with exit status1
otherwise.To list all user IDs that are in the allowlist, enter the following command as
root
:# firewall-cmd --list-lockdown-whitelist-uids
To add a user ID uid to the allowlist, enter the following command as
root
:# firewall-cmd --add-lockdown-whitelist-uid=uid
To remove a user ID uid from the allowlist, enter the following command as
root
:# firewall-cmd --remove-lockdown-whitelist-uid=uid
To query whether the user ID uid is in the allowlist, enter the following command:
$
firewall-cmd --query-lockdown-whitelist-uid=uid
Prints
yes
with exit status0
, if true, printsno
with exit status1
otherwise.To list all user names that are in the allowlist, enter the following command as
root
:# firewall-cmd --list-lockdown-whitelist-users
To add a user name user to the allowlist, enter the following command as
root
:# firewall-cmd --add-lockdown-whitelist-user=user
To remove a user name user from the allowlist, enter the following command as
root
:# firewall-cmd --remove-lockdown-whitelist-user=user
To query whether the user name user is in the allowlist, enter the following command:
$
firewall-cmd --query-lockdown-whitelist-user=user
Prints
yes
with exit status0
, if true, printsno
with exit status1
otherwise.
44.12.3. Configuring lockdown allowlist options using configuration files
The default allowlist configuration file contains the NetworkManager
context and the default context of libvirt
. The user ID 0 is also on the list.
<?xml version="1.0" encoding="utf-8"?> <whitelist> <selinux context="system_u:system_r:NetworkManager_t:s0"/> <selinux context="system_u:system_r:virtd_t:s0-s0:c0.c1023"/> <user id="0"/> </whitelist>
Following is an example allowlist configuration file enabling all commands for the firewall-cmd
utility, for a user called user whose user ID is 815
:
<?xml version="1.0" encoding="utf-8"?> <whitelist> <command name="/usr/libexec/platform-python -s /bin/firewall-cmd*"/> <selinux context="system_u:system_r:NetworkManager_t:s0"/> <user id="815"/> <user name="user"/> </whitelist>
This example shows both user id
and user name
, but only one option is required. Python is the interpreter and is prepended to the command line. You can also use a specific command, for example:
/usr/bin/python3 /bin/firewall-cmd --lockdown-on
In that example, only the --lockdown-on
command is allowed.
In Red Hat Enterprise Linux, all utilities are placed in the /usr/bin/
directory and the /bin/
directory is sym-linked to the /usr/bin/
directory. In other words, although the path for firewall-cmd
when entered as root
might resolve to /bin/firewall-cmd
, /usr/bin/firewall-cmd
can now be used. All new scripts should use the new location. But be aware that if scripts that run as root
are written to use the /bin/firewall-cmd
path, then that command path must be added in the allowlist in addition to the /usr/bin/firewall-cmd
path traditionally used only for non-root
users.
The *
at the end of the name attribute of a command means that all commands that start with this string match. If the *
is not there then the absolute command including arguments must match.