$_ CLI In Depth
Learning progress 0%
lesson syscalls namespaces cgroups

cgroups v2 Basics

5 min read Module 17: Syscalls, Namespaces & cgroups Updated Dec 5, 2026
Cheat Sheet
0:00 / 3:19

Groups of processes, not single processes

A limit on one process is easy to get around: the process forks a child, and the child has its own budget. Control groups solve this by attaching limits to a group. Every process belongs to exactly one cgroup in each hierarchy, and the kernel adds up the usage of the members. A service that starts twenty workers is limited as one unit, not twenty separate times.

Cgroups have two jobs. They account, which means measuring CPU, memory, and I/O for the group, and they control, which means enforcing limits or weights. Monitoring tools read the accounting files, and container runtimes write the control files.

The v2 unified hierarchy

The first version of cgroups had a separate tree for each controller, so a process could sit in one group for memory and another for CPU. Version 2 uses a single unified hierarchy for all controllers. The filesystem is mounted with the type cgroup2, usually at /sys/fs/cgroup. The kernel documentation describes the rules, and the most important one is the no internal processes rule: a group that has child groups does not hold processes itself, except at the root.

You can tell which version a system uses by reading the membership file of your own process. Under v2 there is exactly one line, and it begins with 0:: followed by a path.

BASH1 lines
cat /proc/self/cgroup

The path after the last colon is the group your shell belongs to, relative to the root of the hierarchy. A v1 system would list several lines, one per controller, each with a name in the middle field.

Control files

Every cgroup directory contains files that you read or write. A few are common to all groups: cgroup.procs lists the PIDs in the group, and writing a PID there moves that process into the group. cgroup.controllers lists the controllers available at that level, and cgroup.subtree_control chooses which of them are passed to child groups.

The controller files have names with a prefix. memory.max sets the hard memory limit, and memory.current shows the usage. cpu.max sets a quota per period, and cpu.stat reports the time used. pids.max limits the number of processes the group can create, which is a simple defense against fork bombs.

BASH1 lines
ls /sys/fs/cgroup

On a system with cgroup v2 mounted, the listing includes the control files described above. The lab kernel may mount the hierarchy in a different place or with fewer controllers, so the lab only asks you to read what is there.

Limits are enforced by the kernel

When a group reaches its memory limit, the kernel first tries to reclaim memory from that group. If it cannot, the OOM killer chooses a process in the group and kills it. For CPU, a quota means the group is paused for the rest of the period once it uses its share, so a busy process cannot starve others on the same machine. These rules work for any program, including one that was never written with limits in mind.

Warning

A memory limit that is too low does not fail politely. The process is killed when it exceeds the limit and the reclaim fails, so set limits with measurements from memory.current, not guesses.

Who can change them

Creating a cgroup or writing a limit normally needs root, or a delegated subtree where the owner has been given write access to the directory. Systemd uses delegation to let user services manage their own groups. The lab user is unprivileged, so the exercises only read the membership and the root listing, and that is enough to see how the tree is laid out.

cgroups and namespaces together

A container uses both mechanisms. A PID namespace hides the other processes, a cgroup limits the container to a memory budget, and both are attached to the same set of processes. The cgroup namespace adds one more trick: it makes the container see its own group as the root, so the paths in /proc/self/cgroup do not reveal the host layout. Reading the membership file before and after entering a container shows that change.

For the full reference, read cgroups(7) on man7.org and the cgroup v2 document in the kernel documentation.

Accounting files you can read

Even without permission to write, a process can read the accounting files of its own group. cpu.stat reports usage_usec, the total CPU time the group has used in microseconds, along with counters that show how often the quota paused the group. memory.current holds the current usage in bytes, and memory.stat breaks that usage down into anonymous memory, file cache, and kernel memory. Monitoring tools that show these numbers in a table are reading exactly these files.

Accounting values only grow while the group runs, so a rate needs the difference between two readings over an interval, just as with the counters in /proc. A single reading is a snapshot, and a snapshot is useful for limits but not for speed.

Hierarchy and inheritance

A child group does not inherit a limit automatically, but the kernel checks every ancestor. A process in a leaf group is limited by its own memory.max and by the limit of each parent on the path. The tightest limit wins, which lets an administrator put one budget on a whole service while each worker group gets a smaller slice of it.

Limits and the reading habit

A good habit is to read a limit before you change it. Read memory.max, cpu.max, and pids.max for the group, then read the usage files for the same group, and only then decide whether the limit is too low. A usage number close to the limit means the group is being throttled or killed, and a usage number far below it means the limit is not the problem. The same two-step reading works on a laptop and on a production host.

What a container really is: namespaces, cgroups and a root

Visualizer

A container is not a special kind of object. It is ordinary processes inside namespaces that limit what they can see, cgroups that limit what they can use, and a root directory of their own.

1 / 4
ls -l /proc/PID/ns, one link per namespace (inode numbers are examples)
cgroupcgroup:[4026531835]
ipcipc:[4026531839]
mntmnt:[4026531841]
netnet:[4026531840]
pidpid:[4026531836]
useruser:[4026531837]
utsuts:[4026531838]

Each process points at one namespace of each kind. Two processes with the same inode number share that namespace. This shell uses the default, host-wide ones.

ls -l /proc/$$/ns
Terminal lab · 3 steps · real Linux in your browser

Read cgroup membership

Find the cgroup your process belongs to and read the control files of the root cgroup.

01
Read your cgroup membership
The cgroup file lists the control groups your process belongs to. With cgroups v2 there is one line that starts with 0::. Save it: ``bash cat /proc/self/cgroup > cgroup.txt `` Open cgroup.txt and read the path after the second colon.
Show hint for step 1
Each line has three fields separated by colons: a hierarchy ID, the controllers, and the path.
02
List the cgroup control files
The cgroup filesystem is mounted under /sys/fs/cgroup. List its top level and save the names: ``bash ls /sys/fs/cgroup > cgroup-root.txt 2>&1 `` Open cgroup-root.txt. A v2 hierarchy has control files such as cgroup.controllers and cgroup.procs.
Show hint for step 2
The root of a v2 hierarchy has files that begin with cgroup. The step only checks that the listing was saved.
03
Write a note about the path
Combine the two results in one note. Take the path field from cgroup.txt and write it to note.txt with a label: ``bash path=$(cut -d: -f3 cgroup.txt | head -n 1) echo "cgroup path: $path" > note.txt `` Open note.txt. It should begin with cgroup path:.
Show hint for step 3
cut -d: -f3 takes the third field when the delimiter is a colon.
Check yourself Question 1 of 4
Concept Check The membership file recall

Which file in a cgroup directory lists the process IDs that belong to the group?

Recap & Key Takeaways

A cgroup groups processes so that CPU, memory, process count, and I/O are measured and limited together.
Cgroup v2 uses one unified hierarchy, and /proc/PID/cgroup shows a single 0:: line for your membership.
Control files such as cgroup.procs, memory.max, cpu.max, and pids.max read or set limits for a group.
Namespaces control what a process can see, while cgroups control how much it can use, and containers use both.
Was this lesson helpful? Spot an issue or have a suggestion? Let us know on GitHub.
Report an issue