Mount

當 fstab 有 1777 時,為什麼 /tmp 以權限 0755 掛載?

  • December 26, 2018

對於/tmpin /etc/fstab,我有mode=1777,但重新啟動後,權限/tmp0755。另一個目錄/var/tmp的配置方式完全相同,但沒有這個問題(見下文)。這是一個執行 Ubuntu 18.04 伺服器的樹莓派。根文件系統是一個只讀安裝的 microSD 卡。

使1777權限永久化的正確方法是什麼?

以下是一些額外的細節(重新啟動後):

$ touch /tmp/test
touch: cannot touch '/tmp/test': Permission denied

$ whoami
ubuntu

$ ls -ld /tmp /var/tmp
drwxr-xr-x 9 root root 180 Dec 26 13:54 /tmp
drwxrwxrwt 4 root root  80 Dec 26 13:54 /var/tmp

$ mount |grep /tmp
tmpfs on /var/tmp type tmpfs (rw,nosuid,nodev,noexec,noatime,size=65536k)
tmpfs on /tmp type tmpfs (rw,nosuid,nodev,noexec,noatime,size=131072k)

$ grep /tmp /etc/fstab
tmpfs  /var/tmp  tmpfs  defaults,noatime,nosuid,nodev,noexec,mode=1777,size=64M   0  0
tmpfs  /tmp      tmpfs  defaults,noatime,nosuid,nodev,noexec,mode=1777,size=128M  0  0

$ sudo systemctl status tmp.mount
● tmp.mount - /tmp
  Loaded: loaded (/etc/fstab; generated)
  Active: active (mounted) since Sun 2018-01-28 15:58:18 UTC; 10 months 27 days ago
   Where: /tmp
    What: tmpfs
    Docs: man:fstab(5)
          man:systemd-fstab-generator(8)
 Process: 642 ExecMount=/bin/mount tmpfs /tmp -t tmpfs -o defaults,noatime,nosuid,nodev,noexec,mode=1777,size=128M (code=exited, status=0/SUCCESS)
   Tasks: 0 (limit: 2146)
  CGroup: /system.slice/tmp.mount

Jan 28 15:58:18 testsystem systemd[1]: Mounting /tmp...
Jan 28 15:58:18 testsystem systemd[1]: Mounted /tmp.

$ grep -R '/tmp' /etc/tmpfiles.d /usr/lib/tmpfiles.d
/usr/lib/tmpfiles.d/x11.conf:D! /tmp/.X11-unix 1777 root root 10d
/usr/lib/tmpfiles.d/x11.conf:D! /tmp/.ICE-unix 1777 root root 10d
/usr/lib/tmpfiles.d/x11.conf:D! /tmp/.XIM-unix 1777 root root 10d
/usr/lib/tmpfiles.d/x11.conf:D! /tmp/.font-unix 1777 root root 10d
/usr/lib/tmpfiles.d/x11.conf:D! /tmp/.Test-unix 1777 root root 10d
/usr/lib/tmpfiles.d/x11.conf:r! /tmp/.X[0-9]*-lock
/usr/lib/tmpfiles.d/tmp.conf:D /tmp 1777 root root -
/usr/lib/tmpfiles.d/tmp.conf:#q /var/tmp 1777 root root 30d
/usr/lib/tmpfiles.d/tmp.conf:x /tmp/systemd-private-%b-*
/usr/lib/tmpfiles.d/tmp.conf:X /tmp/systemd-private-%b-*/tmp
/usr/lib/tmpfiles.d/tmp.conf:x /var/tmp/systemd-private-%b-*
/usr/lib/tmpfiles.d/tmp.conf:X /var/tmp/systemd-private-%b-*/tmp
/usr/lib/tmpfiles.d/tmp.conf:R! /tmp/systemd-private-*
/usr/lib/tmpfiles.d/tmp.conf:R! /var/tmp/systemd-private-*

$ sudo chmod 1777 /tmp

$ ls -ld /tmp /var/tmp
drwxrwxrwt 9 root root 180 Dec 26 13:55 /tmp
drwxrwxrwt 4 root root  80 Dec 26 13:55 /var/tmp

$ cat /etc/rc.local
#!/bin/bash
service ntp start
exit 0

$ uname -a
Linux testsystem 4.15.0-1030-raspi2 #32-Ubuntu SMP PREEMPT Fri Dec 7 09:15:28 UTC 2018 armv7l armv7l armv7l GNU/Linux

相關的未回答問題:

這是我初始配置的一部分(因為/以只讀方式安裝):

sudo rm -rf /var/spool && sudo ln -s /tmp /var/spool

顯然在啟動時,系統確實如此chmod 755 /var/spool,這/tmp在我的情況下發生了變化。

修復方法是用普通目錄替換符號連結並添加第三個tmpfs掛載:

sudo rm -rf /var/spool && sudo mkdir /var/spool && sudo chmod 755 /var/spool
echo 'tmpfs /var/spool tmpfs  defaults,noatime,nosuid,nodev,noexec,mode=0755,size=64M   0  0' |sudo tee -a /etc/fstab

感謝大家的評論,這些評論將我引向了正確的方向,尤其是 Filipe Brandenburger 的“你有任何其他腳本或單元在啟動時與 /tmp 混淆嗎?”

引用自:https://unix.stackexchange.com/questions/490995