How to Relocate /var Directory to Another Partition in Linux: A Step-by-Step Guide


1 views

When your root partition (/dev/sda1) is running out of space (95% full in this case) while another partition (/dev/sda2) has ample available space (14GB free), moving directories like /var becomes essential. The /var directory typically contains log files, caches, and other frequently written data that can quickly consume disk space.

Before proceeding, ensure you have:

sudo mkdir /home/newvar
sudo cp -a /var/* /home/newvar/

Edit your /etc/fstab file to make the change persistent:

# Add this line at the end of /etc/fstab
/dev/sda2/home/newvar /var ext4 defaults 0 2

Follow these steps carefully:

sudo mv /var /var.old
sudo mkdir /var
sudo mount --bind /home/newvar /var

After rebooting, verify the changes:

df -h /var
ls -l /var

Once confirmed working, you can safely remove the old directory:

sudo rm -rf /var.old

For simpler cases, you might consider:

sudo mv /var /home/var
sudo ln -s /home/var /var

If services fail after migration:

sudo systemctl stop [service]
sudo systemctl start [service]
journalctl -xe

Remember that:

  • Some applications hardcode paths to /var
  • SELinux contexts must be preserved during copy
  • Always have backups before such operations

When your root partition (/) is nearly full (95% in this case) and contains critical directories like /var, it's time to consider redistributing your storage. Here's the current disk usage from your df output:

Filesystem      Size  Used Avail Use% Mounted on
/dev/sda1       5.0G  4.5G  289M  95% /
/dev/sda2        15G  406M   14G   3% /home

The /home partition (sda2) has ample space (14G free) that we can utilize for /var relocation.

1. Create a temporary mount point

sudo mkdir /mnt/newvar

2. Copy existing /var contents

Use rsync for reliable copying that preserves permissions:

sudo rsync -avz /var/ /mnt/newvar/

3. Backup original /var (critical step)

sudo mv /var /var.old
sudo mkdir /var

4. Update fstab for permanent mounting

Add this line to /etc/fstab (use nano/vim):

/dev/sda2/newvar /var ext4 defaults 0 1

For more flexibility, consider bind mounting a subdirectory of /home:

sudo mkdir /home/newvar
sudo rsync -avz /var/ /home/newvar/
sudo mv /var /var.old
sudo mkdir /var
echo "/home/newvar /var none bind 0 0" | sudo tee -a /etc/fstab

After rebooting, verify with:

df -h /var
ls -l /var

If services fail to start, check logs for missing files in /var and restore from /var.old if needed.

  • Ensure all services using /var are stopped during migration
  • Verify disk space before copying: du -sh /var
  • Consider SELinux contexts if enabled: restorecon -Rv /var

For large /var directories, perform this during maintenance windows as some services (Apache, MySQL) may need to be temporarily stopped.