4

I tried this on Opensuse 12 and Fedora 17, i use the following commans

touch file
chown 777 file
ls -l file
-rw-r--r-- 1 777 root 0 Oct  9 21:43 file
grep 777 /etc/passwd
echo $?
1

There is no user 777

 grep pippo /etc/passwd
 echo $?
 1
 chown pippo file
 chown: invalid user: `pippo'

But if i use non-existing user, i get one error.

Why can i use numeric non-existing user with chown, but not with names?

c4f4t0r
  • 5,301
  • 3
  • 31
  • 42

1 Answers1

10

Why can i use numeric non-existing user with chown, but not with names?

Because a username must be resolved to a UID, so if the user does not exist no resolution can occur, whereas a UID is a terminating type (it describes itself without resolution).

Technically all chown requests by the operating system must be done against a UID. The username is for your benefit, not the operating system.

Matthew Ife
  • 23,357
  • 3
  • 55
  • 72
  • This only happens for root user – c4f4t0r Oct 09 '13 at 20:29
  • Because you need CAP_CHOWN to change ownerships. That is a privileged capability normally reserved only to root. – Matthew Ife Oct 09 '13 at 20:32
  • Why not check if the numeric user correspond to an existing user, before do the system call? – c4f4t0r Oct 09 '13 at 20:37
  • It would be incredibly bad practice to name a user a number, it confuses many utilies, including chown. A username is supposed to provide a symbolic name to a number, providing a symbolic number to a number defeats the purpose. – Matthew Ife Oct 09 '13 at 20:43
  • 3
    `Why not check if the numeric user correspond to an existing use` - Because systems often have an external source for authentication. What happens if that external authentication system is broken because someone messed up the ownership of the config files? So a method needs to be in place to permit the system administrator to set ownership to the correct ID even if the authentication database is broken. – Zoredache Oct 09 '13 at 21:44