1. Introduction

The /tmp directory holds temporary files in Linux. Many programs use this directory for interim data. However, depending on the distribution in use, the system may remove temporary files at certain times. In addition, /tmp is often situated on a separate partition.

In this tutorial, we’ll follow a step-by-step process to safely move /tmp to another partition in Linux.

2. The /tmp Directory

Naturally, when we install any Linux system, it creates the /tmp directory under the root partition /. Linux distributions handle the files in /tmp differently:

  • RHEL runs automated cron jobs that remove any files older than n hours as defined in /usr/lib/tmpfiles.d/tmp.conf
  • Ubuntu removes the contents of /tmp with every reboot

Modern Linux versions that use a virtual filesystem utilize the /tmp directory only as a mount point for RAM with the tmpfs filesystem. Further, the default permissions for /tmp (1777 or rwxrwxrwt) permit all users to read and write to it. Thus, it’s a convenient place for programs and processes to store data without worrying about access limitations.

Having seen the role of the /tmp directory, let’s see how we can move it to a different partition.

3. Moving the /tmp Directory

It’s good practice to create a new partition when moving the /tmp directory. This eliminates the risk of losing important data on an already existing one.

Now, the procedure to move the /tmp directory to another location on the drive starts with creating that new partition. Then, we format it with a filesystem and mount it.

3.1. Create a New Partition

Notably, we can create a new partition with the fdisk utility. Also, we can use the newer and arguably better-equipped parted tool. Alternatively, on an LVM system, we can create a new logical volume using the lvm2 utility.

In any case, there are many factors to partitioning for a new /tmp:

  • the current layout may affect where we locate the partition
  • our size requirements may be different depending on the use case
  • the filesystem type is related to several use-case details such as the number and the average size of files

Because of this, we can’t really see a universal example. Importantly, we only create and format the partition with a filesystem but don’t mount it yet.

Let’s now proceed with the contents of our current /tmp directory.

3.2. Back up the /tmp Directory

Before we move /tmp to a new partition, we need to back up its content to another location:

$ sudo mkdir /tmp_bak

After creating a separate directory, let’s create a backup there using the rsync utility:

$ rsync -avz /tmp/ /tmp_bak
sending incremental file list
./
.X0-lock
.X1-lock
.X1024-lock
...
.font-unix/
snap-private-tmp/
snap-private-tmp/snap.snapd-desktop-integration/
...
snap-private-tmp/snap.snapd-desktop-integration/tmp/.snap/usr/lib/
systemd-private-985cf150d5724ca98540c0e0facf2926-upower.service-lq4VZY/tmp/
tracker-extract-3-files.1000/
tracker-extract-3-files.127/

sent 2,820 bytes  received 269 bytes  6,178.00 bytes/sec
total size is 44  speedup is 0.01

To understand, this command recursively transfers all files from /tmp to /tmp_bak. Also, it moves the files in archive mode, which preserves most properties during the transfer:

  • symbolic links
  • devices
  • attributes
  • permissions
  • ownerships

The trailing slash on the source changes the command behavior to avoid creating an additional directory at the destination. Hence, it copies only the content into /tmp_bak instead of copying the source by name, which would result in /tmp_bak/tmp/. In all cases, the destination retains the attributes of the source.

4. Removing the Old /tmp Entry

Now that we’ve backed up /tmp, let’s move it to a new partition. First, let’s unmount and delete any existing /tmp directory:

$ sudo umount /tmp
$ sudo rm -r /tmp

Here, the -r switch removes the directory and all of its content recursively. However, if the system hasn’t previously used the /tmp directory as a mount point, there’s no need to unmount before removing the directory.

Next, let’s create a new /tmp directory to serve as the mount point of the new partition:

$ sudo mkdir /tmp

At this point, we can mount the new partition at /tmp:

$ sudo mount /dev/PARTITION /tmp

Finally, let’s copy all the contents from the backup directory to the newly mounted partition:

$ sudo rsync -avz /tmp_bak/ /tmp
sending incremental file list
...

sent 2,640 bytes  received 269 bytes  5,818.00 bytes/sec
total size is 44  speedup is 0.02

Now we’ve successfully moved the /tmp directory to another partition. However, to make the change permanent, we need to add or modify an entry in the /etc/fstab file.

5. Making the Changes Persistent

After making the necessary changes to the filesystem, let’s make them persist across reboots using the /etc/fstab file. First, we need to find the UUID (Universal Unique Identifier) of the new partition with the blkid command:

$ sudo blkid -o value -s UUID /dev/sdb1
46898bfd-85c5-47f4-ae1e-72a69a4b2945

Next, using any editor, we should create or modify a /tmp entry in the fstab file:

<file system>                              <mount point>  <type>  <options>   <dump>  <pass>
UUID=46898bfd-85c5-47f4-ae1e-72a69a4b2945  /tmp           ext4    defaults     0       0    

Here, we specify several actions:

  • UUID comes from the blkid command we ran earlier
  • /tmp stands for the mount point
  • ext4 is the filesystem type of the new partition
  • the rest are default options for the new partition

Notably, we should replace the UUID with the actual UUID from the blkid command for our partition.

6. Setting the Permissions

As mentioned earlier, all programs and processes should be able to read from and write to the /tmp directory. To allow this, we need to set the right permissions. The default permission mode for /tmp is 1777. Also, this setting should set the sticky bit on the directory.

Let’s now set the proper permissions for /tmp:

$ sudo chmod 1777 /tmp

We’re not done yet! One last step is to set the user and group ownership over the directory to root. We can do this using the chown command:

$ sudo chown root:root /tmp

Now, let’s take a look at the directory to confirm our changes:

$ sudo ls -ld /tmp
drwxrwxrwt 2 root root 40 Feb  3 22:22 tmp/

The -d flag of the command enables us to see information about the directory and not the content. All our changes are in place. Notably, the t in the permissions shows that the sticky bit is set.

7. Conclusion

In this article, we learned how to safely move our temporary directory to a different partition. First, we started with a brief summary of the purpose of /tmp. Next, we set up a directory, and created and mounted a new partition at its path to act as the new temporary storage.

Then, we moved our old directory and made the changes permanent. Lastly, we set the right permissions to make the /tmp directory useful.

Comments are closed on this article!