在 Ubuntu 20.04 上将 /tmp 挂载为 tmpfs

在 Ubuntu 20.04 上将 /tmp 挂载为 tmpfs

我想/tmp使用没有 fstab 条目的挂载tmp.mount

但是在 Ubuntu 20.04 LTS 上,它似乎不起作用。

user@user-x1:~$ systemctl status tmp.mount 
Unit tmp.mount could not be found.
user@user-x1:~$ sudo systemctl enable tmp.mount 
Failed to enable unit: Unit file tmp.mount does not exist.
user@user-x1:~$ sudo systemctl cat tmp.mount 
No files found for tmp.mount.
user@user-x1:~$ cat /usr/share/systemd/tmp.mount 
#  SPDX-License-Identifier: LGPL-2.1+
#
#  This file is part of systemd.
#
#  systemd is free software; you can redistribute it and/or modify it
#  under the terms of the GNU Lesser General Public License as published by
#  the Free Software Foundation; either version 2.1 of the License, or
#  (at your option) any later version.

[Unit]
Description=Temporary Directory (/tmp)
Documentation=https://systemd.io/TEMPORARY_DIRECTORIES
Documentation=man:file-hierarchy(7)
Documentation=https://www.freedesktop.org/wiki/Software/systemd/APIFileSystems
ConditionPathIsSymbolicLink=!/tmp
DefaultDependencies=no
Conflicts=umount.target
Before=local-fs.target umount.target
After=swap.target

[Mount]
What=tmpfs
Where=/tmp
Type=tmpfs
Options=mode=1777,strictatime,nosuid,nodev

[Install]
WantedBy=local-fs.target
user@user-x1:~$ 

发生了什么事?为什么即使 bash 自动制表符已填满,systemd 仍会说找不到单元?无需任何配置即可tmp.mount启用此功能的最简单方法是什么?tmpfs

答案1

tmp.mount 默认不启用。只需复制并启用它

sudo cp -v /usr/share/systemd/tmp.mount /etc/systemd/system/ 
sudo systemctl enable tmp.mount

答案2

请注意,systemctl enable接受单元文件的绝对路径。(请参阅systemctl 手册页)因此无需手动复制。

我已经在 Ubuntu 22.04 上测试过:

$ sudo systemctl enable /usr/share/systemd/tmp.mount
Created symlink /etc/systemd/system/local-fs.target.wants/tmp.mount → /usr/share/systemd/tmp.mount.
Created symlink /etc/systemd/system/tmp.mount → /usr/share/systemd/tmp.mount.

相关内容