I am adding a particular interface as an trusted zone using below command

firewall-cmd --zone=trusted --change-interface=eth0

Now suppose I have multiple interfaces(like eth0, eth1, bond0, bond1) and I want to add all of them once, by executing the above command written in a bash script.

What will be the syntax and command for adding all the interfaces by executing above command once?

3 Answers 3


This will get all the device names and then proceed to run the command for every interface.

for i in $( ifconfig -a | sed 's/[ \t].*//;/^\(lo\|\)$/d' ); do
    firewall-cmd --zone=trusted --change-interface=$i
    echo "Added $i to trusted\n"
  • 2
    It would be really great if you can explain the working of this 's/[ \t].*//;/^(lo\|)$/d' syntax. Thanks
    – Neeraj
    Sep 15, 2017 at 20:16
  • Lot to explain.. Look at this grymoire.com/Unix/SedChart.pdf . It will explain well what the syntax means. Sep 15, 2017 at 20:33

Assuming you are on a Linux system with the ip command (but not the busybox variant of it), we may ask it for the addr information in JSON format using ip -j addr.

From that result, we may use the JSON processor jq to extract the interface name and create shell code that we can evaluate:

eval "$(
    ip -j addr |
    jq -r 'map(
        ["firewall-cmd", "--zone=trusted", "--change-interface=" + .ifname],
        ["printf", "added %s to trusted\\n", .ifname] | @sh )[]'

On an Ubuntu virtual machine I have, this would evaluate the following commands:

'firewall-cmd' '--zone=trusted' '--change-interface=lo'
'printf' 'added %s to trusted\n' 'lo'
'firewall-cmd' '--zone=trusted' '--change-interface=enp0s3'
'printf' 'added %s to trusted\n' 'enp0s3'
'firewall-cmd' '--zone=trusted' '--change-interface=enp0s8'
'printf' 'added %s to trusted\n' 'enp0s8'
'firewall-cmd' '--zone=trusted' '--change-interface=docker0'
'printf' 'added %s to trusted\n' 'docker0'

Or, we can make a more traditional shell loop that iterates over only the interface names:

ip -j addr | jq -r 'map(.ifname)[]' |
while IFS= read -r ifn; do
    firewall-cmd --zone=trusted --change-interface="$ifn"
    printf 'added %s to trusted\n' "$ifn"

This avoids the complex and unexplained sed syntax from the previous answer

devlist=`netstat -i |cut -f1 -d' '`
for dev in ${devlist}; do
  echo "Adding $dev to trusted"
  firewall-cmd --zone=trusted --change-interface=$dev
  • 1
    Since this looks very similar to the currently accepted answer and since it does not contain any explanation whatsoever, it's unclear to me what benefits this answer has compared to the other.
    – Kusalananda
    Oct 20, 2022 at 17:14
  • This avoids the complex and unexplained sed syntax from the previous answer.
    – jonasmike
    Oct 20, 2022 at 17:26
  • 1
    If you want to explain your answer, you should add the explanation to the answer, not in comments.
    – Kusalananda
    Oct 20, 2022 at 17:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .