RHCSA Rapid Track
In this exercise, you will configure systemd-tmpfiles in order to change how quickly it removes temporary files from /tmp, and also to periodically purge files from another directory.
Outcomes
You should be able to:
Configure
systemd-tmpfilesto remove unused temporary files from/tmp.Configure
systemd-tmpfilesto periodically purge files from another directory.
Log in to workstation as student using student as the password.
On workstation, run lab scheduling-tempfiles start to start the exercise.
This script creates the necessary files and ensures that the environment is set up correctly.
[student@workstation ~]$lab scheduling-tempfiles start
From
workstation, open an SSH session toserveraasstudent.[student@workstation ~]$ssh student@servera...output omitted...[student@servera ~]$Configure
systemd-tmpfilesto clean the/tmpdirectory so that it does not contain files that that have not been used in the last five days. Ensure that the configuration does not get overwritten by any package update.Use the
sudo -icommand to switch to therootuser.[student@servera ~]$sudo -i[sudo] password for student:student[root@servera ~]#Copy
/usr/lib/tmpfiles.d/tmp.confto/etc/tmpfiles.d/tmp.conf.[root@servera ~]#cp /usr/lib/tmpfiles.d/tmp.conf \/etc/tmpfiles.d/tmp.confSearch for the configuration line in
/etc/tmpfiles.d/tmp.confthat applies to the/tmpdirectory. Replace the existing age of the temporary files in that configuration line with the new age of5days. Remove all the other lines from the file including the commented ones. You can use thevim /etc/tmpfiles.d/tmp.confcommand to edit the configuration file. The/etc/tmpfiles.d/tmp.conffile should appear as follows:q /tmp 1777 root root 5d
In the preceding configuration, the
qtype is identical todand instructssystemd-tmpfilesto create the/tmpdirectory if it does not exist. The directory must have the octal permissions set to1777. Both the owning user and group of/tmpmust beroot. The/tmpdirectory must be free from the temporary files which are unused in the last five days.Use the
systemd-tmpfiles --cleancommand to verify that the/etc/tmpfiles.d/tmp.conffile contains the correct configuration.[root@servera ~]#systemd-tmpfiles --clean /etc/tmpfiles.d/tmp.confBecause the preceding command did not return any errors, it confirms that the configuration settings are correct.
Add a new configuration that ensures that the
/run/momentarydirectory exists with user and group ownership set toroot. The octal permissions for the directory must be0700. The configuration should purge any file in this directory that remains unused in the last 30 seconds.Create the file called
/etc/tmpfiles.d/momentary.confwith the following content. You can use thevim /etc/tmpfiles.d/momentary.confcommand to create the configuration file.d /run/momentary 0700 root root 30s
The preceding configuration causes
systemd-tmpfilesto ensure that the/run/momentarydirectory exists with its octal permissions set to0700. The user and group ownership of/run/momentarymust beroot. Any file in this directory that remains unused in the last 30 seconds must be purged.Use the
systemd-tmpfiles --createcommand to verify that the/etc/tmpfiles.d/momentary.conffile contains the appropriate configuration. The command creates the/run/momentarydirectory if it does not exist.[root@servera ~]#systemd-tmpfiles --create \/etc/tmpfiles.d/momentary.confBecause the preceding command did not return any errors, it confirms that the configuration settings are correct.
Use the
lscommand to verify that the/run/momentarydirectory is created with the appropriate permissions, owner, and group owner.[root@servera ~]#ls -ld /run/momentarydrwx------.2root root40 Mar 21 16:39 /run/momentaryNotice that the octal permission set of
/run/momentaryis0700and that the user and group ownership are set toroot.
Verify that any file under the
/run/momentarydirectory, unused in the last 30 seconds, is removed, based on thesystemd-tmpfilesconfiguration for the directory.Use the
touchcommand to create a file called/run/momentary/testfile.[root@servera ~]#touch /run/momentary/testfileUse the
sleepcommand to configure your shell prompt not to return for 30 seconds.[root@servera ~]#sleep 30After your shell prompt returns, use the
systemd-tmpfiles --cleancommand to clean stale files from/run/momentary, based on the rule mentioned in/etc/tmpfiles.d/momentary.conf.[root@servera ~]#systemd-tmpfiles --clean \/etc/tmpfiles.d/momentary.confThe preceding command removes
/run/momentary/testfilebecause the file remained unused for 30 seconds and should have been removed based on the rule mentioned in/etc/tmpfiles.d/momentary.conf.Use the
ls -lcommand to verify that the/run/momentary/testfilefile does not exist.[root@servera ~]#ls -l /run/momentary/testfilels: cannot access '/run/momentary/testfile': No such file or directoryExit the
rootuser's shell and log out ofservera.[root@servera ~]#exitlogout[student@servera ~]$exitlogout Connection to servera closed.[student@workstation ~]$