Linux firewalls: What you need to know about iptables and firewalld (2024)

This article is excerpted from my book, Linux in Action, and a second Manning project that’s yet to be released.

The firewall

A firewall is a set of rules. When a data packet moves into or out of a protected network space, its contents (in particular, information about its origin, target, and the protocol it plans to use) are tested against the firewall rules to see if it should be allowed through. Here’s a simple example:

Linux firewalls: What you need to know about iptables and firewalld (1)

Image by:

A firewall can filter requests based on protocol or target-based rules.

On the one hand, iptables is a tool for managing firewall rules on a Linux machine.

On the other hand, firewalld is also a tool for managing firewall rules on a Linux machine.

You got a problem with that? And would it spoil your day if I told you that there was another tool out there, called nftables?

OK, I’ll admit that the whole thing does smell a bit funny, so let me explain. It all starts with Netfilter, which controls access to and from the network stack at the Linux kernel module level. For decades, the primary command-line tool for managing Netfilter hooks was the iptables ruleset.

Because the syntax needed to invoke those rules could come across as a bit arcane, various user-friendly implementations like ufw and firewalld were introduced as higher-level Netfilter interpreters. Ufw and firewalld are, however, primarily designed to solve the kinds of problems faced by stand-alone computers. Building full-sized network solutions will often require the extra muscle of iptables or, since 2014, its replacement, nftables (through the nft command line tool).

iptables hasn’t gone anywhere and is still widely used. In fact, you should expect to run into iptables-protected networks in your work as an admin for many years to come. But nftables, by adding on to the classic Netfilter toolset, has brought some important new functionality.

From here on, I’ll show by example how firewalld and iptables solve simple connectivity problems.

Configure HTTP access using firewalld

As you might have guessed from its name, firewalld is part of the systemd family. Firewalld can be installed on Debian/Ubuntu machines, but it’s there by default on Red Hat and CentOS. If you’ve got a web server like Apache running on your machine, you can confirm that the firewall is working by browsing to your server’s web root. If the site is unreachable, then firewalld is doing its job.

You’ll use the firewall-cmd tool to manage firewalld settings from the command line. Adding the –state argument returns the current firewall status:

# firewall-cmd --staterunning

By default, firewalld will be active and will reject all incoming traffic with a couple of exceptions, like SSH. That means your website won’t be getting too many visitors, which will certainly save you a lot of data transfer costs. As that’s probably not what you had in mind for your web server, though, you’ll want to open the HTTP and HTTPS ports that by convention are designated as 80 and 443, respectively. firewalld offers two ways to do that. One is through the –add-port argument that references the port number directly along with the network protocol it’ll use (TCP in this case). The –permanent argument tells firewalld to load this rule each time the server boots:

# firewall-cmd --permanent --add-port=80/tcp# firewall-cmd --permanent --add-port=443/tcp

The –reload argument will apply those rules to the current session:

# firewall-cmd --reload

Curious as to the current settings on your firewall? Run –list-services:

# firewall-cmd --list-servicesdhcpv6-client http https ssh

Assuming you’ve added browser access as described earlier, the HTTP, HTTPS, and SSH ports should now all be open—along with dhcpv6-client, which allows Linux to request an IPv6 IP address from a local DHCP server.

Configure a locked-down customer kiosk using iptables

I’m sure you’ve seen kiosks—they’re the tablets, touchscreens, and ATM-like PCs in a box that airports, libraries, and business leave lying around, inviting customers and passersby to browse content. The thing about most kiosks is that you don’t usually want users to make themselves at home and treat them like their own devices. They’re not generally meant for browsing, viewing YouTube videos, or launching denial-of-service attacks against the Pentagon. So to make sure they’re not misused, you need to lock them down.

One way is to apply some kind of kiosk mode, whether it’s through clever use of a Linux display manager or at the browser level. But to make sure you’ve got all the holes plugged, you’ll probably also want to add some hard network controls through a firewall. In the following section, I'll describe how I would do it using iptables.

There are two important things to remember about using iptables: The order you give your rules is critical, and by themselves, iptables rules won’t survive a reboot. I’ll address those here one at a time.

The kiosk project

To illustrate all this, let’s imagine we work for a store that’s part of a larger chain called BigMart. They’ve been around for decades; in fact, our imaginary grandparents probably grew up shopping there. But these days, the guys at BigMart corporate headquarters are probably just counting the hours before Amazon drives them under for good.

Nevertheless, BigMart’s IT department is doing its best, and they’ve just sent you some WiFi-ready kiosk devices that you’re expected to install at strategic locations throughout your store. The idea is that they’ll display a web browser logged into the BigMart.com products pages, allowing them to look up merchandise features, aisle location, and stock levels. The kiosks will also need access to bigmart-data.com, where many of the images and video media are stored.

Besides those, you’ll want to permit updates and, whenever necessary, package downloads. Finally, you’ll want to permit inbound SSH access only from your local workstation, and block everyone else. The figure below illustrates how it will all work:

Linux firewalls: What you need to know about iptables and firewalld (2)

Image by:

The kiosk traffic flow being controlled by iptables.

