### TO ENABLE A http SERVICE IN THE DEFAULT ZONE
## before enabling, list currently enabled services
[root@rhel7server1 ~]# firewall-cmd --list-service
ssh dhcpv6-client dns nfs
## enable http
[root@rhel7server1 ~]# firewall-cmd --permanent --add-service=http
success
## reload firewalld
[root@rhel7server1 ~]# firewall-cmd --reload
success
## list enabled services now
[root@rhel7server1 ~]# firewall-cmd --list-service
ssh dhcpv6-client dns nfs http
### TO USE THE GRAPHICAL USER INTERFACE OF FIREWALL-CMD
[root@rhel7server1 ~]# firewall-config
### CHECK THAT FIREWALLD IS RUNNING
[root@rhel7server1 ~]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
Active: active (running) since Tue 2020-03-03 10:03:07 EST; 25min ago
## or
[root@rhel7server1 ~]# firewall-cmd --state
running
## Note: if the server is multi homed and you want to allow routing traffic to different interface, you must enable ip forwarding
## i.e. set kernel parameter net.ipv4.ip_forward=1 permanently by adding into /etc/sysctl.conf
### LIST AVAILABLE ZONES
[root@rhel7server1 ~]# firewall-cmd --get-zones
block dmz drop external home internal public trusted work
### GET THE DEFAULT ZONE
[root@rhel7server1 ~]# firewall-cmd --get-default-zone
public
### LIST ACTIVE ZONES
[root@rhel7server1 ~]# firewall-cmd --get-active-zones
public
interfaces: enp0s3 enp0s8
### CHANGE DEFAULT ZONE
[root@rhel7server1 ~]# firewall-cmd --set-default-zone=home
success
[root@rhel7server1 ~]# firewall-cmd --get-default-zone
home
[root@rhel7server1 ~]# grep DefaultZone /etc/firewalld/firewalld.conf
DefaultZone=home
### CHECK INTERFACES ASSOCIATED TO WHICH ZONE
[root@rhel7server1 ~]# nmcli device status
EVICE TYPE STATE CONNECTION
enp0s3 ethernet connected enp0s3
enp0s8 ethernet connected enp0s8
enp0s9 ethernet connected enp0s9
lo loopback unmanaged --
## get zones for each interface
[root@rhel7server1 ~]# firewall-cmd --get-zone-of-interface=enp0s3
public
[root@rhel7server1 ~]# firewall-cmd --get-zone-of-interface=enp0s8
no zone
[root@rhel7server1 ~]# firewall-cmd --get-zone-of-interface=enp0s9
public
## change zone for an interface
[root@rhel7mgmt1 ~]# firewall-cmd --change-zone=enp0s9 --zone=internal
success
[root@rhel7mgmt1 ~]# firewall-cmd --reload
success
[root@rhel7mgmt1 ~]# firewall-cmd --get-zone-of-interface=enp0s9
internal
### GET THE PERMANENT CONFIG OF THE public ZONE
[root@rhel7server1 ~]# firewall-cmd --permanent --zone=public --list-all
public
target: default
icmp-block-inversion: no
interfaces:
sources:
services: ssh dhcpv6-client dns nfs
ports:
protocols:
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:
### CREATE NEW ZONE
[root@rhel7server1 ~]# firewall-cmd --get-zones
block dmz drop external home internal public trusted work
[root@rhel7server1 ~]# firewall-cmd --permanent --new-zone=TEST
success
[root@rhel7server1 ~]# firewall-cmd --reload
success
[root@rhel7server1 ~]# firewall-cmd --get-zones
TEST block dmz drop external home internal public trusted work
### GET ALL KNOWN SERVICES
[root@rhel7server1 ~]# firewall-cmd --get-services
RH-Satellite-6 amanda-client amanda-k5-client bacula bacula-client
.... http ....
xmpp-bosh xmpp-client xmpp-local xmpp-server
### GET HELP ON FIREWALLD RICH RULES SYNTAX
[root@rhel7server1 ~]# man firewalld.richlanguage
### ADD / REMOVE RICH RULES
## add
[root@rhel7server1 ~]# firewall-cmd --add-rich-rule 'rule family="ipv4" source address="192.168.1.1" log accept'
success
## verify
[root@rhel7server1 ~]# firewall-cmd --list-rich-rules
rule family="ipv4" source address="192.168.1.1" log accept
## remove
[root@rhel7server1 ~]# firewall-cmd --remove-rich-rule 'rule family="ipv4" source address="192.168.1.1" log accept'
success
## re-list
[root@rhel7server1 ~]# firewall-cmd --list-rich-rules
### ANOTHER EXAMPLE OF RICH RULES
## add
[root@rhel7server1 html]# firewall-cmd --permanent --zone=public --add-rich-rule='rule family=ipv4 source address=192.168.0.20 service name="http" log level=notice prefix="[HTTP RULE] " limit value="20/s" accept'
success
## apply
[root@rhel7server1 html]# firewall-cmd --reload
success
## test from the source
[root@rhel7client1 html]# for i in {1..100};do curl http://rhel7server1;done;
<html><body>rhel7server1 web server</body></html>
...
<html><body>rhel7server1 web server</body></html>
## tail the log at the destination
[root@rhel7server1 html]#
tail /var/log/messages | grep HTTP.RULE
...
## to remove the rich rules
[root@rhel7server1 html]# firewall-cmd --permanent --zone=public --remove-rich-rule='rule family=ipv4 source address=192.168.0.20 service name="http" log level=notice prefix="[HTTP RULE] " limit value="20/s" accept'
success
[root@rhel7server1 html]# firewall-cmd --reload
success
### LISTING RICH RULES
[root@rhel7server1 ~]# firewall-cmd --list-rich-rules
rule family="ipv4" source address="192.168.0.20" forward-port port="8022" protocol="tcp" to-port="22"
### ADD A SOURCE TO A ZONE
[root@rhel7server1 ~]# firewall-cmd --permanent --zone=work --add-source=172.31.1.1
success
[root@rhel7server1 ~]# firewall-cmd --reload
success
### GET DETAILED INFO FOR A ZONE
[root@rhel7server1 ~]# firewall-cmd --info-zone=work
work (active)
target: default
icmp-block-inversion: no
interfaces:
sources: 172.31.1.1
services: ssh dhcpv6-client
ports:
protocols:
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:
### NAT
### Type 1: IP masquerading
### Type 2: Port redirection
### NAT - PORT REDIRECTION
## test before : direct ssh to port 22 allowed, ssh to port 8022 not allowed
[root@rhel7client1 ~]# ssh user1@rhel7server1
user1@rhel7server1's password: *******
[root@rhel7client1 ~]# ssh -p 8022 user1@rhel7server1
ssh: connect to host rhel7server1 port 8022: No route to host
## configure the rules : (1) redirect port 8022 to 22 (2) deny direct access to port 22
## note these addresses are on enp0s9 which is in zone public in our example
[root@rhel7server1 ~]# firewall-cmd --permanent --add-rich-rule='rule family=ipv4 source address=192.168.0.20 forward-port port=8022 protocol=tcp to-port=22'
success
## alternative way without using rich rules :
firewall-cmd --add-forward-port=port=8022:proto=tcp:toport=22
## the same as above using firewall-config GUI
## launch firewall configuration GUI
[root@rhel7server1 ~]# firewall-config
# # Drop down "Configuration" and select Permanent
## at Zones tab, select public
## scroll the options until Port Forwarding
## click Add button
## add the port forwarding options accordingly and then click OK
## activate the new rules
[root@rhel7server1 ~]# firewall-cmd --reload
|
[root@rhel7server1 ~]# firewall-cmd --permanent --zone=public --remove-service=ssh
success
[root@rhel7server1 ~]# firewall-cmd --reload
success
## test after : direct ssh to port 22 NOT allowed, ssh to port 8022 allowed
[root@rhel7client1 ~]# ssh user1@rhel7server1
ssh: connect to host rhel7server1 port 22: No route to host
[root@rhel7client1 ~]# ssh -p 8022 user1@rhel7server1
user1@rhel7server1's password: *******
## remove the rules
[root@rhel7server1 ~]# firewall-cmd --permanent --remove-rich-rule='rule family=ipv4 source address=192.168.0.20 forward-port port=8022 protocol=tcp to-port=22'
success
### NAT - IP MASQUERADE
## see examples here
Masquerading With Firewalld
Masquerading will forward packets that are not directed to an IP address associated to the system itself onto the intended destination. The source IP address of the packets that are sent through our system will be changed to the IP address of our system, rather than the IP address of the original traffic source. Responses to these packets will then go through our system and the destination address will be modified so that the traffic will be sent back to the original host that initiated the traffic
|