Firewalld Command - Useful firewall-cmd Examples (RHEL based)
Image Source: techmint
Useful firewall-cmd Examples
(Source: thegreekdiary.com)
1. List all zones Use the following command to list information for all zones. Only partial output is displayed.
firewall-cmd --list-all-zonesOutput:work
target: default
icmp-block-inversion: no
interfaces:
sources:
services: dhcpv6-client ssh
ports:
protocols:
masquerade: no
forward-ports:
sourceports:
icmp-blocks:
rich rules:
drop
target: DROP
icmp-block-inversion: no
interfaces:
sources:
services:
ports:
protocols:
masquerade: no forward-ports:
sourceports:
icmp-blocks:
rich rules:
.....
Public is the default zone set, if you do not change it. To check the currently set default zone use the below command:
firewall-cmd --get-default-zonepublic2. List allowed service and ports on the system To show currently allowed service on your system use the below command.
firewall-cmd --list-servicesdhcpv6-client ssh To list the ports that are open on your system:
You would normally see no ports listed here when you have just enabled the firewalld.
firewall-cmd --list-ports3. To Enable all the incoming ports for a service You can also open the required ports for a service by using the –add-seervice option.
To permit access by HTTP clients for the public zone:
success To list services that are allowed for the public zone:
To permit access by HTTP clients for the public zone:
firewall-cmd --zone=public --add-service=httpfirewall-cmd --zone=work --list-servicesdhcpv6-client http ssh Using this command only changes the Runtime configuration and does not update the configuration files. The following sequence of commands shows that configuration changes made in Runtime configuration mode are lost when the firewalld service is restarted:
To make changes permanent, use the –permanent option.
Example:
Changes made in Permanent configuration mode are not implemented immediately.
Example:
systemctl restart firewalldfirewall-cmd --zone=work --list-servicesdhcpv6-client sshExample:
firewall-cmd --permanent --zone=public --add-service=httpsuccessExample:
firewall-cmd --zone=work --list-servicesdhcpv6-client ssh However, changes made in a Permanent configuration are written to configuration files.
Restarting the firewalld service reads the configuration files and implements the changes.
Example:
dhcpv6-client http ssh
Example:
systemctl restart firewalldfirewall-cmd --zone=work --list-services4. Allow traffic on an incoming port The command below will open the port 2222 effective immediately, but will not persist across reboots:
For example, to open TCP port 2222 :
The following command will create a persistent rule, but will not be put into effect immediately:
For Example, to open TCP port 2222 :
To list the open ports, use the command :
firewall-cmd --add-port=[YOUR PORT]/tcpfirewall-cmd --add-port=2222/tcpfirewall-cmd --permanent --add-port=[YOUR PORT]/tcpfirewall-cmd --permanent --add-port=2222/tcpfirewall-cmd –-list-ports2222/tcp5. Start and stop firewalld service To start/stop/status firewalld service use the below commands:
To check the status of the firewalld service:
systemctl start firewalld.servicesystemctl stop firewalld.servicesystemctl status firewalld.service