Managing Temporary Files In Linux – systemd-tmpfiles

In this lesson, you will learn how to manage temporary files in Linux using systemd-tmpfiles. You will learn how to create or configure temporary files with tmpfiles rules.

More so, you will learn Which systemd unit regularly triggers the cleanup of the temporary files and how to automatically clean unused temporary files in Linux.


In one of our previous lessons on Linux file system structure, we learnt about temporary files such as /tmp and /var/tmp. We understood that temporary files can be stored in “/tmp” and “/var/tmp”.

We also understood that some temporary files are stored in the memory and are not persistent across reboot. We understood that the /run directory is basically for runtime data and the files in this directory will not be persistent when the server is restarted.

Now, the question is; have you thought about how these temporary files, many times, don’t get to fill up these directories? have you thought about how these temporary files are managed?

Well, the tool responsible for managing these temporary files is “systemd-tmpfiles“. Interesting, right?

Going forward, in one of our previous lessons, we learnt about systemd, being the first service that is started after the system boots up and it has the process ID of 1. We also learnt that systemd uses unit files for its management.

Now, it’s high time you know that one of the first systemd service units that is started when the server boots up is this same guy, “systemd-tmpfiles“. This service automatically runs the command (systemd-tmpfiles – -create – -remove).

This command means that systemd should look into “/etc/tmpfiles.d/*.conf“, “/run/tmpfiles.d/*.conf“, and “/usr/lib/tmpfiles.d/*.conf“, then creates or removes the files as stated, required or configured in the configuration files.

Administrators or users can edit or create their configuration files in these directories. We will see how to do that as we proceed.

This same guy, “systemd-tmpfiles” uses systemd timer unit (systemd-tmpfiles-clean.timer) to ensure that the temporary files are well managed and rotated away from the directories as specified in the timer unit file.

Let’s have a look at the “systemd-tmpfiles-clean.timer” unit file.

[root@DRDEV1 ~]# cat /usr/lib/systemd/system/systemd-tmpfiles-clean.timer

From the configuration file above where the arrows are pointed to, OnBootSec=15min means that “systemd-tmpfiles-clean.service” unit will be triggered after 15 minutes when the system has booted up,

Also, OnUnitActiveSec=1d means that there should be another trigger after 24hours when the unit was last triggered.

If you wish, you can change the values of these parameters to suit what may be required in your environment? Yes, you can.

To do that, put in the required value, reload systemd by using the command, “systemctl daemon-reload”, and enable systemd-tmpfiles-clean.timer unit by using the command, “systemctl enable –now systemd-tmpfiles-clean.timer”.

Many times in a large environment, you may need to manually manage these temporary directories and files without editing “systemd-tmpfiles-clean.timer” unit file.

You may also even need to manually create and manage other temporary directories different from /tmp and /var/tmp to store temporary files.

A very good reason why you may need to manage these directories is if you have many users, which of course, the users are expected to store their temporary files in these locations and you wouldn’t want these files filling up the disks.

Another possible reason is that many applications are also configured to store temporary files in these directories or a customized directory.

If an application is expected to use these directories and there is not enough space or the directories don’t even exist, the application may begin to malfunction, hence the need for temporary files to be managed.

So, how can these temporary files be manually managed?

well, it can be manually managed by editing or creating configuration files in “/etc/tmpfiles.d/*.conf“, “/run/tmpfiles.d/*.conf“, or “/usr/lib/tmpfiles.d/*.conf” directories.

Let’s see how these directories work by checking the man page of tmpfiles.d

[root@DRDEV1 ~]# man tmpfiles.d 5
TMPFILES.D(5)                               tmpfiles.d                               TMPFILES.D(5)

       tmpfiles.d - Configuration for creation, deletion and cleaning of volatile and temporary

temporary files in Linux

You should know that these directories, the highlighted ones in yellow take precedence over one another. Configuration files defined in /etc/tmpfiles.d/*.conf takes higher precedence over the others, followed by /run/tmpfiles.d/*.conf.

Hence, whatever configuration defined in /etc/tmpfiles.d/*.conf directory override the others.

This is just similar to what we learnt about systemd unit files in one of our previous lessons when we mentioned that it is recommended that users define their configuration files in /etc/systemd/system instead of /usr/lib/systemd/system because config files defined in/usr/lib/systemd/system may be overridden.

The “/usr/lib/tmpfiles.d/*.conf” is a system defined location. You will see all the files when you change directory to this location, while the “/etc/tmpfiles.d/*.conf” location is a user defined location for managing temporary files.

Hence why it is recommended that users define their configuration files in /etc/tmpfiles.d/*.conf

Let me reiterate again, make your changes in /etc/tmpfiles.d/*.conf. More so, changes made here will be persistent across reboot.

so, how do users define their configuration files in /etc/tmpfiles.d/*.conf location?

Let’s look at the tmpfiles.d man page again.

[root@DRDEV1 ~]# man tmpfiles.d 5


From the man page, you can see that users define their configuration files in the file format below

 #Type Path        Mode UID  GID  Age Argument
           d     /run/user   0755 root root 10d -
           L     /tmp/foobar -    -    -    -   /dev/null

The configuration file must basically contain 7 columns which are Type, Path, Mode, UID, GID, Age, and Argument, and an example of a configuration file can be

 d     /run/user   0755 root root 10d -


L     /tmp/foobar -    -    -    -   /dev/null

The type consists of a single letter and optionally an exclamation mark. Type is the action that systemd-tmpfiles will take. From the man page, you can see the different Types such as f, F, w, d, D, e, v, q, Q, p+, etc.

The f Type means that systemd-tmpfiles should create a file if it doesn’t exist yet. Also, the d type means that systemd-tmpfiles should create a directory, and the mode and ownership will be adjusted if specified and the directory already exists.

The man page is very useful to explain the functionalities of each Types.

More so, path is the path that systemd-tmpfiles will store the temporary files. The path must be absolute.

Mode is the permission the temporary files or directories should have.

UID is the owner of the temporary files/directories

GID is the group owner of the temporary files/directories

Age, which is the date field is used to decide what files to delete when cleaning and to clean up the files or directories that are associated with the time specified in this filed respectively.

Argument, for L lines determines the destination path of the symlink.

To know more about these fields, parameters, and how they are used, see the “tmpfiles.d 5” man page.

More so, if you will be writing a Linux exam, use this man page as a reference to be able to correctly answer your exam questions

Having understood how temporary files can be managed in Linux, let’s see the step by step guide of how to create and manage temporary files in Linux.


Step By Step Guide Of How To Create and Manage Temporary Files With Examples in Linux

using systemd-tmpfiles to configure /tmp directory not to contain files that have not been used in the last three days, take the following processes.

1. create a systemd-tmpfiles configuration file in /etc/tmpfiles.d/ directory

N:B: Make sure the file name has a “.conf” extension

[root@DRDEV1 ~]# vi /etc/tmpfiles.d/tekneed.conf
q /tmp 1777 root root 3d


You can make life easy by copying the /usr/lib/tmpfiles.d/tmp.conf to /etc/tmpfiles.d/tekneed.conf and edit the necessary files as needed.

[root@DRDEV1 ~]# cp /usr/lib/tmpfiles.d/tmp.conf /etc/tmpfiles.d/tekneed.conf
q /tmp 1777 root root 3d

The q Type is similar to the d Type. This will simply tell systemd to create /tmp directory if it does not exist, and the permission will be set to 1777. The user and group owner of /tmp file will be root, and every file that has been idle for the last 3 days will be deleted.

You should also know that the d Type can also be used depending on what you want to achieve.

2. Verify that the configuration file is correct.

[root@DRDEV1 ~]# systemd-tmpfiles --clean /etc/tmpfiles.d/tekneed.conf

If you get an error, then the config file is wrong, if you don’t get an error, the config file is right.

Tutorial Video On How To Create & Manage Temporary Files In Linux

RHCSA 8 Exam Practice Question & Answer On How To Create & Manage Temporary Files in Linux

Your feedback is welcomed. If you love others, you will share with others

Be the first to comment

Leave a Reply

Your email address will not be published.