我正在尝试使用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
Run Code Online (Sandbox Code Playgroud)
有谁知道如何解决这一问题?
小智 8
这里真的没有很好的答案。对不起,如果线程已死,它出现在谷歌搜索中,但没有真正回答这个问题。尽管 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
Run Code Online (Sandbox Code Playgroud)
希望有帮助!
就地编辑文件时,sed 创建一个临时文件,保存结果,最后将原始文件与临时文件一起 mv。
问题是您在 sed 试图创建临时文件的目录中没有写权限。
由于文件是/tmp/file,请检查您运行命令的目录的权限。
似乎您对/tmp目录有权限问题。(如下所述,您在 中运行命令phpshell,因此TMP dir可以在 之外的其他地方进行设置/tmp)
它应该看起来像:
$ ls -ld /tmp
drwxrwxrwx 333 root root 32768 12 oct. 03:13 /tmp/
Run Code Online (Sandbox Code Playgroud)
解释
sed使用-i标志调用时,sed在/tmpdir 中创建一个临时文件。
证明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 +++
Run Code Online (Sandbox Code Playgroud)