FirewallD : Allow connections only from certain IP addresses

thisisshantzz asked:

I am trying to use FirewallD to restrict access to a CentOS server from other machines on the network. It has a single network interface and it is operating in the public zone. Lets say that the ip address of this server

What I want to do is to allow only machines with IP addresses and to be able to connect (ssh and https) to this server. None of the other ip addresses should be able to connect to this server (or even know that it exists).

I tried using FirewallD’s rich rules as follows (on

sudo firewall-cmd --add-rich-rule 'rule family="ipv4" source address="" drop'

sudo firewall-cmd --add-rich-rule 'rule family="ipv4" source address="" accept'

sudo firewall-cmd --add-rich-rule 'rule family="ipv4" source address="" accept'

But it doesn’t seem to work. I cannot make ssh connections to from or

I tried entering the rules in the reverse order, but it still does not work.

Can someone help me out here? Do I need to change the zone from public to a more restrictive one like drop before the rules I wrote above can be applied?

My answer:

Rich rules aren’t the way to go about this. They’ll just create confusion, now and later.

Understand that a firewalld zone corresponds to a set of services that you may wish to allow, and the sources of the traffic to those services.

All you have to do is to set the services you want to allow in the zone (which you probably already have done) and then set the sources.

Traffic sources can be designated in two ways: By interface, or by source IP address. Traffic that matches any source passes this check.

So, what you want to do is to add the IP addresses allowed to reach the services, and then remove the interface (if any).

firewall-cmd --zone=public --add-source=
firewall-cmd --zone=public --add-source=
firewall-cmd --zone=public --remove-interface=enp2s1
firewall-cmd --runtime-to-permanent

And note that you probably do not want to do this in the public zone, but create a new zone. That zone has several things set up to be allowed by default (such as DHCP) which could cause you problems if you remove the interface and restrict the zone by source IP address.

View the full question and any other answers on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.