Table of Contents

Firewall daemon

General

In this manual, I just list the basic commands for setup and configure iptables firewall using firewalld agent.

IMPORTANT: The agent firewalld.service must be startet before you can use firewall-cmd command!

NOTE: To the most arguments, you can add –permanent option, to list the permanent settings (not dynamic ones!). You can eather use the –permanent option and then the firewall-cmd –reload command, or, do the command first with –permanent option and then do the same command without –permanent option.

Firewall zones

To list everything from one zone, you can use the –list-all switch:

Firewall interfaces

NOTE: If you have given a wrong ethernet interface name, it will not complain about the name, but just list no zone!

To list the interfaces of all zones, use this command:

Firewall services

A lot of services are preconfigured (port(s) and protocol(s)). Each zone can have different services, to list all services of default zone, use following command:

If you want to list all services, you can use this for loop:

Sometimes you want to have more information about a service configuration, which is pre-defined.

Firewall ports

If the port is not defined in a pre-defined service, you also can add ports to the configuration of the firewall. Here, I show how-to list these ports in a zone.

Configure

IMPORTANT: Also for the configuration, you can add the –permanent switch, this will make the setting permanent. If you ommit this switch, the setting is set dynamically, it will not survife a reboot!

NOTE: To setup a new service or port, you always should test it in dynamic mode, when fine, use the permanent switch.

NOTE: If you work on only one zone, it makes sense to define this zone as the default zone:

Add a service

NOTE: Don't forget the command to list all services, see «Firewall services»

Firewall ICMP types

IPSET informations

Remove services

NOTE: To list all services of the zone, check this out: «Firewall services»

Add a port

If the service you want to configure is not pre-defined, you can add the ports manually.

Remove ports

NOTE: To list all ports in a zone, refer to «Firewall ports» chapter.

Add interface to zone

Sometimes, it's necesary to add an interface to a zone.

firewall-cmd --add-interface=eth0 --zone=public

Rich rules

Sometimes it's very helpful to define source IP's or IP ranges. To do this, you have to add a rich rule using this switch –add-rich-rule:

Add a subnet (all ports)

In some cases, the whole subnet with enabling all ports is required. Next rule shows how-to.

Rich rule for nfs service

In this example, I add a rule for NFS service, which allows connections from '192.168.122.0/24' subnet.

Rich rule for ports

It is also possible to add as specified port (in this case '389' for LDAP). This allows all connections from '192.168.122.0/24' subnet on this port:

NOTE: This could also be done using the service rule!

Rich rule for a UDP port

Sometimes, you also need some UDP port open. For example DNS:

Add/remove rich rules

Sometimes it's necessary to add/remove special rules. For example you want to allow access to MySQL port '3306', but only from a defined source (IP). This can be done by using rich rules, the next example shows the how-to:

IMPORTANT: In this example the rule is not defined permanently!

Direct rules

In some cases, a role is not possible to implement using standard firewalld command (including rich rules). In such a case, you can add direct rules (syntax of the corresponding firewall: iptables or nftables syntax).

Check, if the rule in that sequence where you expect it (the sequence of rules is very important! You can use iptables -S or nft list ruleset to check this out)

You should not use direct rules, if you can do the same using other firewalld rules!

List all active direct rules

firewall-cmd --direct --get-all-rules

Add a direct rule

The syntax how-to add a direct rule is quite similar to the backend firewall (iptables, ebtables or nftables). Here an example:

firewall-cmd --permanent --direct --add-rule ipv4 filter FORWARD 1 -i ens192 -j DROP
firewall-cmd --direct --add-rule ipv4 filter FORWARD 1 -i ens192 -j DROP 

Description (based on iptables backend):

firewall-cmd --direct --add-rule <protocol> <table (filter, mangle, nat, ...)> <chain> <priority> <arguments> -j <action (DROP, ACCEPT, ...)> 

Remove a direct rule

To remove a rich rule, the syntax is the same, except –remove-rule:

firewall-cmd --permanent --direct --remove-rule ipv4 filter FORWARD 1 -i ens192 -j DROP
firewall-cmd --direct --remove-rule ipv4 filter FORWARD 1 -i ens192 -j DROP 

Forwarding ports

If you want to forward a port from extern to an internal network, you have to configure this on the external zone.

IPSET

Very important: This procedure does not work with firewalld and nftables backend!

To setup a blacklist using ipset, you have to follow this example:

  1. If you want to add first an old (active) ipset rule, do following:
    ipset save blockednets > ipsetsavelist.ipset
    sed -e 's/^add blockednets //' ipsetsavelist.ipset | grep -Ev '^create' > list
  2. Create the 'hash:net' ipset hash:
    firewall-cmd --permanent --new-ipset=blockednets --type=hash:net
  3. Add the network, you want to drop, from a file list. Each line must have only one IP:
    firewall-cmd --permanent --ipset=blockednets --add-entries-from-file=list
  4. Optionally add additional networks:
    firewall-cmd --permanent --ipset=blockednets --add-entry=119.6.204.0/24
  5. Shows the permanent entries in a ipset:
    firewall-cmd --permanent --ipset=blockednets --get-entries
  6. Add the ipset to the firewalld 'drop' zone and List forwarded ports:
    firewall-cmd --permanent --zone=drop --add-source=ipset:blockednets
  7. Reload firewalld after these changes:
    firewall-cmd --reload

Queries

Example

Here an example, how-to add an official blacklist into ipset: