How to add/remove ports without starting firewalld in RHEL?
Environment
- Red Hat Enterprise Linux 7
- Red Hat Enterprise Linux 8
Issue
- Is there a way to add or remove ports from the
firewalldconfiguration whilst thefirewallis disabled? - I don't want to start
firewalldfor adding or removing ports as it will cause an outage.
Resolution
-
firewall-offline-cmdis an offline command line client of thefirewallddaemon. It should be used only if thefirewalldservice is not running. -
A port can be added or removed via
firewall-offline-cmdin casefirewalldis not active. -
For adding a port:
# firewall-offline-cmd --port=XXXX:tcp
- For removing a port:
# firewall-offline-cmd --remove-port=XXXX/tcp
-
If a zone is not defined, the port will be added to the
default zone. -
To add a port into a specific zone:
# firewall-offline-cmd --zone=<zone-name> --add-port=XXXX/tcp
- To remove a port from a specific zone:
# firewall-offline-cmd --zone=<zone-name> --remove-port=XXXX/tcp
- Since the port gets added in
<zone-name>.xmlfile, the port will remain added even after we enable or reloadfirewalldservice.
NOTE: Replace XXXX with the actual port numbers.
- Refer to the
man pagefor more information onfirewall-offline-cmd.
$ man firewall-offline-cmd
Diagnostic Steps
- The status of
firewalldis inactive(dead).
[root@rhel7 ~]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
Active: inactive (dead)
Docs: man:firewalld(1)
- We now add the port
1234/tcp.
[root@rhel7 ~]# firewall-offline-cmd --port=1234:tcp
Adding port '1234/tcp' to default zone.
success
- The port is listed under the
default public zone.
[root@rhel7 ~]# firewall-offline-cmd --list-all
public
target: default
icmp-block-inversion: no
interfaces:
sources:
services: ssh dhcpv6-client
ports: 1234/tcp
protocols:
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:
- We double-check the status of
firewalld.
[root@rhel7 ~]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
Active: inactive (dead)
Docs: man:firewalld(1)
- To remove the port, we use
--remove-portoption.
[root@rhel7 ~]# firewall-offline-cmd --remove-port=1234/tcp
success
- The port is no longer listed under the
public zone.
[root@rhel7 ~]# firewall-offline-cmd --list-all
public
target: default
icmp-block-inversion: no
interfaces:
sources:
services: ssh dhcpv6-client
ports:
protocols:
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:
- We add port into a specific zone.
[root@rhel7 ~]# firewall-offline-cmd --zone=trusted --add-port=1234/tcp
success
- The port is listed under the
trusted zone.
[root@rhel7 ~]# firewall-offline-cmd --zone=trusted --list-all
trusted
target: ACCEPT
icmp-block-inversion: no
interfaces:
sources:
services:
ports: 1234/tcp
protocols:
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:
- We now remove the port from the
trusted zone.
[root@rhel7 ~]# firewall-offline-cmd --zone=trusted --remove-port=1234/tcp
success
- The port is no longer listed under the
trusted zone.
[root@rhel7 ~]# firewall-offline-cmd --zone=trusted --list-all
trusted
target: ACCEPT
icmp-block-inversion: no
interfaces:
sources:
services:
ports:
protocols:
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:
This solution is part of Red Hat’s fast-track publication program, providing a huge library of solutions that Red Hat engineers have created while supporting our customers. To give you the knowledge you need the instant it becomes available, these articles may be presented in a raw and unedited form.
Comments