如下圖,在開發調試的時候會遇到這么一個問題。
file_put_contents時,$tmp_file顯示的目標文件是/tmp/xxx.而這個文件實際放在linux的目錄卻是/tmp/systemd-private-xxxxx-php-fpm.service/xxx(見圖二)
為什么會出現這種情況?
只要使用Systemd這個進程作為啟動進程的linux系統,其子進程都會有PrivateTmp這么一個屬性,用於設置是否使用私有的tmp目錄。
那么只要設置使用這個屬性的service,都會使用私有的tmp目錄。
比如說:
nginx會有一個systemd-private-xxx-nginx.service/tmp目錄
php-fpm會有一個systemd-private-xxx-php-fpm.service/tmp目錄
使用PrivateTmp屬性有什么好處嗎?
1. 見下圖,/tmp目錄一般是所有用戶和所有service都共享的,對於所有用戶和service來說,都會有寫和讀的權限。
那么這里會存在安全性的問題。把各個service的tmp目錄隔離開的話,可以保證一定的安全性。
2.見下圖, 對於每個service的tmp目錄,會在服務啟動(start)時創建該目錄,並且在關閉(stop)服務時刪除該目錄。如果我們是使用自己定義的臨時目錄路徑的話,就需要我們自己定義相關的維護機制了(如:定期刪除這些臨時文件等),而使用這個PrivateTmp的話,就不需要我們自己去維護了。
怎么設置這個PrivateTmp屬性呢?
打個比方,現在我們要開啟 or 關閉 php-fpm 的這個屬性。
1. 利用systemctl找到對應的service路徑。
2 找到PrivateTmp並修改之。
ps:xx.service這種文件叫做unit文件,systemd把一個服務相關的啟動腳本叫做一個unit文件,如果要查看更多service文件的編寫規則,可以搜索"systemd unit" or "systemd unit 文件編寫" 這類的關鍵字 or man systemd.unit 。
相關參考:
man systemd.exec 查找PrivateTmp關鍵字
https://lists.centos.org/pipermail/centos/2015-April/151589.html
http://0pointer.de/blog/projects/security.html
https://access.redhat.com/blogs/766093/posts/1976243
http://fedoraproject.org/wiki/Features/ServicesPrivateTmp