在 CGI 中写入文件会产生不同的文件

Writing file in CGI results in different file

我在 Perl CGI 中有这部分代码:

open(FH, '>', '/tmp/homeip.conf') or die $!;
print FH $ip;
close FH;

但是文件被写入 /tmp/systemd-private-CxvLw6/tmp/homeip.conf 而不是 /tmp/homeip.conf。为什么是这样?以及如何将文件写入所需的文件夹?

Why is this?

应用程序不应使用 /tmp,因为它通常是全局可写的,这是一种安全风险。 systemd tries to fix that by allowing services to create a private /tmp directory 与其他用户和服务看到的 /tmp 不同。

这在 Apache 的 systemd 单元文件中启用(/usr/lib/systemd/system/httpd.service 在我的系统上):

[Unit]
Description=The Apache HTTP Server
After=network.target remote-fs.target nss-lookup.target

[Service]
...
PrivateTmp=true

how can I write file to the desired folder?

这是一个安全风险,所以不要这样做。如果您需要其他进程可以访问您的临时文件,请将它们存储在一个不可写入的目录中。