I wrote a shell script .scripter.sh and wanted to simulate executing the script as another non-root user. I used sudo -u daemon bash .scripter.sh to do so, but it says "no permission". I then saw that I cannot read any file as user daemon, and in fact

stefan@stefan-HP-Laptop:~$ touch file
stefan@stefan-HP-Laptop:~$ ll file 
-rw-r--r-- 1 stefan stefan 0 Dez  4 19:01 file
stefan@stefan-HP-Laptop:~$ sudo -u daemon [ -r file ]; echo $?
1
stefan@stefan-HP-Laptop:~$

which shows that the user has no read permission, although the permission -rw-r--r-- indicates that the file is readable by anyone. I hope someone can shed light on this mystery. Thanks

Edit: Actually, the following works

stefan@stefan-HP-Laptop:~$ cd test/
stefan@stefan-HP-Laptop:~/test$ ll -d .
drwxr-xr-x 2 stefan stefan 4096 Dez  4 19:40 ./
stefan@stefan-HP-Laptop:~/test$ touch file
stefan@stefan-HP-Laptop:~/test$ sudo -u daemon [ -r file ]; echo $?
[sudo] Passwort für stefan: 
0

As steeldriver wrote, it has to do with execute permission on the file's parent directory. Indeed, my home directory did not grant any user other than myself any rights.

Edit2: It seems you need execute permission on every directory between you and the file you want to read, including your current working directory. I cannot access ~/test/file as another user from my home folder, but I can do it as another user when I'm in directory ~/test. This coincides with this answer

  • @steeldriver: Actually, I'm still a bit puzzled. I will need to look into this. Permission for a directory keep confusing me :-) Thank you – Stefan Hamcke Dec 4 '17 at 18:53
  • @steeldriver: Would you like to make this an answer, since you gave the crucial information? – Stefan Hamcke Dec 4 '17 at 19:18
up vote 3 down vote accepted

This behavior may result from a lack of execute permission on the file's parent directory

Ex. given

$ ls -ld tests/{,file}
drwxrwxr-x 2 steeldriver steeldriver 4096 Dec  4 13:46 tests/
-rw-rw-r-- 1 steeldriver steeldriver  322 Dec  4 13:33 tests/file

then

$ sudo -u daemon [ -r tests/file ]; echo $?
0

but

$ chmod o-x tests
$ sudo -u daemon [ -r tests/file ]; echo $?
1

You can think of execute permission for a directory as permission to open and traverse the directory contents.

  • Yes It appears to be the case but OP did not state that originally! – George Udosen Dec 5 '17 at 3:14

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.