So as root, when I try to cat a file without the read permission, I can still see what's in it, which seems kind of strange, because as root I cannot write to a file without the w permission (it opens as read-only) and I cannot execute a file without the x permission.

Am I doing something wrong?

[root@aaaaaa enc]# ll logsuper
--w-------. 1 root root 3268 Apr 19 13:26 logsuper
[root@aaaaaa enc]# head logsuper
Done at Fri Apr 19 12:22:02 UTC 2019

Also can't execute file without the x permission as root:

[root@aaaaaa enc]# ll test1
----------. 1 root root 25 Apr 19 13:40 test1
[root@aaaaaa enc]# cat test1

echo "${1}"
[root@aaaaaa enc]# ./test1 asdfasdasgasga
bash: ./test1: Permission denied
  • Have you tested the exit code of your commands with echo $?; also for root the permission tests are bypassed: so root is, by definition, allowed to do anything. At last, learn about setuid – Basile Starynkevitch Apr 19 '19 at 13:32
  • 3
    Ordinary permissions do not apply to root. If an editor is saying the file is read only, root should be able to override it (with e.g. :w! in Vim). – Kusalananda Apr 19 '19 at 13:33
  • 4
    root isn't restricted by the lack of r permissions. It can also write to files without w. – Stephen Harris Apr 19 '19 at 13:34
  • I see, but what about the x permission? I still can't execute a file without x even as root – user323587 Apr 19 '19 at 13:38

The root can do anything and permission tests are bypassed for it.

Read also about setuid.

Since a program -even head or a shell, even when run by root (whose uid is 0 by definition)- uses system calls (listed in syscalls(2)...)

See also path_resolution(7) and capabilities(7) and credentials(7). Together they explain when open(2) -done by the process running /usr/bin/head - will fail. BTW, read(2) does not check permissions.

Notice that execution of some executable is done by execve(2) which documents when it could fail. Your shell is doing many fork(2) and execve calls.

A good Unix programming book, such as the old ALP, has several chapters explaining all that. Your shell is just another program (read also about the Unix philosophy) and you could write a shell in C (or study the source code of existing free software shell programs, e.g. of GNU bash).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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