systemd-tmpfiles is started while booting, and manages temporary files and directories.
It will create and delete temporary files automatically, according to the configuration files in the following locations:
/usr/lib/tmpfiles.d/
/etc/tmpfiles.d/
/run/tmpfiles.d/
For manual configuration command, may create new .conf file and save at locations above.
Difference Between Type q And d
Type d: Creates a standard directory.
Type q: Create a btrfs subvolume.
The subvolume will behave like standard directory if in a standard filesystem like ext4.
Btrfs subvolume store data that fixed volume size and can be rollback if anything happen. Like OS root, web server logs.
So user's personal files are usually not stored in subvolume but stored in normal directory. So everytime rollback user's files are preserved.
systemd-tmpfiles --create
Refer 17 - File Permission
q /tmp 1777 root root --> Creates a subvolume at /tmp with permissions 1777, set root user and root group as owner
d /tmp/myfiles 0770 root root --> Creates a directory at /tmp/myfiles with permissions 0770, set root user and root group as owner
systemd-tmpfiles --clean
7d --> Clean up files/directories in /tmp older than 7 days
30s --> Clean up files/directories in /tmp/myfiles older than 30 seconds
systemd-tmpfiles --remove
/tmp --> Clean up files/directories in /tmp regardless age
/tmp/myfiles --> Clean up files/directories in /tmp/myfiles regardless age
systemd-tmpfiles --create
Create configuration file and save it at /etc/tmpfiles.d
echo "[Configuration]" | sudo tee -a /etc/tmpfiles.d/[Configuration File Name].conf;
Example: echo "d /tmp/myfiles 0770 root root 30s" | sudo tee -a /etc/tmpfiles.d/myfiles.conf;
Run create command
systemd-tmpfiles --create /etc/tmpfiles.d/[Configuration File Name].conf;
Example: systemd-tmpfiles --create /etc/tmpfiles.d/myfiles.conf;
Verify
Confirm permissions is drwxrwx--- (0770), owned by root user and root group.
ls -ld [Path]
Example: ls -ld /tmp/myfiles
systemd-tmpfiles --clean
Create a test file inside the directory
touch [Path]/[Test File Name]
Example: touch /tmp/myfiles/test
Verify test file creation
ls -ld [Path]/[Test File Name]
Example: ls -ld /tmp/myfiles/test
Pause execution for 30 seconds so the test file reaches its expiration threshold.
sleep 30
Run clean command
systemd-tmpfiles --clean /etc/tmpfiles.d/[Configuration File Name].conf;
Example: systemd-tmpfiles --clean /etc/tmpfiles.d/myfiles.conf;
Verify
Terminal should return No such file or directory
ls -ld [Path]/[Test File Name]
Example: ls -ld /tmp/myfiles/test
systemd-tmpfiles --remove
Run remove command
systemd-tmpfiles --remove /etc/tmpfiles.d/[Configuration File Name].conf;
Example: systemd-tmpfiles --remove /etc/tmpfiles.d/myfiles.conf;
Delete the configuration file
rm /etc/tmpfiles.d/[Configuration File Name].conf
Example: rm /etc/tmpfiles.d/myfiles.conf
Delete the directory
sudo rmdir /tmp/myfiles
Verify
Terminal should return No such file or directory
ls -ld [Path]/[Test File Name] /etc/tmpfiles.d/[Configuration File Name].conf;
Example: ls -ld /tmp/myfiles /etc/tmpfiles.d/myfiles.conf
Result
student@localhost:~$ echo "d /tmp/myfiles 0770 root root 30s" | sudo tee -a /etc/tmpfiles.d/myfiles.conf;
[sudo] password for student:
d /tmp/myfiles 0770 root root 30s
student@localhost:~$ sudo systemd-tmpfiles --create /etc/tmpfiles.d/myfiles.conf;
student@localhost:~$ ls -ld /tmp/myfiles
drwxrwx---. 2 root root 6 Sep 19 22:19 /tmp/myfiles
student@localhost:~$ sudo touch /tmp/myfiles/test
student@localhost:~$ sudo ls -ld /tmp/myfiles/test
-rw-r--r--. 1 root root 0 Sep 19 22:20 /tmp/myfiles/test
student@localhost:~$ sleep 30
student@localhost:~$ sudo systemd-tmpfiles --clean /etc/tmpfiles.d/myfiles.conf;
student@localhost:~$ sudo ls -ld /tmp/myfiles/test
ls: cannot access '/tmp/myfiles/test': No such file or directory
student@localhost:~$ sudo systemd-tmpfiles --remove /etc/tmpfiles.d/myfiles.conf;
student@localhost:~$ sudo rm /etc/tmpfiles.d/myfiles.conf
student@localhost:~$ sudo rmdir /tmp/myfiles
[sudo] password for student:
student@localhost:~$ sudo ls -ld /tmp/myfiles /etc/tmpfiles.d/myfiles.conf
ls: cannot access '/tmp/myfiles': No such file or directory
ls: cannot access '/etc/tmpfiles.d/myfiles.conf': No such file or directory