Ubuntu 14.04 / 16.04 在 reboot 後保留 tmp 內的檔案

2018-01-05 Ubuntu

最近有同事問到在 Ubuntu 16.04 要怎麼讓 /tmp 裡面的檔案保留,預設 Ubuntu Reboot 後都會清空一些暫存的目錄,例如 /tmp、/run、/var/run、/var/tmp 這類型的位置,不是單單只有 /tmp 而已。

 

現在這個 case 是同事報案想要了解 reboot 後怎麼保留 /tmp 裡的資料,一開始同事找到在 askubuntu 裡「How is the /tmp directory cleaned up?」 的最佳解答 /etc/default/rcS 設定 TMPTIME = 0 的方式

 

不過這個解答只適用於 Ubuntu 14.04 的環境,因為 Ubuntu 16.04 已經不使用 TMPTIME 的方式來處理暫存檔案的清除了。

 

在同一篇下面的 Answers 有提到 Ubuntu 16.04 改用 tmpfiles.d 來處理所有的暫存檔案

 

I’m checking this on Ubuntu 16.10. I can certify that editing /etc/default/rcS has no effect at all anymore and the files in tmp are wiped out by reboot no matter what you put in that file. As others mention, tmpreaper is no longer used.

I think the right answer is that Ubuntu 16.10 has a new setup. There is a folder /etc/tmpfiles.d, documented in the man page “tmpfiles.d”. In that folder, one should place a configuration file to control whether the /tmp is to be erased. This is what I am doing to stop reboots from erasing files in /tmp unless they are 20 days old:

 

首先你可以用 man tmpfiles.d 來查看文件,你可以在這三個地方找到設定

 

  • /etc/tmpfiles.d/*.conf
  • /run/tmpfiles.d/*.conf
  • /usr/lib/tmpfiles.d/*.conf

 

裝起來的服務大致上都會寫在 /usr/lib/tmpfiles.d/.conf 這個位置,使用者自定的部份可以在 /etc/tmpfiles.d/.conf 去覆蓋。

 

如果要讓 /tmp 保留 20 天可以這樣寫

# /etc/tmpfiles.d/tmp.conf

d /tmp 1777 root root 20d

 

或是永久保留用 – 參數

# /etc/tmpfiles.d/tmp.conf

d /tmp 1777 root root -

 

重新 reboot 一次測試 OK

 

 

 

給 Mr. 沙先生一點建議

彙整

分類

展開全部 | 收合全部

License

訂閱 Mr. 沙先生 的文章

輸入你的 email 用於訂閱