Linux Kernel: uid and gid vs /etc/passwd

linuxlinux-kernelusers

How does the Linux kernel deal with UIDs and GIDs?

When I want to add a user to a system, does the kernel require some type of "registration" for this user (syscall?)? Does the kernel even care about which users are available in /etc/passwd or does it simply know about and deal with numeric values independently of that file's content?

Best Answer

For the kernel, a user or a group are just a number (the UID and GID) attached to a process and which are used to see if the process is allowed to e.g. read (really open(2)) a file (files carry UID/GID and permission bits around for this very purpose), and also other operations (e.g., processes can manipulate processes belonging to the same UID). There are system calls to change UID/GID of the calling process (setuid(2)/setgid(2) and friends). Obviously, there are severe restrictions on who can use them.

The system can use the numbers to look up names from /etc/passwd, /etc/group or a slew of other mechanisms (LDAP, NIS, others), but that is strictly for human consumption.

When you log in and give your username, a program (running as root, and so alowed to do a lot of things normal users aren't allowed) takes the username and looks up the UID (to see if that user exists in the first place), asks for the password (or some other authentication) and checks it. If all goes well, the program changes to that UID/GID and exec(2)s the user's shell (which again is just a run-of-the-mill program, exactly which one to start is part of the user's account description).

Related Question