The script

Here’s how that will all fit into a Bash script:

#!/bin/bashiptables -A OUTPUT -p tcp -d bigmart.com -j ACCEPTiptables -A OUTPUT -p tcp -d bigmart-data.com -j ACCEPTiptables -A OUTPUT -p tcp -d ubuntu.com -j ACCEPTiptables -A OUTPUT -p tcp -d ca.archive.ubuntu.com -j ACCEPTiptables -A OUTPUT -p tcp --dport 80 -j DROPiptables -A OUTPUT -p tcp --dport 443 -j DROPiptables -A INPUT -p tcp -s 10.0.3.1 --dport 22 -j ACCEPTiptables -A INPUT -p tcp -s 0.0.0.0/0 --dport 22 -j DROP

The basic anatomy of our rules starts with -A, telling iptables that we want to add the following rule. OUTPUT means that this rule should become part of the OUTPUT chain. -p indicates that this rule will apply only to packets using the TCP protocol, where, as -d tells us, the destination is bigmart.com. The -j flag points to ACCEPT as the action to take when a packet matches the rule. In this first rule, that action is to permit, or accept, the request. But further down, you can see requests that will be dropped, or denied.

Remember that order matters. And that’s because iptables will run a request past each of its rules, but only until it gets a match. So an outgoing browser request for, say, youtube.com will pass the first four rules, but when it gets to either the –dport 80 or –dport 443 rule—depending on whether it’s an HTTP or HTTPS request—it’ll be dropped. iptables won’t bother checking any further because that was a match.

On the other hand, a system request to ubuntu.com for a software upgrade will get through when it hits its appropriate rule. What we’re doing here, obviously, is permitting outgoing HTTP or HTTPS requests to only our BigMart or Ubuntu destinations and no others.

The final two rules will deal with incoming SSH requests. They won’t already have been denied by the two previous drop rules since they don’t use ports 80 or 443, but 22. In this case, login requests from my workstation will be accepted but requests for anywhere else will be dropped. This is important: Make sure the IP address you use for your port 22 rule matches the address of the machine you’re using to log in—if you don’t do that, you’ll be instantly locked out. It's no big deal, of course, because the way it’s currently configured, you could simply reboot the server and the iptables rules will all be dropped. If you’re using an LXC container as your server and logging on from your LXC host, then use the IP address your host uses to connect to the container, not its public address.

You’ll need to remember to update this rule if my machine’s IP ever changes; otherwise, you’ll be locked out.

Playing along at home (hopefully on a throwaway VM of some sort)? Great. Create your own script. Now I can save the script, use chmod to make it executable, and run it as sudo. Don’t worry about that bigmart-data.com not found error—of course it’s not found; it doesn’t exist.

chmod +X scriptname.shsudo ./scriptname.sh

You can test your firewall from the command line using cURL. Requesting ubuntu.com works, but manning.com fails.

curl ubuntu.comcurl manning.com

Configuring iptables to load on system boot

The Linux Terminal

Now, how do I get these rules to automatically load each time the kiosk boots? The first step is to save the current rules to a .rules file using the iptables-save tool. That’ll create a file in the root directory containing a list of the rules. The pipe, followed by the tee command, is necessary to apply my sudo authority to the second part of the string: the actual saving of a file to the otherwise restricted root directory.

I can then tell the system to run a related tool called iptables-restore every time it boots. A regular cron job of the kind we saw in the previous module won’t help because they’re run at set times, but we have no idea when our computer might decide to crash and reboot.

There are lots of ways to handle this problem. Here’s one:

On my Linux machine, I’ll install a program called anacron that will give us a file in the /etc/ directory called anacrontab. I’ll edit the file and add this iptables-restore command, telling it to load the current values of that .rules file into iptables each day (when necessary) one minute after a boot. I’ll give the job an identifier (iptables-restore) and then add the command itself. Since you’re playing along with me at home, you should test all this out by rebooting your system.

sudo iptables-save | sudo tee /root/my.active.firewall.rulessudo apt install anacronsudo nano /etc/anacrontab1 1 iptables-restore iptables-restore < /root/my.active.firewall.rules

I hope these practical examples have illustrated how to use iptables and firewalld for managing connectivity issues on Linux-based firewalls.

10 Comments

These comments are closed.

Linux firewalls: What you need to know about iptables and firewalld (3)This work is licensed under a Creative Commons Attribution-Share Alike 4.0 International License.

Linux firewalls: What you need to know about iptables and firewalld (2024)

FAQs

Linux firewalls: What you need to know about iptables and firewalld? ›

Firewalls are tools that can protect an OS. Linux has iptables and firewalld, which contain firewall rules and can manage firewall rules in Linux. Essentially, iptables and firewalld are configured by the systems administrator to reject or accept traffic.

What is the difference between firewalld and iptables in Linux? ›

Iptables is a little more complicate to use, but UFW simplifies it. Firewalld is easy to use for basic rules but can be complicated if we need to manage some advanced rules. The major difference between then is the notion of zone, firewalld work with zone. By default, All rules are applied in the default zone.

