Migrate FreeBSD Jails Between Hosts: Step-by-Step
In this article
Migrating a FreeBSD jail between hosts lets you move a running service or application to new hardware without rebuilding it from scratch. This guide covers copying the jail’s filesystem, updating its configuration, and starting it on the destination host. You’ll need root access on both hosts, a way to transfer files (like rsync or tar over SSH), and a basic understanding of the jail and ezjail or iocage commands, depending on which management tool you use.
What You'll Need
- Root or sudo access on both the source and destination FreeBSD hosts.
- Network connectivity between the hosts (SSH is typical).
- Enough disk space on the destination to hold the jail’s files.
- Knowledge of which jail management tool you use: classic
jail(8),ezjail, oriocage. - The destination host must have the same or newer FreeBSD version (or at least compatible userland) to avoid binary mismatch issues.
Step-by-Step
- Identify the jail’s name and path on the source host.
Runjlsto list active jails with their JID and path. Forezjail, useezjail-admin list; foriocage, useiocage list. Note the jail’s full path (e.g.,/usr/jails/myjail). - Stop the jail cleanly.
Usejail -r jailnamefor classic jails,ezjail-admin stop jailnamefor ezjail, oriocage stop jailnamefor iocage. This ensures files are consistent and no processes are writing. - Create a snapshot or archive of the jail’s filesystem.
On the source host, create a compressed tar archive of the jail directory. For example:
tar -czf /tmp/jailname.tar.gz -C /path/to/jails jailname
If you preferrsync, you can skip archiving and sync directly (see step 5). - Transfer the archive to the destination host.
Usescporrsyncover SSH:
scp /tmp/jailname.tar.gz user@desthost:/tmp/
For large jails,rsync -avz /path/to/jail user@desthost:/path/to/destination/is more efficient and resumable. - Extract the jail on the destination host.
Place the jail in the desired directory, e.g.,/usr/jails/. If you used a tar archive:
mkdir -p /usr/jails && tar -xzf /tmp/jailname.tar.gz -C /usr/jails/
Ensure the directory ownership and permissions match the original (usually root:wheel). - Adjust the jail’s configuration for the new host.
Classic jails: edit/etc/jail.confon the destination to add or update ajailnamesection with the correctpath,ip, andhost.hostname. For ezjail, edit/usr/local/etc/ezjail/jailnameand update the IP address. For iocage, useiocage set ip4_addr="interface|IP" jailname. - Update the jail’s internal configuration if necessary.
If the jail’s IP address changed, edit/etc/rc.confinside the jail (e.g.,ifconfig_lo0_alias0or service-specific settings) to reflect the new IP. Also check/etc/resolv.confif DNS settings differ. - Start the jail on the destination host.
For classic jails:service jail start jailnameorjail -c jailname. For ezjail:ezjail-admin start jailname. For iocage:iocage start jailname. - Verify the jail is running and services respond.
Runjlsto see the jail’s JID and status. Test network connectivity (e.g.,pingfrom inside the jail) and check that any services (web server, database) are up. Check logs inside the jail (/var/log/messages) for errors. - Update any external references.
If other hosts or firewalls referenced the old IP, update them. Also update DNS records if the jail’s hostname resolves to a new IP.
Common Mistakes
- Copying the jail while it is still running, leading to inconsistent files.
- Forgetting to update the jail’s IP address in both the host configuration and inside the jail’s
/etc/rc.conf. - Using a different FreeBSD version on the destination, causing binary incompatibilities (e.g., libc mismatch).
- Overlooking the jail’s
mountpoints—if the jail uses nullfs mounts from the host, those need to be recreated on the destination. - Not preserving file ownership when using
taras a non-root user.
Tips
- Use
rsyncwith--numeric-idsand-ato preserve ownership and permissions accurately. - Before migrating, test the jail on the destination with a temporary IP to ensure everything works before switching production traffic.
- If you use ZFS, consider sending a ZFS snapshot to the destination with
zfs send | zfs recvfor faster, atomic migration. - Keep a backup of the original jail until the new one has been stable for a few days.
- Document the jail’s configuration (IP, mount points, services) before migration to ease troubleshooting.
Troubleshooting
- Jail fails to start with “jail: jailname: failed” — Check the jail configuration syntax with
jail -f /etc/jail.conf -c jailnamefor verbose errors. Common issues: wrong path, missing IP address, or missingexec.startscript. - Network unreachable inside the jail — Verify the IP address is correctly assigned to an interface on the host. Use
ifconfigon the host to see if the alias is set. Also check the jail’s/etc/rc.conffor interface aliases. - Services inside the jail don’t start — Check the jail’s
/var/log/messagesand/var/log/daemon.log. Ensure the service is enabled in/etc/rc.confinside the jail and that the jail’sexec.startscript runs/etc/rc. - Permission denied when accessing jail files — Ensure the jail directory is owned by root:wheel and has proper permissions (usually 755). If using ZFS, check dataset permissions.
- Jail starts but cannot resolve DNS — Copy the correct
/etc/resolv.conffrom the host or set it manually inside the jail.
Frequently Asked Questions
Can I migrate a jail without stopping it?
Technically you can copy the files while running, but it’s risky. Files may change during the copy, leading to corruption. Always stop the jail for a consistent snapshot.
Do I need to recompile software inside the jail after migration?
If the destination host runs the same FreeBSD version and architecture, no. If it’s a different minor version, it may still work, but if you see “Fatal error: __getreent” or similar, reinstall packages or rebuild from ports.
What if my jail uses a custom kernel module?
Kernel modules must be loaded on the destination host before starting the jail. Add them to the host’s /boot/loader.conf or load them manually.
How long does migration take?
It depends on the jail size and network speed. A small jail (a few hundred MB) can take minutes; a large one (tens of GB) may take hours over a slow link.
Can I move a jail to a different IP subnet?
Yes, but you must update the jail’s IP configuration and any services that bind to specific IPs. Also update firewall rules and DNS records.
What if the destination host already has a jail with the same name?
You must either remove the existing jail or rename the incoming one. Changing the jail name in the configuration is straightforward, but be aware that some services inside the jail may rely on the hostname.