I am attempting to clear out all existing services configured in firewalld
via a bash script.
# produces {cockpit,dhcpv6-client,ssh} as an example
local EXISTING_SERVICES="{$(firewall-cmd --permanent --list-service | sed -e 's/ /,/g')}"
# firewall-cmd --permanent --remove-service={cockpit,dhcpv6-client,ssh}
firewall-cmd --permanent --remove-service="${EXISTING_SERVICES}"
When this is run, firewall-cmd
returns:
Warning: NOT_ENABLED: {cockpit,dhcpv6-client,ssh}
success
The problem seems to be firewall-cmd
interprets the list of services to disable as a single service name, instead of a list. When I run the command manually from the shell, the same exact (copy/pasted) command works like expected.
Example script to replicate:
EXISTING_SERVICES="{$(firewall-cmd --permanent --list-service | sed -e 's/ /,/g')}"
echo "firewall-cmd --permanent --remove-service=${EXISTING_SERVICES}"
firewall-cmd --permanent --remove-service="${EXISTING_SERVICES}"
Results:
What is the difference between running this via script and via direct shell commands?
Update: Tried running the script with set -x
as suggested by @fra-san, which produced the following results when run from the script:
And the following results when run from the shell:
It seems the shell (and/or firewalld) behaves differently when run interactively and expands the list of services into 3 separate --remove-service=
flags. This is very unexpected behavior.
firewall-cmd
commands you are running are not equivalent to each other; usingset -x
to look at what is actually executed will likely be revealing. By the way,firewall-cmd --permanent --remove-service={cockpit,dhcpv6-client,ssh}
(the commented line) would work fine in your script, right? – fra-san May 12 '21 at 20:29set -x
is likely to be revealing if you use it when running the command interactively (the one that works). As you have found out, it doesn't tell much about what happens in your script. – fra-san May 12 '21 at 20:48