# Update multiple jails together



## trumee (Jan 23, 2016)

Hi,

I have a bunch of jails running. Each time they need an update, I have to login in them individually and run `pkg update; pkg upgrade`

Is there a way to run the `pkg update; pkg upgrade` in one go on all the jails from the host?

Thanks


----------



## ondra_knezour (Jan 23, 2016)

You can enumerate through the list of jails from jls(8) and call the pkg(8) utility with a -j option to execute it in the given jail(s).


----------



## hukadan (Jan 24, 2016)

If you follow the ondra_knezour advice, it should give you something like the following :

```
#! /bin/sh
jail_ids=$(jls -h jid | grep -v jid)
echo "Starting upgrading jails..."
for x in $jail_ids ; do
  echo "jail id : $x"
  pkg -j $x upgrade
done
```


----------



## trumee (Jan 24, 2016)

This code works fine. The only issue I have is that some of my jails use different gateways so I need to specify the fib number like this:
`setfib 1 pkg -j $x upgrade`

Is there anyway to find out what fib is a particular jail is using except extracting it from the config file in /usr/local/etc/ezjail/ ?


----------



## trumee (Jan 24, 2016)

Ultima on irc came up with the following script. The fib numbers should match the jails.


```
#!/bin/sh
jails="jail1 jail2 jail3"
fib="0 1 2"
dollarsign="$"
pos="1"
for j in ${jails}; do
   f=`echo $fib | awk "{ print $dollarsign$pos }"`
   setfib $f jexec $j pkg upgrade -y
   setfib $f jexec $j pkg autoremove -y
   setfib $f jexec $j pkg clean -y
   pos=$(( $pos + 1 ))
done
```


----------

