Skip to content

Firewall Management - FirewallD

The FirewallD is the standard firewall management tool for Linux operating systems in distributions such as Fedora, Red Hat and CentoOS. It acts as a front-end for the Linux kernel packet filtering framework, known as netfilter.

This firewall has some default rules and works with the concept of zones where the allowance of services is done within them.

The table below shows how the network firewall is configured after the operating system installation:

RegraComportamento
INPUTLiberado o acesso conexões do tipo RELATED,ESTABLISHED.
FORWARDAceita apenas conexões do tipo RELATED,ESTABLISHED.
OUTPUTNão possui restrições.

firewalld manages a set of rules known as zones. Zones define the type of traffic that will be allowed based on the trust level of the network to which your server is connected. Each zone is attached to an existing network interface on the server.

The command below lists the existing zones:

Terminal window
firewall-cmd --get-zones

Below are the zones available in firewalld shown in order of trust level:

ZonaDescrição
dropTodos os pacotes são descartados.
blockTodos os pacotes são rejeitados.
publicRede que você não conhece, pública.
externalRede externa onde o servidor com o firewalld funciona como um
gatewaypara a rede interna. É configurada com mascaramento para manter a privacidade da rede interna.
internalÉ a parte interna da rede. Equipamentos nessa rede possuem um nível maior de confiança e serviços adicionais estão disponíveis.
dmzSão equipamentos isolados, ou seja, que não devem possuir acesso a sua rede. Apenas algumas conexões de entrada para esses equipamentos são permitidas.
workEquipamentos de trabalho com liberação de serviços adicionais.
homeEquipamentos de casa. São dispositivos mais conhecidos e
confiáveis e que possuem liberação para um pouco mais de serviços que a zona work.
trustedEquipamentos de confiança. Praticamente todos os serviços estão disponíveis para os equipamentos nesta zona.

The command below lists all existing rules in the firewalld service:

Terminal window
firewall-cmd --list-all

If you want to list only the rules of a specific zone use the –zone option:

Terminal window
firewall-cmd –zone=public --list-all

To modify the firewall incoming rules on Fedora, we use the firewall-cmd command.

The example below shows how to open ports 80(TCP) and 443(TCP) for access from the public network, permanently, for an HTTP server via the command line:

Terminal window
firewall-cmd --permanent --zone=public --add-port=80/tcp
firewall-cmd --permanent --zone=public --add-port=443/tcp
firewall-cmd --set-default-zone=public
firewall-cmd --reload

where:

ParâmetroDescrição
--permanentAdds the rule permanently, that is, after restarting the filter the rules will remain. If this option is omitted the rules are valid until firewalld is restarted.
--zone=publicIt is the untrusted public zone. These are addresses you do not know but may be authorized case by case.
--add-port=80/tcpInformation of the port and protocol that will be added to the public zone.
--reloadReloads the rules keeping the connection states.
--set-default-zone=publicSets the public zone as the default to be used.

The example below shows how to open the SSH port for the Linux server:

Terminal window
firewall-cmd --permanent --zone=public --add-port=22/tcp
firewall-cmd --set-default-zone=public
firewall-cmd --reload

Below is shown how to allow full access to the server for the network whose source is 192.168.1.0/24:

Terminal window
firewall-cmd --permanent --zone=public --add-source=127.0.0.1/8
firewall-cmd --reload
ParâmetroDescrição
--permanentAdds the rule permanently, that is, after restarting the filter the rules will remain. If this option is omitted the rules are valid until firewalld is restarted.
--zone=publicIt is the untrusted public zone. These are addresses you do not know but may be authorized case by case.
--add-source=192.168.1.0/24Information of the network or host that will be added to the public zone.
--reloadReloads the rules keeping the connection states.

For this function it is necessary to have at least 2 network interfaces on the server, one that connects to the public network and another to the internal network.

In the example below, interface eth0 is connected to the public network and eth1 to the internal network:

Terminal window
firewall-cmd --permanent –zone=internal –add-interface=eth1
firewall-cmd –permanent –zone=public -add-masquerade
firewall-cmd --reload
ParâmetroDescrição
--permanentAdds the rule permanently, that is, after restarting the filter the rules will remain. If this option is
omitted this option the rules are valid until firewalld is restarted.
--zone=public
--zone=internal
We select the public zone to perform masquerading and the internal to indicate the internal network.
--add-masqueradeAdds masquerading to the selected zone.
--reloadReloads the rules keeping the connection states.

To forward ports from the external network to an address on the internal network, use the commands below:

Terminal window
firewall-cmd --permanent --zone=public –add-forward-port=port=443:proto=tcp:toport=443:toaddr=192.168.1.11
firewall-cmd --reload
ParâmetroDescrição
--permanentAdds the rule permanently, that is, after restarting the filter the rules will remain. If this option is omitted the rules are valid until firewalld is restarted.
--zone=publicIt is the untrusted public zone. These are addresses you do not know but may be authorized case by case.
--add-forward-port=Enables the rule for port forward.
port=443Source port.
proto=tcpSource protocol.
toport=443Destination port.
toaddr=192.168.1.11Destination IP on the internal network.
--reloadReloads the rules keeping the connection states.