This question already has an answer here:

My aim is to edit the file (whose owner is user1) using a script where user2 doesn't have permission to directly edit the file but it should be edited when executed through script by user2. So I set SUID on the script My script has the following content

echo "Enter variable a"
read a
echo "Enter path"
read Path
echo "Enter variable b"
read b
sed -i -c "/#Hello variable \<$a\>/{h;n;s/\$Initial_Value=.*/\$Initial_Value=$b/}" "$Path"

The script replaces some matching pattern in the file provided as input Permissions of the script are as follows

-rwsr-xr-x 1 user1 user1 648 Jul 21 08:03 Script.ksh

Permissions of the file which script modifies are as follows

-rwxrwxr-x 1 user1 user1 224 Jul 21 10:46 file

Permissions of the directory under which the file is present are as follows

drwxrwxr-x 6 user1 group1 122880 Jul 22 11:17 directory

The script is executed by user2 who is part of group1 where the file exists, and executing user2 will not have permissions to directly edit the file.

To edit the file indirectly using script I have set SUID bit on the script. I thought that when executed by user2 it will get executed with the permissions of user1 But when I am executing the script, SED is giving below error.

sed: cannot copy Path/sedFKvRSo to Path/file: Permission denied

marked as duplicate by Gilles linux Jul 24 '15 at 0:17

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 2
    You can't suid a script. – mikeserv Jul 22 '15 at 17:24