Firewalld - How to filter your traffic based on an IP, port or Region?

About

This page shows you how to filter your traffic based on a IP with firewalld rich rules.

HowTo

How to filter based on an IP

firewall-cmd --permanent \
  --add-rich-rule='rule family="ipv4" source address="192.168.78.76/32" protocol value="tcp" accept'

where:

  • –permanent means that the rule will be used even after reboot.
  • –add-rich-rule specifies the rich rule that has the following options:
    • family - the type of IP:
      • ipv4 for an ipv4 ip
      • ipv6 for an ipv6 ip
    • source address=“192.168.78.76/32” - the source specified as an cidr
    • protocol value=“tcp” - the network protocol (ie TCP generally, UDP if you know what you are doing)
    • accept - the action to take (accept|reject|drop|mark)

How to filter based on IP and Port

Execute this command:

firewall-cmd --permanent \
  --add-rich-rule='rule family="ipv4" source address="192.168.78.76/32" port port="8080" protocol="tcp" accept'

where:

  • –permanent means that the rule will be used even after reboot.
  • –add-rich-rule specifies the rich rule that has the following options:
    • family - the type of IP
    • source address=“192.168.78.76/32” - the source specified as an cidr
    • port port=“8080” - the port defined by port (you could also have defined a service)
    • protocol=“tcp” - the network protocol (ie TCP generally, UDP if you know what your are doing)
    • accept - the action to take

How to filter packets based on country

If you want to filter your packets by country, see this page:

How to restrict your traffic to a country with Firewalld / Iptable? (ie packet filtering by country)

How to filter based on a subnet (Cloudflare)

If you want to filter your HTTP packet based on subnet such as all server from Cloudlfare, see this page:

How to allow only the HTTP traffic from a subnet such as Cloudflare with Firewalld?





Discover More
Firewalld - A list of common How-to's

This page regroups a list of how to around the firewall firewalld If your firewall is locking you out of your machine, this article will explains you how to disable it and take control...



Share this page:
Follow us:
Task Runner