Bookmark this page

Practice: Managing Temporary Files

In this lab, you will configure your system to purge files older than 5 days from /tmp. You will also add a new temporary directory called /run/gallifrey to be automatically created, with files which have been unused for more than 30 seconds being automatically purged.

Resources
Files:
  • /etc/tmpfiles.d/

  • /usr/lib/tmpfiles.d/tmp.conf

Machines: serverX

Outcomes:

A new temporary directory called /run/gallifrey, set up for automatic purging, and a modified purging configuration for /tmp.

Reset your serverX system.

In production, you have run into a number of issues:

  • /tmp is running out of disk space. It seems that allowing files to be unused for 10 days before they are deleted is too long for your site. You have determined that deleting files after five days of disuse is acceptable.

  • Your time-travel research daemon gallifrey needs a separate temporary directory called /run/gallifrey. Files in this directory should be purged automatically after they have been unused for 30 seconds. Only root should have read and write access to /run/gallifrey.

  1. /tmp is under systemd-tmpfiles control. To override the upstream settings, copy /usr/lib/tmpfiles.d/tmp.conf to /etc/tmpfiles.d/.

    1. [student@serverX ~]$ sudo cp /usr/lib/tmpfiles.d/tmp.conf /etc/tmpfiles.d/
  2. Find the line in /etc/tmpfiles.d/tmp.conf that controls the purging interval for /tmp, and change the interval from 10d to 5d.

    1. Open /etc/tmpfiles.d/tmp.conf in an editor and make the change, or:

      [student@serverX ~]$ sudo sed -i '/^d .tmp /s/10d/5d/' /etc/tmpfiles.d/tmp.conf 
  3. Test if systemd-tmpfiles --clean accepts the new configuration.

    [student@serverX ~]$ sudo systemd-tmpfiles --clean tmp.conf
  4. Create a new configuration file /etc/tmpfiles.d/gallifrey.conf with the following content:

    # Set up /run/gallifrey, owned by root with 0700 permissions
    # Files not used for 30 seconds will be automatically deleted
    d /run/gallifrey 0700 root root 30s
  5. Test your new configuration for creating /run/gallifrey.

    1. [student@serverX ~]$ sudo systemd-tmpfiles --create gallifrey.conf
    2. [student@serverX ~]$ ls -ld /run/gallifrey
      drwx------. 2 root root Feb 19  10:29 /run/gallifrey
  6. Test the purging of your /run/gallifrey directory.

    1. Create a new file under /run/gallifrey.

      [student@serverX ~]$ sudo touch /run/gallifrey/companion
    2. Wait for at least 30 seconds.

      [student@serverX ~]$ sleep 30s
    3. Have systemd-tmpfiles clean the /run/gallifrey directory.

      [student@serverX ~]$ sudo systemd-tmpfiles --clean gallifrey.conf
    4. Inspect the contents of /run/gallifrey.

      [student@serverX ~]$ sudo ls -l /run/gallifrey
Revision: rh134-7-c643331