Bookmark this page

Guided Exercise: Manage Temporary Files

In this exercise, you configure systemd-tmpfiles to change how quickly it removes temporary files from the /tmp directory, and also to periodically purge files from another directory.

Outcomes

  • Configure systemd-tmpfiles to remove unused temporary files from the /tmp directory.

  • Configure systemd-tmpfiles to periodically purge files from another directory.

As the student user on the workstation machine, use the lab command to prepare your system for this exercise.

This command prepares your environment and ensures that all required resources are available.

[student@workstation ~]$ lab start scheduling-tempfiles

Instructions

  1. Log in to the servera system as the student user and switch to the root user.

    [student@workstation ~]$ ssh student@servera
    ...output omitted...
    [student@servera ~]$ sudo -i
    [sudo] password for student: student
    [root@servera ~]#
  2. Configure the systemd-tmpfiles service to clean the /tmp directory of any unused files from the last five days. Ensure that a package update does not overwrite the configuration files.

    1. Copy the /usr/lib/tmpfiles.d/tmp.conf file to the /etc/tmpfiles.d directory.

      [root@servera ~]# cp /usr/lib/tmpfiles.d/tmp.conf \
      /etc/tmpfiles.d/tmp.conf
    2. Search for the configuration line in the /etc/tmpfiles.d/tmp.conf file that applies to the /tmp directory. Replace the existing age of the temporary files in that configuration line with the new age of 5 days. Remove from the file all the other lines, including the commented lines. You can use the vim /etc/tmpfiles.d/tmp.conf command to edit the configuration file.

      In the configuration, the q type is the same as the d type, and instructs the systemd-tmpfiles service to create the /tmp directory if it does not exist. The directory's octal permissions must be set to 1777. Both the owning user and group of the /tmp directory must be root. The /tmp directory must not contain the unused temporary files from the last five days.

      The /etc/tmpfiles.d/tmp.conf file should appear as follows:

      q /tmp 1777 root root 5d
    3. Verify the /etc/tmpfiles.d/tmp.conf file configuration.

      Because the command does not return any errors, it confirms that the configuration settings are correct.

      [root@servera ~]# systemd-tmpfiles --clean /etc/tmpfiles.d/tmp.conf
  3. Add a new configuration that ensures that the /run/momentary directory exists, and that user and group ownership is set to the root user. The octal permissions for the directory must be 0700. The configuration must purge from this directory any files that remain unused in the last 30 seconds.

    1. Create the /etc/tmpfiles.d/momentary.conf file with the following content.

      With the configuration, the systemd-tmpfiles service ensures that the /run/momentary directory exists and that its octal permissions are set to 0700. The ownership of the /run/momentary directory must be the root user and group. The service purges from this directory any file if it remains unused for 30 seconds.

      [root@servera ~]# vim /etc/tmpfiles.d/momentary.conf
      d /run/momentary 0700 root root 30s
    2. Verify the /etc/tmpfiles.d/momentary.conf file configuration. The command creates the /run/momentary directory if it does not exist.

      Because the command does not return any errors, it confirms that the configuration settings are correct.

      [root@servera ~]# systemd-tmpfiles --create \
      /etc/tmpfiles.d/momentary.conf
    3. Verify that the systemd-tmpfiles command creates the /run/momentary directory with the appropriate permissions, owner, and group owner.

      The octal permission for the /run/momentary directory is set to 0700, and the user and group ownership are set to root.

      [root@servera ~]# ls -ld /run/momentary
      drwx------. 2 root root 40 Apr  4 06:35 /run/momentary
  4. Verify that the systemd-tmpfiles --clean command removes from the /run/momentary directory any file that is unused in the last 30 seconds, based on the systemd-tmpfiles configuration for the directory.

    1. Create the /run/momentary/test file.

      [root@servera ~]# touch /run/momentary/test
    2. Configure your shell prompt not to return for 30 seconds.

      [root@servera ~]# sleep 30
    3. After your shell prompt returns, clean stale files from the /run/momentary directory, based on the referenced rule in the /etc/tmpfiles.d/momentary.conf configuration file.

      The command removes the /run/momentary/test file, because it remains unused for 30 seconds. This behavior is based on the referenced rule in the /etc/tmpfiles.d/momentary.conf configuration file.

      [root@servera ~]# systemd-tmpfiles --clean \
      /etc/tmpfiles.d/momentary.conf
    4. Verify that the /run/momentary/test file does not exist.

      [root@servera ~]# ls -l /run/momentary/test
      ls: cannot access '/run/momentary/test': No such file or directory
    5. Return to the workstation machine as the student user.

      [root@servera ~]# exit
      logout
      [student@servera ~]$ exit
      logout
      Connection to servera closed.
      [student@workstation ~]$

Finish

On the workstation machine, change to the student user home directory and use the lab command to complete this exercise. This step is important to ensure that resources from previous exercises do not impact upcoming exercises.

[student@workstation ~]$ lab finish scheduling-tempfiles

This concludes the section.

Revision: rh199-9.0-4fecb06