Linux 为什么我们需要 mktemp?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11636790/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 13:54:00  来源:igfitidea点击:

Why do we need mktemp?

linuxshellfilesystems

提问by Shehbaz Jaffer

I do not understand the function of mktempand what a temporary file means.

我不明白mktemp临时文件的功能和含义。

Whats the difference between say touch xyzand mktemp xyz(apart from the fact that mktempwill create some file with xxx appended to it and will have 600 permissions?)

saytouch xyzmktemp xyz(除了mktemp会创建一些附加了 xxx 的文件并具有 600 权限?)

Please clarify.

请说清楚。

采纳答案by Igor Chubin

mktemprandomizes the name. It is very important from the security point of view.

mktemp随机化名称。从安全的角度来看,这是非常重要的。

Just imagine that you do something like:

试想一下,您执行以下操作:

echo something > /tmp/temporary-file

in your root-running script.

在您的 root 运行脚本中。

And someone (who has read your script) does

有人(读过你的剧本)做了

ln -s /etc/passwd /tmp/temporary-file

before.

前。

The mktempcommand could help you in this situation:

mktemp在这种情况下,该命令可以帮助您:

TEMP=$(mktemp /tmp/temporary-file.XXXXXXXX)
echo something > ${TEMP}

Now this ln /etc/passwdattack will not work.

现在这种ln /etc/passwd攻击将不起作用。

回答by paulsm4

You often want a "scratchpad file" (or directory). Moreover, you might need several such files at the same time, and you don't want to bother figuring out how to name them so there's no conflict.

您通常需要一个“便笺文件”(或目录)。此外,您可能同时需要多个这样的文件,并且您不想费心去弄清楚如何命名它们以免发生冲突。

"mktemp" fits the bill :)

“mktemp”符合要求:)

回答by linuxexplore

Ok actually it is written clearly in man pages.

好的,实际上它在手册页中写得很清楚。

mktemp - create a temporary file or directory.

Create a temporary file or directory, safely, and print its name.

mktemp - 创建一个临时文件或目录。

安全地创建一个临时文件或目录,并打印其名称。

It create a file or directory safely means no other user can access it, that's why its permission is 600

它安全地创建一个文件或目录意味着没有其他用户可以访问它,这就是为什么它的权限是 600

touch - change file timestamps

触摸 - 更改文件时间戳

It simply change the timestamps of a file if already created and create a file if does not exist. But file permission is still 644 by default.

如果已经创建,它只是更改文件的时间戳,如果文件不存在则创建一个文件。但是默认情况下文件权限仍然是 644。

For more detail check following man pages:

有关更多详细信息,请查看以下手册页:

http://linux.die.net/man/1/mktemp

http://linux.die.net/man/1/mktemp

http://linux.die.net/man/1/touch

http://linux.die.net/man/1/touch

回答by leed25d

At least in the bash shell you can do something like:

至少在 bash shell 中,您可以执行以下操作:

dirpath="/tmp/dir1-$$/dir2-$$"  
mkdir -p $dirpath  
chmod -R 0700 /tmp/dir1-$$  

for instance.

例如。

回答by Grzegorz Wierzowiecki

One more extra reason: not all systems use /tmpas temporary directory. For example https://termux.com/due to technical reasons (it runs as processes inside Android), has different long path as it's tmp directory.

还有一个额外的原因:并非所有系统都/tmp用作临时目录。例如https://termux.com/由于技术原因(它作为 Android 内部的进程运行),与 tmp 目录具有不同的长路径。

Scripts that create temporary files or directories using mktempwill be portable and also work in such special environments.

使用创建临时文件或目录的脚本mktemp将是可移植的,并且也可以在此类特殊环境中工作。