How to Synchronize Files Between Linux Servers Using the Unison Utility

Why Unison Instead of Rsync?

The rsync utility is the undisputed king of pushing data from a source to a destination. However, rsync is strictly unidirectional. If you have two web servers running in an active/active cluster, and users might upload a profile picture to Server A or Server B, rsync will overwrite changes on the destination. Unison is a bidirectional file synchronization tool. It analyzes both servers, determines which files were modified on either side, and seamlessly merges the directories so both servers have the exact same updated files without data loss.

Step 1: Install Unison on Both Servers

Unison requires the exact same version of the software to be installed on both the primary and secondary servers. Open a terminal on both machines and install the package:

On Debian/Ubuntu:

sudo apt update && sudo apt install unison -y

On RHEL/CentOS (requires EPEL):

sudo yum install epel-release -y

sudo yum install unison -y

Step 2: Setup Passwordless SSH Keys

Unison synchronizes data over standard SSH. To automate the process, the primary server must be able to securely connect to the secondary server without prompting for a password.

On the primary server, generate an SSH key pair (if you haven’t already):

ssh-keygen -t ed25519 (Press Enter for no passphrase)

Copy the public key to the secondary server:

ssh-copy-id [email protected]

Verify you can SSH into the secondary server without being prompted for a password.

Step 3: Create a Unison Profile

Instead of typing long commands, Unison uses configuration profiles (.prf files). On the primary server, create a hidden Unison directory in your user’s home folder, and create a profile for your web directory:

mkdir ~/.unison

nano ~/.unison/web-sync.prf

Add the following configuration:


# The local root directory
root = /var/www/html/uploads/

# The remote root directory over SSH
root = ssh://[email protected]//var/www/html/uploads/

# Run silently without interactive prompts
batch = true

# Automatically resolve conflicts in favor of the most recently modified file
prefer = newer

# Maintain original permissions and ownership
owner = true
group = true
perms = -1

Step 4: Run the Synchronization

You can execute the synchronization manually by simply calling Unison and providing the name of the profile you just created (omitting the .prf extension):

unison web-sync

Unison will hash the files in both directories, compare the states, and bi-directionally transfer any missing or updated files.

Step 5: Automate with Cron

To ensure your web cluster remains synchronized automatically, you can add Unison to the primary server’s crontab. Open the cron editor:

crontab -e

Add the following line to silently run the Unison sync every 5 minutes:

*/5 * * * * /usr/bin/unison web-sync > /dev/null 2>&1

Your two servers will now constantly compare notes and ensure both web directories remain perfectly in sync, regardless of which server a user uploads their files to.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.