Changes

Basic RHEL Firewall Configuration with firewalld

20,089 bytes added, 17:21, 10 June 2019
Created page with "{{#pagetitle: Basic RHEL 8 Firewall Configuration with firewalld }} <table border="0" cellspacing="0" width="100%"><tr> <td width="20%">Previous<td..."
{{#pagetitle: Basic RHEL 8 Firewall Configuration with firewalld }}
<table border="0" cellspacing="0" width="100%"><tr>
<td width="20%">[[RHEL Network Management|Previous]]<td align="center">[[Red Hat Enterprise Linux Essentials|Table of Contents]]<td width="20%" align="right">[[Configuring SSH Key-based Authentication on RHEL|Next]]</td>
<tr>
<td width="20%">RHEL 8 Network Management<td align="center"><td width="20%" align="right">Configuring SSH Key-based Authentication on RHEL 8</td>
</table>
<hr>


<htmlet>rhel8</htmlet>


A firewall is a vital component in protecting a computer system or network of computers from external attack (typically from an external source via an internet connection). Any computer connected directly to an internet connection must run a firewall to protect against malicious activity. Similarly, any internal network must have some form of firewall between it and an external internet connection.

All Linux distributions are provided with a firewall solution of some form. In the case of RHEL 8 this takes the form of a service named ''firewalld''.

While the subject of firewall configuration can be complex, fortunately RHEL 8 provides command-line, web-based and graphical tools that ease the firewall configuration process. This chapter will introduce the basic concepts of firewalld and cover the steps necessary to configure a firewall using the tools provided with the operating system.

== An Introduction to firewalld ==

The firewalld service uses a set a rules to control incoming network traffic and define which traffic is to be blocked and which is to be allowed to pass through to the system and is built on top of a more complex firewall tool named ''iptables''.

The firewalld system provides a flexible way to manage incoming traffic. The firewall could, for example, be configured to block traffic arriving from a specific external IP address, or to prevent all traffic arriving on a particular TCP/IP port. Rules may also be defined to forward incoming traffic to different systems or to act as an internet gateway to protect other computers on a network.

In keeping with common security practices, a default firewalld installation is configured to block all access with the exception of SSH remote login and the DHCP service used by the system to obtain a dynamic IP address (both of which are essential if the system administrator is to be able to gain access to the system after completing the installation).

The key elements of firewall configuration on RHEL 8 are ''zones'', ''interfaces'', ''services'' and ''ports''.

=== Zones ===

By default, firewalld is installed with a range of pre-configured ''zones. ''A zone is a preconfigured set of rules which can be applied to the system at any time to quickly implement firewall configurations for specific scenarios. The ''block'' zone, for example, blocks all incoming traffic, while the ''home'' zone imposes less strict rules on the assumption that the system is running in a safer environment where a greater level of trust is expected. New zones may be added to the system, and existing zones modified to add or remove rules. Zones may also be deleted entirely from the system. Table 13-1 lists the set of zones available by default on a RHEL 8 system:

=== Interfaces ===

Any RHEL 8 system connected to the internet or a network (or both) will contain at least one ''interface'' in the form of either a physical or virtual network device. When firewalld is active, each of these interfaces is assigned to a zone allowing different levels of firewall security to be assigned to different interfaces. Consider a server containing two interfaces, one connected externally to the internet and the other to an internal network. In such a scenario, the external facing interface would most likely be assigned the more restrictive ''external'' zone while the internal interface might use the ''internal'' zone.

=== Services ===


TCP/IP defines a set of services that communicate on standard ports. Secure HTTPS web connections, for example, use port 443, while the SMTP email service uses port 25. To selectively enable incoming traffic for specific services, firewalld rules can be added to zones. The ''home'' zone, for example, does not permit incoming HTTPS connections by default. This traffic can be enabled by adding rules to a zone to allow incoming HTTPS connections without having to reference the specific port number.

=== Ports ===


Although common TCP/IP services can be referenced when adding firewalld rules, situations will arise where incoming connections need to be allowed on a specific port that is not allocated to a service. This can be achieved by adding rules that reference specific ports instead of services. This technique was used in the chapter entitled [[An Overview of the RHEL Cockpit Web Interface|An Overview of the RHEL 8 Cockpit Web Interface]] when port 9090 was opened to allow access to the Cockpit web interface.

== Checking firewalld Status ==

The firewalld service is installed and enabled by default on all RHEL 8 installations. The status of the service can be checked via the following command:

<pre>
# systemctl status firewalld
firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
Active: active (running) since Thu 2019-02-14 14:24:31 EST; 3 days ago
Docs: man:firewalld(1)
Main PID: 816 (firewalld)
Tasks: 2 (limit: 25026)
Memory: 30.6M
CGroup: /system.slice/firewalld.service
816 /usr/libexec/platform-python -s /usr/sbin/firewalld --nofork --nopid
</pre>

If necessary, the firewalld service may be installed as follows:

<pre>
# dnf install firewalld
</pre>

The firewalld service is enabled by default so will start automatically both after installation is complete and each time the system boots.

=== Configuring Firewall Rules with firewall-cmd ===

The ''firewall-cmd'' command-line utility allows information about the firewalld configuration to be viewed and changes to be made to zones and rules from within a terminal window.

When making changes to the firewall settings, it is important to be aware of the concepts of ''runtim''e and ''permanent'' configurations. By default, any rule changes are considered to be runtime configuration changes. This means that while the changes will take effect immediately, they will be lost next time the system restarts or the firewalld service reloads, for example by issuing the following command:

<pre>
# firewall-cmd --reload
</pre>

To make a change permanent, the ''--permanent'' command-line option must be used. Permanent changes do not take effect until the firewalld service reloads but will remain in place until manually changed.

=== Identifying and Changing the Default Zone ===

To identify the default zone (in other words the zone to which all interfaces will be assigned unless a different zone is specifically selected) use the ''firewall-cmd'' tool as follows:

<pre>
# firewall-cmd --get-default-zone
public
</pre>

To change the default to a different zone:

<pre>
# firewall-cmd --set-default-zone=home
success
</pre>

=== Displaying Zone Information ===

To list all of the zones available on the system:

<pre>
# firewall-cmd --get-zones
block dmz drop external home internal libvirt public trusted work
</pre>

Obtain a list of zones currently active together with the interfaces to which they are assigned as follows:

<pre>
# firewall-cmd --get-active-zones
external
interfaces: eth0
internal
interfaces: eth1
</pre>

All of the rules currently configured for a specific zone may be listed as follows:

<pre>
# firewall-cmd --zone=home --list-all
home (active)
target: default
icmp-block-inversion: no
interfaces: eth0
sources:
services: cockpit dhcpv6-client http mdns samba-client ssh
ports:
protocols:
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:
</pre>

Use the following command to list the services currently available for inclusion in a firewalld rule:

<pre>
# firewall-cmd --get-services
RH-Satellite-6 amanda-client amanda-k5-client amqp amqps apcupsd audit bacula bacula-client bgp bitcoin bitcoin-rpc bitcoin-testnet bitcoin-testnet-rpc ceph ceph-mon cfengine cockpit ...
</pre>

To list the services currently enabled for a zone:

<pre>
# firewall-cmd --zone=public --list-services
cockpit dhcpv6-client ssh
</pre>

A list of port rules can be obtained as follows:

<pre>
# firewall-cmd --zone=public --list-ports
9090/tcp
</pre>

=== Adding and Removing Zone Services ===

To add a service to a zone, in this case adding HTTPS to the public zone, the following command would be used:

<pre>
# firewall-cmd --zone=public --add-service=https
success
</pre>

By default this is a ''runtime'' change, so the added rule will be lost after a system reboot. To add a service permanently so that it remains in effect next time the system restarts, use the ''--permanent'' flag:

<pre>
# firewall-cmd --zone=public --permanent --add-service=https
success
</pre>

To verify that a service has been added permanently, be sure to include the ''--permanent'' flag when requesting the service list:

<pre>
# firewall-cmd --zone=public --permanent --list-services
cockpit dhcpv6-client http https ssh
</pre>

Note that as a permanent change, this new rule will not take effect until the system restarts or firewalld reloads:

<pre>
# firewall-cmd --reload
</pre>

Remove a service from a zone using the ''--remove-service'' option. Since this is a runtime change, the rule will be re-instated the next time the system restarts:

<pre>
# firewall-cmd --zone=public --remove-service=https
</pre>

To remove a service permanently use the ''--permanent'' flag, remembering to reload firewalld if the change is required to take immediate effect.:

<pre>
# firewall-cmd --zone=public --permanent --remove-service=https
</pre>

=== Working with Port-based Rules ===


To enable a specific port, use the ''--add-port'' option. Note that when manually defining the port, both the port number and protocol (TCP or UDP) will need to be provided:

<pre>
# firewall-cmd --zone=public --permanent --add-port=5000/tcp
</pre>

It is also possible to specify a range of ports when adding a rule to a zone:

<pre>
# firewall-cmd --zone=public --permanent --add-port=5900-5999/udp
</pre>

=== Creating a New Zone ===

An entirely new zone may be created by running the following command. Once created, the zone may be managed in the same way as any of the predefined zones:

<pre>
# firewall-cmd --permanent --new-zone=myoffice
success
</pre>

After adding a new zone, firewalld will need to be restarted before the zone becomes available:

<pre>
# firewall-cmd --reload
success
</pre>

=== Changing Zone/Interface Assignments ===

As previously discussed, each interface on the system must be assigned to a zone. The zone to which an interface is assigned can also be changed using the ''firewall-cmd'' tool. In the following example, the eth0 interface is assigned to the ''public'' zone:

<pre>
firewall-cmd --zone=public --change-interface=eth0
success
</pre>

=== Masquerading ===

Masquerading is better known in networking administration circles as Network Address Translation (NAT). When using a RHEL 8 system as a gateway to the internet for a network of computers, masquerading allows all of the internal systems to use the IP address of that RHEL 8 system when communicating over the internet. This has the advantage of hiding the internal IP addresses of any systems from malicious external entities and also avoids the necessity to allocate a public IP address to every computer on the network.

Use the following command to check whether masquerading is already enabled on the firewall:

<pre>
# firewall-cmd --zone=external --query-masquerade
</pre>

Use the following command to enable masquerading (remembering to use the ''--permanent'' flag if the change is to be permanent):

<pre>
firewall-cmd --zone=external --add-masquerade
</pre>

=== Adding ICMP Rules ===

The Internet Control Message Protocol (ICMP) is used by client systems on networks to send information such as error messages to each other. It is also the foundation of the ''ping'' command which is used by network administrators and users alike to detect whether a particular client is alive on a network. The ICMP category allows for the blocking of specific ICMP message types. For example, an administrator might choose to block incoming ping (Echo Request) ICMP messages to prevent the possibility of a ping based denial of service (DoS) attack (where a server is maliciously bombarded with so many ping messages that it becomes unable to respond to legitimate requests).

To view the ICMP types available for inclusion in firewalld rules, run the following command:

<pre>
# firewall-cmd --get-icmptypes
address-unreachable bad-header beyond-scope communication-prohibited destination-unreachable echo-reply ...
</pre>

The following command, for example, permanently adds a rule to block echo-reply (ping request) messages for the public zone:

<pre>
# firewall-cmd --zone=public --permanent --add-icmp-block=echo-reply
</pre>

=== Implementing Port Forwarding ===


Port forwarding is used in conjunction with masquerading when the RHEL 8 system is acting as a gateway to the internet for an internal network of computer systems. Port forwarding allows traffic arriving at the firewall via the internet on a specific port to be forwarded to a particular system on the internal network. This is perhaps best described by way of an example.

Suppose that a RHEL 8 system is acting as the firewall for an internal network of computers and one of the systems on the network is configured as a web server. Let’s assume the web server system has an IP address of 192.168.2.20. The domain record for the web site hosted on this system is configured with the public IP address behind which the RHEL 8 firewall system sits. When an HTTP web page request arrives on port 80 the RHEL 8 system acting as the firewall needs to know what to do with it. By configuring port forwarding it is possible to direct all web traffic to the internal system hosting the web server (in this case, IP address 192.168.2.20), either continuing to use port 80 or diverting the traffic to a different port on the destination server. In fact, port forwarding can even be configured to forward the traffic to a different port on the same system as the firewall (a concept known as local forwarding).

To use port forwarding, begin by enabling masquerading as follows (in this case the assumption is made that the interface connected to the internet has been assigned to the ''external'' zone):

<pre>
# firewall-cmd --zone=external --add-masquerade
</pre>

To forward from a port to a different local port, a command similar to the following would be used:

<pre>
# firewall-cmd --zone=external --add-forward-port=port=22:proto=tcp:toport=2750
</pre>

In the above example, any TCP traffic arriving on port 22 will be forwarded to port 2750 on the local system. The following command, on the other hand, forwards port 20 on the local system to port 22 on the system with the IP address of 192.168.0.19:

<pre>
# firewall-cmd --zone=external \
--add-forward-port=port=20:proto=tcp:toport=22:toaddr=192.168.0.19
</pre>

Similarly, the following command forwards local port 20 to port 2750 on the system with IP address 192.168.0.18:

<pre>
# firewall-cmd --zone=external --add-forward-port=port=20:proto=tcp:toport=2750:toaddr=192.168.0.18
</pre>

== Managing firewalld from the Cockpit Interface ==

So far this chapter has provided an overview of firewalld and explored the use of the ''firewall-cmd ''command-line tool to manage firewall zones and interfaces. While ''firewall-cmd'' provides the most flexible way to manage the firewalld configuration, it is also possible to view and manage the services for the default zone within the Cockpit web console.

To access the firewalld settings, sign into the Cockpit interface and select ''Networking'' from the navigation panel. On the networking page, select the ''Firewall'' option as highlighted in Figure 13-1 below:


[[File:rhel_8_firewall_link_cockpit.png]]


Figure 13-1

The Firewall page displays the current service rules configured for the default zone (and allows services to be removed using the trash can buttons), new services to be added to the zone and for the firewall to be turned on and off:


[[File:rhel_8_firewall_cockpit_page.png]]


Figure 13-2

== Managing firewalld using firewall-config ==

If you have access to the graphical desktop environment, the firewall may also be configured using the ''firewall-config'' tool. Though not installed by default, ''firewall-config'' may be installed as follows:

<pre>
# dnf install firewall-config
</pre>

When launched, the main ''firewall-config'' screen appears as illustrated in Figure 13-3:


[[File:rhel_8_firewall-config.png]]


Figure 13-3

The key areas of the tool can be summarized as follows:

* '''A''' - Displays all of the currently active interfaces and the zones to which they are assigned. To assign an interface to a different zone, select it from this panel, click on the ''Change Zone'' button and select the required zone from the resulting dialog.

* '''B''' - Controls whether the information displayed and any changes made within the tool apply to the runtime or permanent rules.

* '''C''' - The list of zones, services or IPSets configured on the system. The information listed in this panel depends on the selection made from toolbar F. Selecting an item from the list in this panel updates the main panel marked D.

* '''D''' - The main panel containing information about the current category selection in toolbar E. In this example, the panel is displaying services for the ''public'' zone. The checkboxes next to each service control whether the service is enabled or not within the firewall. It is within these category panels that new rules can be added or existing rules configured or removed.

* '''E''' - Controls the content displayed in panel D. Selecting items from this bar displays the current rule for the chosen category.

* '''F''' - Controls the list displayed in panel C.

The ''firewall-config'' tool is straightforward and intuitive to use and allows many of the tasks available with ''firewall-cmd'' to be performed in a visual environment.

== Summary ==

A carefully planned and implemented firewall is a vital component of any secure system. In the case of RHEL 8, the firewalld service provides a firewall system that is both flexible and easy to administer.

The firewalld service uses the concept of zones to group together sets of firewall rules and includes a suite of pre-defined zones designed to meet a range of firewall protection requirements. These zones may be modified to add or remove rules, or entirely new zones created and configured. The network devices on the system that connect to networks or the internet are referred to as interfaces. Each interface, in turn, is assign to a zone. The primary tools for working with firewalld are the ''firewall-cmd ''command-line tool and the ''firewall-config'' graphical utility. Minimal firewall management options are also available via the Cockpit web interface.


<htmlet>rhel8</htmlet>


<hr>
<table border="0" cellspacing="0" width="100%"><tr>
<td width="20%">[[RHEL Network Management|Previous]]<td align="center">[[Red Hat Enterprise Linux Essentials|Table of Contents]]<td width="20%" align="right">[[Configuring SSH Key-based Authentication on RHEL|Next]]</td>
<tr>
<td width="20%">RHEL 8 Network Management<td align="center"><td width="20%" align="right">Configuring SSH Key-based Authentication on RHEL 8</td>
</table>