RHCSA Rapid Track
Course update
An updated version of this course is available that uses a newer version of Red Hat Enterprise Linux in the lab environment. Therefore, the RHEL 9.0 version of the lab environment will retire on December 31, 2024. Please complete any work in this lab environment before it is removed on December 31, 2024. For the most up-to-date version of this course, we recommend moving to the RHEL 9.3 version.
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-tmpfilesto remove unused temporary files from the/tmpdirectory.Configure
systemd-tmpfilesto 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
Log in to the
serverasystem as thestudentuser and switch to therootuser.[student@workstation ~]$
ssh student@servera...output omitted... [student@servera ~]$sudo -i[sudo] password for student:student[root@servera ~]#Configure the
systemd-tmpfilesservice to clean the/tmpdirectory of any unused files from the last five days. Ensure that a package update does not overwrite the configuration files.Copy the
/usr/lib/tmpfiles.d/tmp.conffile to the/etc/tmpfiles.ddirectory.[root@servera ~]#
cp /usr/lib/tmpfiles.d/tmp.conf \/etc/tmpfiles.d/tmp.confSearch for the configuration line in the
/etc/tmpfiles.d/tmp.conffile that applies to the/tmpdirectory. 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 thevim /etc/tmpfiles.d/tmp.confcommand to edit the configuration file.In the configuration, the
qtype is the same as thedtype, and instructs thesystemd-tmpfilesservice to create the/tmpdirectory if it does not exist. The directory's octal permissions must be set to1777. Both the owning user and group of the/tmpdirectory must beroot. The/tmpdirectory must not contain the unused temporary files from the last five days.The
/etc/tmpfiles.d/tmp.conffile should appear as follows:q /tmp 1777 root root 5d
Verify the
/etc/tmpfiles.d/tmp.conffile 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
Add a new configuration that ensures that the
/run/momentarydirectory exists, and that user and group ownership is set to therootuser. The octal permissions for the directory must be0700. The configuration must purge from this directory any files that remain unused in the last 30 seconds.Create the
/etc/tmpfiles.d/momentary.conffile with the following content.With the configuration, the
systemd-tmpfilesservice ensures that the/run/momentarydirectory exists and that its octal permissions are set to0700. The ownership of the/run/momentarydirectory must be therootuser and group. The service purges from this directory any file if it remains unused for 30 seconds.[root@servera ~]#
d /run/momentary 0700 root root 30svim /etc/tmpfiles.d/momentary.confVerify the
/etc/tmpfiles.d/momentary.conffile configuration. The command creates the/run/momentarydirectory 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.confVerify that the
systemd-tmpfilescommand creates the/run/momentarydirectory with the appropriate permissions, owner, and group owner.The octal permission for the
/run/momentarydirectory is set to0700, and the user and group ownership are set toroot.[root@servera ~]#
ls -ld /run/momentarydrwx------.2root root40 Apr 4 06:35 /run/momentary
Verify that the
systemd-tmpfiles --cleancommand removes from the/run/momentarydirectory any file that is unused in the last 30 seconds, based on thesystemd-tmpfilesconfiguration for the directory.Create the
/run/momentary/testfile.[root@servera ~]#
touch /run/momentary/testConfigure your shell prompt not to return for 30 seconds.
[root@servera ~]#
sleep 30After your shell prompt returns, clean stale files from the
/run/momentarydirectory, based on the referenced rule in the/etc/tmpfiles.d/momentary.confconfiguration file.The command removes the
/run/momentary/testfile, because it remains unused for 30 seconds. This behavior is based on the referenced rule in the/etc/tmpfiles.d/momentary.confconfiguration file.[root@servera ~]#
systemd-tmpfiles --clean \/etc/tmpfiles.d/momentary.confVerify that the
/run/momentary/testfile does not exist.[root@servera ~]#
ls -l /run/momentary/testls: cannot access '/run/momentary/test': No such file or directoryReturn to the
workstationmachine as thestudentuser.[root@servera ~]#
exitlogout [student@servera ~]$exitlogout Connection to servera closed. [student@workstation ~]$
This concludes the section.