How to Manage a Linux Firewall
Firewalls are important components of a layered approach to security. Here's how to monitor and test firewall configurations regularly to ensure proper security.
Aug 18th, 2024 12:00pm by
Feature image via Pexels.
This article on services fits into a larger series of Linux articles covering various sysadmin topics, including hardware identification and managing system processes. You can build a lab environment by following the information in the Linux: Companion Lab for Linux Skill Blocks Repository article. In this series, we also covered how to pick a distribution and installation platform, how the Linux kernel interacts with hardware and how Linux manages system services, storage, file permissions, system processes, and user and group permissions.
Firewalls control access to networks and (potentially) network devices, including workstations and servers. Administrators rely on firewalls to permit or deny connections based on various criteria, including source, destination and protocol type.
Firewalls have the following three primary functions:
- Filter network traffic.
- Act as a gatekeeper between networks and network segments.
- Log and monitor network connection attempts.
- Network Address Translation (NAT) support to manage internal and external IP addresses.
- Virtual Private Network (VPN) endpoint support to allow secure connectivity.
- Network-based firewalls: Control access between networks or network segments to protect all data and devices in each segment.
- Host-based firewalls: Control connections to or from devices, helping to protect the data and services on each device.
Understand How Firewalls Manage Network Traffic
Basic firewalls identify network traffic based on three criteria: source, destination and protocol. Firewalls accomplish this by examining addressing information found in TCP/IP communications. This data shows the sending device’s IP address, the destination system’s IP address and the communication protocol in use. Suppose workstation3 with IP address 192.168.2.200 wants to establish an HTTP connection to webserver02 with IP address 192.168.2.10 . The firewall checks its rules to see whether the client device is allowed to send traffic to the destination server. It also checks the rules to see whether HTTP (port 80) traffic is allowed. People tend to identify application layer services by protocol, such as the Hypertext Transfer Protocol (HTTP), Simple Mail Transfer Protocol (SMTP) or the Secure Shell (SSH). Network devices do not usually recognize these protocols by names but rather identify them by numeric values called port numbers. There are potentially over 65,000 possible port numbers, but only the first 1,023 are standardized. These are referred to as the “well-known” port numbers. Here are a few common protocols and their well-known port numbers:- Hypertext Transfer Protocol (HTTP): Port 80, web services
- Hypertext Transfer Protocol Secure (HTTPS): Port 443, encrypted web services
- Secure Shell (SSH): Port 22, secure remote administration
- File Transfer Protocol (FTP): Port 21, file transfers
- Simple Mail Transfer Protocol (SMTP): Port 25, email transfers
- Post Office Protocol v3 (POP3): Port 110, accessing email
- Internet Message Access Protocol (IMAP4): Port 143, accessing email
- Network Time Protocol (NTP): Port 123, time synchronization
- Remote Desktop Protocol (RDP): Port 3389, remote connectivity to graphical user interfaces
Common Linux Firewall Interfaces
The underlying firewall service for Linux is iptables or nftables. These are configurable parts of the Linux kernel capable of filtering network traffic. You’ll use a frontend application to manage these settings. There are several of these frontend programs, but two of the most common are listed below:- Uncomplicated Firewall (UFW): Straightforward if less advanced configuration.
- firewalld: More complex but with more configuration options.
Uncomplicated Firewall (UFW) Settings
The basic command syntax is the ufw command followed by one or more subcommands and configuration parameters. These tend to be pretty simple with UFW. It may be a good idea to reset the UFW to its defaults if you’re on a Debian-based system where you’re unsure of the current configuration. I don’t recommend this on a production workstation or server, as it may disrupt communications. Use the following commands to reset the inbound and outbound UFW settings:
$ sudo ufw default deny incoming
$ sudo ufw default deny outgoing
$ sudo ufw app list
$ sudo ufw allow OpenSSH
$ sudo ufw allow 2222
$ sudo ufw disable
$ sudo ufw enable
$ sudo ufw status
$ sudo ufw allow from 192.168.2.42 to any port 22
$ sudo ufw deny from 192.168.2.200
- List the current rules: sudo ufw status
- Add any necessary rules: sudo ufw allow OpenSSH
- Review the updated rules: sudo ufw status
- Reload the UFW configuration: sudo ufw disable followed by sudo ufw enable
Figure 1: Allow the OpenSSH application through the UFW.
Firewalld Settings
Linux distributions derived from Red Hat tend to rely on the firewalld interface to manage connectivity. This utility uses the firewall-cmd command with a series of flags to define your settings. However, the overall functionality is the same as with UFW — define which connections are allowed and which are not. You can do this by service name, protocol or port number. The firewall-cmd command manages firewalld settings. This command contains many flags to display and configure rules. Notice that these flags use two dashes (–option ), whereas many other Linux command options use only a single dash (-option ). Use an = character to define the parameter or setting.
$ sudo firewall-cmd --list-all
Figure 2: The firewall-cmd command displays the default zone's rules.
- Home
- Work
- Internal
- DMZ
- Docker
- Public
$ sudo firewall-cmd --get-active-zones
$ sudo firewall-cmd --get-zones
Figure 3: The --get-zones subcommand displays the available zones.
$ sudo firewall-cmd --list-all-zones
$ sudo ip addr
$ sudo firewall-cmd --change-interface=enp0s5 --zone=public --permanent
Figure 4: Assign an interface to a specific zone.
- –permanent
- –zone=
- –add-service=
- –add-port=
- –remove-service=
- –remove-port=
- –reload
$ sudo firewall-cmd --zone=public --add-service=ssh --permanent
$ sudo firewall-cmd --zone=public --add-port=22/tcp --permanent
Figure 5: Configure the firewall for recognized services.
Figure 6: Configure the firewall for specific port numbers.
$ sudo firewall-cmd --list-all
$ sudo firewall-cmd --zone=public --remove-service=ssh --permanent
$ sudo firewall-cmd --reload
Figure 7: Reload the firewall configuration after any changes.
- List the current rules: sudo firewall-cmd –zone=public –list-all
- Permanently add any necessary rules: sudo firewall-cmd –zone-public –add-service=ssh –permanent
- Remove any rules that no longer apply: sudo firewall-cmd –zone-public –remove-service=http –permanent
- Review the new settings: sudo firewall-cmd –zone=public –list all
- Reload the firewall to update the settings: sudo firewall-cmd –reload
What About Graphical Firewall Interfaces?
It’s often easier to work with system configurations using a graphical interface. Command line tools are fast and scriptable, but only if you remember the specific commands. A graphical tool may be best if you’re sitting at a Linux workstation with a graphical user interface (GUI) and you just need to add a firewall rule quickly. Both UFW and firewalld have GUI options available.The GUFW Interface
Use the following steps if GUFW is not already installed on your Debian-based distribution. You must add the universe repository, update the Apt configuration, and install the package. Here are the commands:
$ sudo apt add-apt-repository universe
$ sudo apt update
$ sudo apt install gufw -y
Figure 8: Search for the GUFW interface if it is installed.
Figure 9: Select the appropriate profile.
Figure 10: Add rules, such as allowing port 22 traffic.
The firewall-config Interface
Add the firewall-config utility to configure the firewall using a graphical interface if you’re on a Linux distribution related to Red Hat Linux. Use this command to install firewall-config :
$ sudo dnf install -y firewall-config
Figure 11: Search for the GUI firewall configuration tool.
Figure 12: Select a zone and then check boxes for protocols or services to allow.
Test Firewall Configurations
It’s a good idea to test your firewall settings, too. Obviously, you can read the rules and logic out what effect they should have on inbound connection attempts. This is certainly valid for basic auditing and confirmation. However, you should also test the allowed connections from remote systems to ensure the services that should have access actually do have access. Finally, consider using a network scanning application like Nmap to validate the settings. This approach is more efficient than checking connectivity from lots of remote devices.Wrap Up
Firewalls are important components of a layered approach to security. Monitor and test firewall configurations regularly to ensure network services and systems are properly secured. In most cases, Linux workstations and servers should use host-based firewall configurations to permit only necessary inbound connections. The connections will vary depending on the device’s role, such as webserver or database host. Firewalls typically default to a “deny all” configuration. Some administrators use Linux systems as routers between network segments. The firewall mechanism supports this role too. You must know which firewall interface is available on your selected distribution.- UFW: Found on Debian-based systems, including Ubuntu, Linux Mint and Debian itself.
- firewalld: Found Red Hat-based systems, including Red Hat Enterprise Linux (RHEL), Fedora, AlmaLinux and Rocky Linux.
- GUFW: For UFW-based systems, such as those derived from Debian.
- firewall-config: For firewalld-based systems, such as those derived from Red Hat Linux.
YOUTUBE.COM/THENEWSTACK
Tech moves fast, don't miss an episode. Subscribe to our YouTube
channel to stream all our podcasts, interviews, demos, and more.