Linux › Section A · The big picture › Topic A.4
User space and users
Almost everything you notice on a Linux system, from the shell to the web server, happens in user space. This topic shows how the processes there are organised and how users, groups and the superuser decide who may do what.
Where the action happens
The memory the kernel hands out to processes is called user space, and the word also covers the whole collection of running processes; the informal term userland means the same. Linux user space is remarkably varied. The same kernel sits under the servers behind most websites, every Android phone, routers and car systems, and, since November 2017, every machine on the TOP500 list of the fastest supercomputers. What differs from one device to another is mostly the user-space software. To the kernel, though, all processes look alike: each is simply a running program with memory, open files and an owner.
Layers inside user space
Processes do different kinds of jobs, and it helps to sort them roughly into three levels. At the bottom are small basic services, each doing one simple task: recording log messages, configuring network interfaces, or carrying messages between programs over a communication bus such as D-Bus. In the middle are larger utility services, such as a name-caching server for DNS lookups, a print service or a database. At the top are the applications people use directly, such as a desktop, a browser or an editor. A component usually relies on others at its own level or below. This picture is only a guide: user space has no strict rules, and a web server could be placed at the top or in the middle.
Worked example: one web request across user space
You type a web address into a browser on a Linux desktop. Which user-space components take part? 1. The browser, at the top level, needs the site's IP address. 2. It asks the system's resolver; on many distributions this is the caching service systemd-resolved, a middle-level component that answers from its cache or asks a DNS server. 3. Reaching the network relies on bottom-level services: a network manager has already configured the interface, and a logging service such as the systemd journal records any errors. 4. The kernel moves the actual packets. Check: each component used ones at its own level or below, and only the packet handling happened in the kernel, below all of them.
The Unix way of building tools
Much of user space follows conventions inherited from Unix. Programs tend to be small and do one job well, and they are combined: the output of one becomes the input of the next through a pipe, as the topic "Shell input and output: redirection and pipes" shows. Text is the common format, so configuration lives in readable files, mostly under /etc, and logs can be searched with ordinary tools. Many things appear as files even when they are not stored on a disk: devices under /dev, and process and kernel information under /proc and /sys. Learning these habits makes an unfamiliar Linux system feel predictable.
Users and user IDs
The kernel supports the traditional Unix idea of a user: an identity that can run processes and own files. People know users by usernames such as ana, but the kernel itself works only with numbers called user IDs (UIDs). The translation between names and numbers is kept in ordinary files, as the topic "User management files" explains. The superuser, root, always has UID 0. On most current distributions the first account created for a person gets UID 1000, the next 1001, and so on. Because files record a UID rather than a name, renaming a user leaves their files' ownership intact.
Worked example: reading the id command
On an Ubuntu laptop, user ana runs id and sees this illustrative output: uid=1000(ana) gid=1000(ana) groups=1000(ana),27(sudo). 1. uid=1000(ana): her processes run with user ID 1000, shown with its name. 2. gid=1000(ana): her primary group, which new files she creates will belong to, also happens to be named ana. 3. groups lists every group she belongs to, including group 27, sudo. 4. On Ubuntu, membership of sudo allows a user to run commands as root with the sudo command; Fedora uses a group called wheel for the same purpose. Check: the numbers are what the kernel uses; the names in brackets are only for people.
Owners and boundaries
Users exist mainly to set boundaries. Every process has an owner, and we say it runs as that user. A user may stop or change the behaviour of their own processes, within limits, but not those of other users. Suppose ana tries kill 2310 on a process owned by ben: the kernel refuses, and bash reports an error such as kill: (2310) - Operation not permitted. Files have owners too, and an owner decides whether others may read or change them, as the topic "File modes and permissions" shows. Many accounts belong to no person: services often run as dedicated system users, so a fault in one service cannot touch other users' files.
Groups
A group is a named set of users, identified to the kernel by a group ID (GID). Its main purpose is sharing: a file can be made readable or writable by every member of a group without opening it to everyone. Each user has one primary group, used for new files, and may belong to several supplementary groups. On many distributions each new user gets a private primary group with the same name, as ana did. Groups also grant abilities: on Ubuntu, members of sudo may use the sudo command, and on Fedora the same is true of wheel.
The superuser
The root user is the exception to every boundary above. Root may stop or alter any process and read or change any file on the local system, which is why it is called the superuser, and anyone who can act as root is an administrator. That power has no safety net: the system will carry out a harmful command from root without question. So designers try to make root unnecessary for everyday tasks, and administrators use sudo to run single commands as root, as the topic "Running commands as the superuser" explains. Note that root's processes still run in user mode; root is all-powerful in user space, not part of the kernel.
Common mistakes to avoid
First, assuming the kernel stores usernames; it works with numeric UIDs and GIDs. Second, thinking root runs in kernel mode; root's processes are ordinary user-mode processes with every permission. Third, treating the three levels of user space as fixed rules; they are only a rough guide. Fourth, assuming every account is a person; many are system users for services. Fifth, logging in as root for daily work, when one mistyped command could then damage the whole system. Sixth, confusing a primary group with the full list of groups that id prints.
Key takeaways
User space is the memory given to processes and the processes themselves. It roughly divides into basic services, utility services and applications, each using components at its level or below, and it follows Unix habits: small tools, pipes, text configuration under /etc, and devices and kernel data shown as files. Users own processes and files; the kernel knows them by UID, with root as UID 0 and people usually from 1000. Users control only their own processes and files. Groups, with GIDs, share access and grant abilities such as sudo or wheel. Root can do anything but still runs in user mode.
Flashcards
Flashcard
Memory given to processes, and the running processes themselves
Test your understanding
Question 1 of 5
User ana (UID 1000) runs kill on a process owned by ben (UID 1001). What happens?
Question 2 of 5
id prints uid=1000(ana) gid=1000(ana) groups=1000(ana),27(sudo). Which statement is correct?
Question 3 of 5
In which mode do the root user's processes run?
Question 4 of 5
In the rough three-level picture of user space, where does a DNS name-caching service belong?
Question 5 of 5
Why does renaming a user account leave the ownership of their files unchanged?
