Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sed permission denied when overwriting file

Tags:

linux

sed

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

$ 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?

like image 999
user1190650 Avatar asked Oct 12 '12 01:10

user1190650


6 Answers

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

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!

like image 84
Dan Miller Avatar answered Nov 18 '22 02:11

Dan Miller


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.

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

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

like image 43
nonuser Avatar answered Nov 18 '22 01:11

nonuser


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

It should looks like :

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

explanations

When invoking sed with -i flag, sed create a temporary file in /tmp dir.


Proof with 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 +++
like image 5
Gilles Quenot Avatar answered Nov 18 '22 02:11

Gilles Quenot


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.

like image 5
Ayvin Avatar answered Nov 18 '22 02:11

Ayvin


Given that you do not have write access to the directory where index.php is 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 -i is 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 sed without -i and end the line with something like > ~/sed.out to write the result into a file in your home directory. Then do what you will with that file.

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.php you can not mv ~/sed.out index.php. It would seem that cp will put the contents into index.php (not modifying the directory you do not have write permissions to) while mv tries to change the directory (that you don't have write permissions to).

like image 2
altendky Avatar answered Nov 18 '22 01:11

altendky


check for the /tmp folder permission It should have the following permission

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 sudo infront of your sed command to execute the command as a root user.

This will solve the issue

like image 1
Jithin Avatar answered Nov 18 '22 01:11

Jithin