Linux 覆盖文件时sed权限被拒绝

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12850882/
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 14:35:16  来源:igfitidea点击:

sed permission denied when overwriting file

linuxsed

提问by user1190650

I am trying to use sed to overwrite my index.php file, but I am getting an error:

我正在尝试使用 sed 覆盖我的 index.php 文件,但出现错误:

$ sed -i 's@<head>@<head><script type="text/javascript" src="new.js"></script>@' index.php
sed: couldn't open temporary file ./sedmZNx8T: Permission denied

Does anyone know how to fix this?

有谁知道如何解决这一问题?

回答by Gilles Quenot

Seems like you have a permission issue on the /tmpdir. (as discussed bellow, you run command in phpshell, so TMP dircan be setted elsewhere than /tmp)

似乎您对/tmp目录有权限问题。(如下所述,您在 中运行命令phpshell,因此TMP dir可以在 之外的其他地方进行设置/tmp

It should looks like :

它应该看起来像:

$ ls -ld /tmp
drwxrwxrwx 333 root root 32768 12 oct.  03:13 /tmp/

explanations

解释

When invoking sedwith -iflag, sedcreate a temporary file in /tmpdir.

sed使用-i标志调用时,sed/tmpdir 中创建一个临时文件。



Proof with strace:

证明strace

$ strace -f -e trace=file sed -i 's/a/z/' /tmp/a
execve("/bin/sed", ["sed", "-i", "s/a/z/", "/tmp/a"], [/* 94 vars */]) = 0
access("/etc/ld.so.preload", R_OK)      = -1 ENOENT (No such file or directory)
open("/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3
(...)
open("/tmp/sedPSBTPG", O_RDWR|O_CREAT|O_EXCL, 0600) = 4
rename("/tmp/sedPSBTPG", "/tmp/a")      = 0
+++ exited with 0 +++

回答by altendky

Given that you do not have write access to the directory where index.phpis stored, then generally you would not have access to index.php...? Assuming this is a 'non-standard' case and you do have write access, sed -iis trying to create a temp file in the same directory as index.php(just checked and it is the same directory, not the CWD). Run sedwithout -iand end the line with something like > ~/sed.outto write the result into a file in your home directory. Then do what you will with that file.

鉴于您没有对index.php存储目录的写访问权限,那么通常您将无权访问index.php...?假设这是一个“非标准”情况并且您确实具有写访问权限,sed -i则尝试在与index.php(刚刚检查并且它是同一目录,而不是 CWD)相同的目录中创建一个临时文件。sed不带运行-i并以类似> ~/sed.out将结果写入主目录中的文件的方式结束该行。然后对那个文件做你想做的事情。

sed 's@<head>@<head><script type="text/javascript" src="new.js"></script>@' index.php > ~/sed.out

Be aware that you while you can cp ~/sed.out index.phpyou can not mv ~/sed.out index.php. It would seem that cpwill put the contents into index.php(not modifying the directory you do not have write permissions to) while mvtries to change the directory (that you don't have write permissions to).

要知道cp ~/sed.out index.php你能不能mv ~/sed.out index.php。似乎cp将内容放入index.php(不修改您没有写入权限mv的目录)同时尝试更改目录(您没有写入权限)。

回答by Dan Miller

Really no great answers here. Sorry if thread is dead, it came up in google searches and nothing really answered the question. Although altendky's answer is sort of the same as mine... There IS a workaround...find a world writable directory, say /tmp

这里真的没有很好的答案。对不起,如果线程已死,它出现在谷歌搜索中,但没有真正回答这个问题。尽管 altendky 的答案与我的有点相同......有一个解决方法......找到一个世界可写的目录,比如 /tmp

cp /etc/file_to_mod /tmp
sed -i -e 's/foo/bar/g' /tmp/file_to_mod
cat /tmp/file_to_mod >/etc_file_to_mod

Hope that helps!

希望有帮助!

回答by Bishwas Mishra

You need to have the write permissions to the folder whose file you are changing.

您需要对要更改其文件的文件夹具有写入权限。

As explained here: https://stackoverflow.com/a/36404569/5381704

如此处所述:https: //stackoverflow.com/a/36404569/5381704

回答by Ayvin

I think you can you "sudo" in front of your command. Like sudo sed -i 's/geteuid/getppid/g' /usr/bin/vlc It worked for me.

我认为你可以在你的命令前“sudo”。就像 sudo sed -i 's/geteuid/getppid/g' /usr/bin/vlc 它对我有用。

回答by Kevin RED

check for the /tmpfolder permission It should have the following permission

检查/tmp文件夹权限它应该具有以下权限

drwxrwxrwt   7 root root 4.0K Nov 16 15:06 tmp

If it is not ok for you then run the following commands

如果它不适合您,请运行以下命令

sudo chown root:root /tmp
sudo chmod 1777 /tmp

once this is done, then put a sudoinfront of your sedcommand to execute the command as a root user.

完成此操作后,sudo在您sed的命令前面放置一个以 root 用户身份执行该命令。

This will solve the issue

这将解决问题

回答by nonuser

While editing a file in place, sed creates a temporary file, saves the result and then finally mv the original file with the temporary one.

就地编辑文件时,sed 创建一个临时文件,保存结果,最后将原始文件与临时文件一起 mv。

The problem is that you don't have write permission in the directory where sed is trying to create the temp file.

问题是您在 sed 试图创建临时文件的目录中没有写权限。

As the file is /tmp/file, check the permission of the directory where you are running the command.

由于文件是/tmp/file,请检查您运行命令的目录的权限。