How to Stop Ubuntu from Automatically Emptying the /tmp Directory on Boot

In Ubuntu and most other modern Linux distributions, the /tmp directory is used by applications to store temporary files. By default, the system automatically clears the entire contents of this directory every time the computer reboots. This is generally a good practice, as it prevents temporary files from endlessly accumulating and wasting disk space.

However, if you are developing an application, running long mathematical simulations, or managing specific server workloads that require temporary files to persist across a reboot, you will need to disable this automatic clearing behavior.

On modern Ubuntu systems (which use systemd), the /tmp directory is typically managed by a service called systemd-tmpfiles.

How to Prevent /tmp from Clearing on Reboot

To stop systemd from emptying the /tmp directory, you need to override the default configuration for temporary files.

  1. Open your terminal application.
  2. The default configuration is stored in /usr/lib/tmpfiles.d/tmp.conf. Do not edit this file directly, as your changes will be overwritten during the next system update. Instead, you must copy it to the local administrator configuration directory:

sudo cp /usr/lib/tmpfiles.d/tmp.conf /etc/tmpfiles.d/

  1. Now, open your new local copy in a text editor (such as nano):

sudo nano /etc/tmpfiles.d/tmp.conf

  1. Look for the line that configures the /tmp directory. It usually looks like this:

q /tmp 1777 root root 10d (or similar, sometimes starting with a D or v).

  1. To prevent systemd from deleting files in /tmp during boot or during its daily cleanup timer, you must change the first letter of that line to a lowercase x (which tells systemd to “ignore” the path for cleanup). Change the line so it looks like this:

x /tmp 1777 root root -

  1. Save the file and exit the text editor (in nano, press Ctrl+O, Enter, then Ctrl+X).

From now on, any files you place in the /tmp directory will survive a system reboot. Be aware that because the system is no longer cleaning this folder, you will eventually need to monitor it and delete old files manually to prevent your hard drive from filling up.

Get the best tech tips delivered straight to your inbox.

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