What is a firewall and iptables? ›

Iptables is a standard firewall included in most Linux distributions by default. It is a command-line interface to the kernel-level netfilter hooks that can manipulate the Linux network stack. It works by matching each packet that crosses the networking interface against a set of rules to decide what to do.

What does firewalld do in Linux? ›

Firewalld can restrict access to services, ports, and networks. You can block specific subnets and IP addresses. As with any firewall, firewalld inspects all traffic traversing the various interfaces on your system. The traffic is allowed or rejected if the source address network matches a rule.

What is the purpose of iptables in Linux? ›

iptables is a user-space utility program that allows a system administrator to configure the IP packet filter rules of the Linux kernel firewall, implemented as different Netfilter modules. The filters are organized in different tables, which contain chains of rules for how to treat network traffic packets.

What is the relationship between iptables and firewalld? ›

firewalld is just a daemon that interacts with iptables, which is the actual kernel-based packet filtering system. They aren't two different firewalls. What is the difference between iptables and uncomplicated firewall(UFW) in Linux?

Can I use iptables and firewalld? ›

That means you could use iptables. However, if you are using firewalld, you don't need to use neither iptables, nor nft, this does firewalld for you. Since there are many ways to screw up things with iptables/nft rules (even for professionals), firewalld would be the most recommended way to configure the firewall.

What are the 3 types of firewalls? ›

The 3 Types of Firewalls You Need to Know
  • Network-Based Firewall. A network-based firewall routes traffic between networks. ...
  • Application Firewall. An application firewall (also called an application layer firewall) works with the TCP/IP stack to filter and intercept all traffic packets to/from apps. ...
  • Proxy Server.
Jun 22, 2022

How to use iptables firewall in Linux? ›

How to Install and Use Iptables Linux Firewall
  1. Connect to your server via SSH. If you don't know, you can read our SSH tutorial.
  2. Execute the following command one by one: sudo apt-get update sudo apt-get install iptables.
  3. Check the status of your current iptables configuration by running: sudo iptables -L -v.
Jan 17, 2024

What are the advantages of firewall with iptables? ›

IPTables is a very powerful security tool used to block unwanted traffic, allow desired traffic, redirect packets to alternate TCP/UDP ports, redirect packets to alternate IP addresses, protect against Denial of Service attacks (DoS) and so much more.

What is the difference between firewall and firewalld? ›

Firewalls are tools that can protect an OS. Linux has iptables and firewalld, which contain firewall rules and can manage firewall rules in Linux. Essentially, iptables and firewalld are configured by the systems administrator to reject or accept traffic.

Which firewall is most commonly used on Linux? ›

​ The most widely used command-line-based firewall is Iptables/Netfilter. It is the initial line of defense for the security of a Linux server.

What type of firewall is firewalld? ›

Firewalld provides a dynamically managed firewall with support for network/firewall zones that define the trust level of network connections or interfaces. It has support for IPv4, IPv6 firewall settings, ethernet bridges and IP sets. There is a separation of runtime and permanent configuration options.

How many tables are in iptables? ›

iptables contains five tables: raw is used only for configuring packets so that they are exempt from connection tracking. filter is the default table, and is where all the actions typically associated with a firewall take place. nat is used for network address translation (e.g. port forwarding).

What is the rule of iptables? ›

You can have a rule in the filter table. Iptables rules define what traffic is allowed or blocked based on the source and destination IP address, port number, and protocol. Each packet is checked against each rule. Rules are basically row inside the tables we described above.

What layer does iptables use? ›

iptables operates at OSI Layer 3 (Network). For OSI Layer 2 (Link), there are other technologies such as ebtables (Ethernet Bridge Tables).

Does Ubuntu use iptables or FirewallD? ›

The default firewall configuration tool for Ubuntu is ufw. Developed to ease iptables firewall configuration, ufw provides a user-friendly way to create an IPv4 or IPv6 host-based firewall. ufw by default is initially disabled.

What is the difference between FirewallD and nftables? ›

Starting with el8 the kernel has nf_tables for the rules. You are able to read (and write) those with 'nft'. FirewallD reads config files that are in FirewallD's syntax and generates rules into the kernel. FirewallD used to write to netfilter and does now write to nf_tables.

What is replacing iptables? ›

nftables is the default and recommended firewalling framework in Debian, and it replaces the old iptables (and related) tools.

Top Articles
Latest Posts
Article information

Author: Rev. Porsche Oberbrunner

Last Updated:

Views: 6232

Rating: 4.2 / 5 (73 voted)

Reviews: 80% of readers found this page helpful

Author information

Name: Rev. Porsche Oberbrunner

Birthday: 1994-06-25

Address: Suite 153 582 Lubowitz Walks, Port Alfredoborough, IN 72879-2838

Phone: +128413562823324

Job: IT Strategist

Hobby: Video gaming, Basketball, Web surfing, Book restoration, Jogging, Shooting, Fishing

Introduction: My name is Rev. Porsche Oberbrunner, I am a zany, graceful, talented, witty, determined, shiny, enchanting person who loves writing and wants to share my knowledge and understanding with you.