Useful FirewallD Rules To Configure and Manage Firewall in Linux
Useful FirewallD Rules To Configure and Manage Firewall in Linux
Firewalld provides a way to configure dynamic firewall rules in Linux that can be applied instantly,
without the need of firewall restart and also it support D-BUS and zone concepts which makes configuration
easy.
Firewalld replaced old Fedoras firewall (Fedora 18 onwards) mechanism, RHEL/CentOS 7 and
other latest distributions rely on this new mechanism. One of the biggest motive of introducing new firewall
system is that the old firewall needs a restart after making each change, thus breaking all active connections.
As said above, that the latest firewalld supports dynamic zones which is useful in configuring different set of
zones and rules for your office or home network via a command line or using a GUI method.
Initially, firewalld concept looks very difficult to configure, but services and zones makes it easier by
keeping both together as covered in this article.
In our earlier article, where we have seen how to play with firewalld and its zones, now here, in this
article, we will see some useful firewalld rules to configure your current Linux systems using command line
way.
1. Firewalld Configuration in RHEL/CentOS 7
All the examples covered in this article are practically tested on CentOS 7 distribution, and also works
on RHEL and Fedora distributions.
Before implementing firewalld rules, make sure to first check whether firewalld service enabled and
running.
# systemctl status firewalld
The above picture shows that firewalld is active and running. Now its time to check all the active
zones and active services.
# firewall-cmd --get-active-zones
# firewall-cmd --get-services
If incase, youre not familiar with command line, you can also manage firewalld from the GUI, for this
you need to have GUI package installed on the system, if not install it using the following command.
# yum install firewalld firewall-config
As said above, this article is specially written for command line lovers and all the examples, which
were going to cover are based on command line only, no GUI way..sorry..
Before moving further, first make sure to confirm on which public zone youre going to configure
Linux firewall and list all active services, ports, rich rules for public zone using following command.
# firewall-cmd --zone=public --list-all
Similarly, to remove added port, just use the remove option with firewalld command as shown
below.
# firewall-cmd --zone=public --remove-port=80/tcp
After adding or removing specific ports, make sure to confirm whether the port is added or removed
by using list-ports option.
# firewall-cmd --zone=public --list-ports
After enabling panic mode, try to ping any domain (say google.com) and check whether the panic
mode is ON using query-panic option as listed below.
# ping google.com -c 1
# firewall-cmd --query-panic
# firewall-cmd --query-panic
# firewall-cmd --panic-off
# ping google.com -c 1
4. Masquerading IP Address
Masquerade also known as Network Address Translation (NAT), which is basically a simple method
for allowing a computer to connect with internet with the help of base machine just a intermediary work.
Here, we will see how to forward a port to outside network. For example, if I want to do a ssh into my
home virtual machine from anywhere, I need to forward my ssh port 22 to different port (i.e. 2222).
Before doing a port forwarding, first make sure check whether Masquerade enabled for external zone,
because we are going to access the machine from outside network.
# firewall-cmd --zone=external --query-masquerade
Now lets forward all ssh port 22 connections to port 2222 for IP address 192.168.0.132.
# firewall-cmd --zone=external --add-forwardport=port=22:proto=tcp:toport=2222:toaddr=192.168.0.132
# firewall-cmd --zone=external --list-all
Port Forwarding
To add icmp block on any zone, you can use the following command. For example, here I am going to
add icmp block on external zone, before blocking, just do a icmp ping to confirm the status of icmp block.
# firewall-cmd --zone=public --query-icmp-block=echo-reply
If you get no, that means there isnt any icmp block applied, lets enable (block) icmp.
# firewall-cmd --zone=public --add-icmp-block=echo-reply
Change no to yes
Lockdown=yes
After making above changes, make sure to verify whether firewalld was lockdown using query.
# firewall-cmd --query-lockdown
After adding above rule, dont forget to list all the trusted public zone rules.
# firewall-cmd --zone=public --list-all
To remove any added rule, just replace the add-rich-rule with remove remove-rich-rule as
show in below command.
# firewall-cmd --zone=public --remove-rich-rule='rule family="ipv4" source
address="192.168.0.254" accept'
Reject IP Address
Conclusion
Here we have seen how to configure some of the rules and default services in firewalld. If there any
query regarding above firewalld rules, feel free to leave your valuable comments below.