2.7 Users, groups, permissions, udev, and capabilities

On a single-tasking microcontroller without an operating system (or a simple RTOS build), every function runs with full hardware privilege. When engineers transition from microcontrollers to an embedded Linux gateway, a common bad habit is to compile their C++ or Python gateway program and run it as the superuser (root, UID 0), either by logging in as root or prefixing every command with sudo, because their first attempt to open /dev/i2c-1 or /sys/class/leds/ACT/brightness returned Permission denied.
Running a network-connected gateway application as root breaks the principle of least privilege:12 if an attacker exploits a buffer overflow, an MQTT parsing bug, or a command-injection flaw in a daemon running as root, the attacker controls the whole system, including the kernel, every cryptographic key on the board, and every connected actuator.
In this section, we examine how Linux enforces multi-user permissions, how hardware device nodes are granted to non-root accounts via hardware groups (gpio, i2c, spi, dialout) and udev rules, and how Linux capabilities (setcap) replace the older practice of setuid-root binaries.
Users, root, and sudo (/etc/sudoers.d/)
Section titled “Users, root, and sudo (/etc/sudoers.d/)”Every process and file in Linux is owned by a numeric User ID (UID) and Group ID (GID), mapped to human-readable names in /etc/passwd and /etc/group (with salted password hashes protected inside /etc/shadow, readable only by root).3
root(UID 0,GID 0): The superuser account, which bypasses traditional file permission checks and can load kernel modules, reformat disks, or reconfigure network interfaces. On Raspberry Pi OS and Debian, therootaccount has no password set (it is locked by default), so nobody can log in directly asrootover SSH or the console.- Administrative user (
UID 1000): The account you created in Raspberry Pi Imager (e.g.,derek), which belongs to thesudogroup.4 - Dedicated service accounts (
UID < 1000): System daemons (mosquitto,avahi, and thesensorsvcaccount we will create in Lab 2) run under dedicated, locked system accounts with/usr/sbin/nologinas their shell. They have no interactive login password and cannot runsudo.5
How sudo and /etc/sudoers.d/ work
Section titled “How sudo and /etc/sudoers.d/ work”When you prefix a command with sudo, the /usr/bin/sudo binary checks the policy defined in /etc/sudoers and the drop-in directory /etc/sudoers.d/. Never edit /etc/sudoers directly with nano; a syntax error will lock you out of sudo completely. Always use sudo visudo (or create a validated drop-in file with sudo visudo -f /etc/sudoers.d/my-policy), which validates syntax before saving:6
# Standard rule inside /etc/sudoers (note: usernames are case-sensitive!)# user hosts=(run_as_user:run_as_group) commandsroot ALL=(ALL:ALL) ALL%sudo ALL=(ALL:ALL) ALL(Note: On a new installation of Raspberry Pi OS 6.2 (April 2026) or later, sudo asks for your password, as it does on Debian.7 Earlier images created /etc/sudoers.d/010_pi-nopasswd, which let the first user run sudo with no password, and a system upgraded from one of those images keeps that file. On a gateway deployed in the field, delete it so that escalating to root requires authentication).
POSIX file permissions, chmod, chown, and umask
Section titled “POSIX file permissions, chmod, chown, and umask”When you run ls -l on /dev/ or your home directory, the first 10 characters encode the file type and the three 3-bit permission triads (Owner / User, Group, and Others):
crw-rw---- 1 root i2c 89, 1 Nov 7 12:00 /dev/i2c-1│├─┤├─┤├─┤ ├──┘ ├──┘│ │ │ │ │ └── Group owner: "i2c"│ │ │ │ └─────── User owner: "root"│ │ │ └───────────── Others permissions: --- (0: no access)│ │ └──────────────── Group permissions: rw- (4+2 = 6: read & write)│ └─────────────────── User permissions: rw- (4+2 = 6: read & write)└───────────────────── File type: 'c' = Character device ('-' regular, 'd' directory, 'l' symlink, 'b' block device)Each triad sums three octal weights: Read (r = 4), Write (w = 2), and Execute / Search directory (x = 1).8 You can modify permissions using chmod in either octal mode or symbolic mode, and change ownership with chown:
# Octal mode: Owner=rwx (7), Group=r-x (5), Others=--- (0)derek@rpi:~ $ chmod 750 deploy_script.sh
# Symbolic mode: add group write (g+w), remove all permissions for others (o-rwx)derek@rpi:~ $ chmod g+w,o-rwx sensor_config.json
# Change the user owner to root and the group owner to gpio:derek@rpi:~ $ sudo chown root:gpio sensor_config.jsonDefault permissions for new files: umask
Section titled “Default permissions for new files: umask”When your C++ program creates a new log file with std::ofstream or open(..., O_CREAT, 0666), why does the file end up on disk with permissions 0644 (-rw-r--r--) instead of 0666 (-rw-rw-rw-)?
Every Linux process inherits a User File-Creation Mode Mask (umask). A service started by systemd has a umask of 0022.9 (A login shell on Trixie uses 0002, because each user has a private group, so files that you create by hand are group-writable.) The kernel strips away any permission bits set in the umask (actual = requested & ~umask):10
- For a new regular file (base
0666):0666 & ~0022 = 0644(-rw-r--r--). - For a new directory (base
0777):0777 & ~0022 = 0755(drwxr-xr-x). - In a
systemdunit file (Section 2.6), you can setUMask=0027so that any telemetry files created by your service are never readable byothers(0640).
The sticky bit (t) on /tmp
Section titled “The sticky bit (t) on /tmp”If you run ls -ld /tmp, you will see permissions drwxrwxrwt (1777). Because /tmp is world-writable (rwx for everyone), what stops User A from deleting User B’s temporary files inside /tmp? The trailing t is the Sticky Bit (1000): when set on a directory, any user can create files inside it, but only the file’s owner, the directory’s owner, or root can delete or rename a file.8
Hardware access groups and custom udev rules
Section titled “Hardware access groups and custom udev rules”Look back at /dev/i2c-1 above: crw-rw---- 1 root i2c. Why can your derek user run i2cdetect -y 1 without sudo? Run the groups (or id) command to see which supplementary groups your account belongs to:
derek@rpi:~ $ groupsderek adm dialout cdrom sudo audio video plugdev games users input render netdev gpio i2c spiNotice the four hardware groups at the end of that list (Table 2.8).
Table 2.8. Standard hardware device access groups on Raspberry Pi OS.
| Linux Group | Device Nodes Controlled | Hardware Interfaces |
|---|---|---|
gpio | /dev/gpiochip0 | 40-pin GPIO lines (libgpiod v2). It does not own the onboard LEDs, which stay root:root unless you add a udev rule (below). |
i2c | /dev/i2c-1 | The I²C bus on Pins 3 and 5 (DS3231 RTC, ADXL345, BME280). |
spi | /dev/spidev0.0, /dev/spidev0.1 | SPI buses (MCP3208 ADC, MCP2515 CAN controller). |
dialout | /dev/serial0, /dev/ttyUSB0, /dev/ttyACM0 | Onboard UART serial ports and USB-to-serial / ESP32 USB-JTAG adapters. |
When you create a dedicated non-root user for a gateway service (as we will do in Lab 2), you add that service user to the specific hardware groups it needs using usermod -aG:
# Create a locked system user "sensorsvc" and grant it access ONLY to GPIO and I2C:derek@rpi:~ $ sudo useradd --system --no-create-home --shell /usr/sbin/nologin sensorsvcderek@rpi:~ $ sudo usermod -aG gpio,i2c sensorsvcHow /dev and /sys get their permissions: udev rules
Section titled “How /dev and /sys get their permissions: udev rules”Device nodes in /dev/ and virtual files in /sys/ do not persist on the microSD card; they are created in RAM dynamically at boot (or when a USB device is hot-plugged) by the kernel and systemd-udevd.11
How does udev know that /dev/i2c-1 should belong to group i2c with mode 0660? It reads rules in /usr/lib/udev/rules.d/ (installed by packages, such as /usr/lib/udev/rules.d/99-com.rules on Raspberry Pi OS) and in /etc/udev/rules.d/ (your own rules, which take precedence).11
Raspberry Pi OS has no stock rule for the onboard LEDs, so /sys/class/leds/ACT/trigger and brightness are writable only by root. To let the gpio group write to them, add your own rule in /etc/udev/rules.d/99-gateway-leds.rules:
# Allow members of the 'gpio' group to control onboard LED triggers and brightness without root:SUBSYSTEM=="leds", ACTION=="add|change", RUN+="/bin/chgrp -R gpio /sys%p", RUN+="/bin/chmod -R g=u /sys%p"A second rule file gives a USB device a fixed name. This one matches a specific FTDI USB-to-RS-485 adapter (VID 0403, PID 6001):
SUBSYSTEM=="tty", ATTRS{idVendor}=="0403", ATTRS{idProduct}=="6001", GROUP="dialout", MODE="0660", SYMLINK+="rs485_bus"A rule does nothing until an event arrives for its device. Reload the rules and replay an event for the LEDs without rebooting:
derek@rpi:~ $ sudo udevadm control --reload-rulesderek@rpi:~ $ sudo udevadm trigger --subsystem-match=ledsThe ACTION=="add|change" match matters for two reasons. First, udevadm trigger sends change events by default,12 so a rule that matches only add is not applied until the next boot. Second, when a program writes timer to trigger, the kernel creates delay_on and delay_off as root:root and sends a change event, and the rule must run again to give those new files to the gpio group.
Figure 2.15 follows one rule from the kernel’s event to the resulting permissions.
From uevent to permissions: how a udev rule is applied
Choose a device, edit the rule, then press an event and follow the four steps.
# /etc/udev/rules.d/99-gateway-leds.rules SUBSYSTEM=="leds", ACTION=="add|change", RUN+="/bin/chgrp -R gpio /sys%p", RUN+="/bin/chmod -R g=u /sys%p"
GROUP= and MODE= set the group and mode of a device node in /dev. The LED has no device node, only attribute files in /sys, so this rule uses RUN+= to call chgrp and chmod on the device's sysfs directory (%p is the DEVPATH). Raspberry Pi OS has no stock rule that gives the gpio group the LEDs.
No event yet. The files below are as the kernel created them, before any rule has run.
Waiting for an event.
-rw-r--r-- 1 root root … /sys/class/leds/ACT/trigger -rw-r--r-- 1 root root … /sys/class/leds/ACT/brightness
- ✕ Permission denied (EACCES): write
/sys/class/leds/ACT/trigger. sensorsvc is not in group root, and "other" has r--. - ✕ Permission denied (EACCES): write
/sys/class/leds/ACT/brightness. sensorsvc is not in group root, and "other" has r--.
Not modelled: rule file ordering details and GOTO labels, ENV and TAG keys and systemd device units, the difference between ATTR and ATTRS parent matching, and udevadm test. systemd-udevd reads /usr/lib/udev/rules.d/ and /etc/udev/rules.d/ together in lexical order of file name; this figure follows one rule.
Figure 2.15. One udev rule followed from the kernel’s uevent to the result. Every match key must match before the assignment keys are applied, and a rule does nothing until an event arrives for its device.
Try this:
- For the ACT LED, select
ACTION=="add", pressudevadm control --reload-rules, thenudevadm trigger. Neither changes the files: the first sends no event and the second sendsACTION=change. Then switch toACTION=="add|change"and pressudevadm triggeragain. - With
ACTION=="add", press Boot, then Write timer to trigger.delay_onanddelay_offappear asroot:root 0644, andsensorsvcis denied. Switch to"add|change"and writetimeragain. - For the USB RS-485 adapter, choose the CP2102 and plug it in. The node is
/dev/ttyUSB0, and opening/dev/rs485_busgivesNo such file or directory, because the vendor and product IDs do not match. Unplug it, choose the FTDI adapter and plug it in to get the symlink.
The figure does not model rule file ordering, GOTO labels, ENV and TAG keys, or udevadm test.
Why Linux capabilities (setcap) replace setuid-root
Section titled “Why Linux capabilities (setcap) replace setuid-root”What if your non-root C++ gateway application needs to perform an operation restricted by the kernel, such as binding a web dashboard to TCP port 80 or 443 (ports below 1024 are privileged)13, opening a raw ICMP ping socket, or scheduling a real-time SCHED_FIFO thread (Chapter 12)?
Historically, UNIX programmers used the Set-User-ID (setuid) bit: changing the binary’s owner to root (sudo chown root:root myapp) and setting the setuid bit (sudo chmod u+s myapp, which displays as -rwsr-xr-x). When any user executed that binary, the process ran with full root (UID 0) power. (Note: Linux ignores the setuid bit on interpreted #! shell or Python scripts14 because of race-condition vulnerabilities).
Setting setuid-root (chmod u+s) on a network-facing binary is a serious security risk: if an attacker compromises a setuid-root web server, they gain unrestricted UID 0 privileges over the entire operating system.
Linux capabilities (setcap and AmbientCapabilities=)
Section titled “Linux capabilities (setcap and AmbientCapabilities=)”The kernel divides the power of root into about 40 Linux capabilities (man 7 capabilities). They were introduced in Linux 2.2, and since Linux 2.6.24 a capability can be attached to an executable file.13 Instead of making a binary setuid-root, you run it as an unprivileged user and grant only the single capability it requires, either on the file with setcap or in its systemd unit with AmbientCapabilities= (Table 2.9).
Table 2.9. setuid-root compared with Linux capabilities (setcap and systemd).
| Approach | Command / Configuration | Privilege Granted if Compromised |
|---|---|---|
Legacy setuid-root(avoid) | sudo chown root:root ./my_gateway_httpsudo chmod u+s ./my_gateway_http (4755) | Full unrestricted root (UID 0): attacker can read /etc/shadow, load rootkit kernel modules, or wipe /dev/mmcblk0. |
setcap on the binary(programs started by hand) | sudo setcap 'cap_net_bind_service=+ep' ./my_gateway_http(Verify with: getcap ./my_gateway_http) | Only CAP_NET_BIND_SERVICE: the process runs as an unprivileged user and can bind to ports < 1024, with no other root privilege. File capabilities are ignored when a service runs with NoNewPrivileges=true.9 |
systemd unit(recommended for services) | Add AmbientCapabilities=CAP_NET_BIND_SERVICE (or CAP_SYS_NICE for real-time threads) to /etc/systemd/system/my-app.service | The same single capability, granted by systemd to that service only. It works with NoNewPrivileges=true and needs no change to the binary file.9 |
Figure 2.16 applies these rules the way the kernel does. Choose who runs the program and what it tries to do, and follow which check decides.
Can the service do it? The kernel's permission check
Choose who runs the program and what it tries to do.
- 1Not the owner (root).
- 2In group i2c: the group triad decides.
- 3Needs read and write; group has rw-.
If this process were taken over, the attacker gets: Only its own files, the devices of groups gpio and i2c.
Figure 2.16. The kernel’s permission check for a gateway service. Only one triad (owner, group or other) is used for each file,15 root bypasses the file permissions entirely, and binding a port below 1024 needs CAP_NET_BIND_SERVICE rather than full root.
🧩 Knowledge Check
Section titled “🧩 Knowledge Check”Match the hardware group to the devices it gives access to
A service creates a file with open(..., O_CREAT, 0666), and its systemd unit sets UMask=0027. What permissions does the new file get?
A file is owned by sensorsvc:gpio with mode 0407 (-r----rwx). The sensorsvc service tries to write to it. What happens?
Which of the following statements about sudo and service accounts on Raspberry Pi OS are correct?
Footnotes
Section titled “Footnotes”-
J. H. Saltzer and M. D. Schroeder, “The Protection of Information in Computer Systems,” Proceedings of the IEEE, vol. 63, no. 9, pp. 1278–1308, Sept. 1975, doi:
10.1109/PROC.1975.9939. ↩ -
NIST Computer Security Resource Center, Glossary: least privilege (definitions from NIST SP 800-53 Rev. 5 and CNSSI 4009-2022). Available: csrc.nist.gov/glossary/term/least_privilege. ↩
-
Linux man-pages project, shadow(5): shadowed password file, man-pages 6.19. Available: man7.org/linux/man-pages/man5/shadow.5.html. ↩
-
Debian Wiki, sudo. Available: wiki.debian.org/sudo. ↩
-
Debian, Debian Policy Manual, v4.7.4.1, sec. 9.2.2, “UID and GID classes”. Available: debian.org/doc/debian-policy/ch-opersys.html. ↩
-
Sudo project, visudo(8): edit the sudoers file. Available: sudo.ws/docs/man/visudo.man. ↩
-
S. Long, “A security update for Raspberry Pi OS,” Raspberry Pi News, 14 Apr. 2026. Available: raspberrypi.com/news/a-security-update-for-raspberry-pi-os. ↩
-
Linux man-pages project, inode(7): file inode information, man-pages 6.19. Available: man7.org/linux/man-pages/man7/inode.7.html. ↩ ↩2
-
systemd project, systemd.exec(5): execution environment configuration (
UMask=,NoNewPrivileges=,AmbientCapabilities=), systemd 262. Available: freedesktop.org/software/systemd/man/latest/systemd.exec.html. ↩ ↩2 ↩3 -
Linux man-pages project, umask(2): set file mode creation mask, man-pages 6.19. Available: man7.org/linux/man-pages/man2/umask.2.html. ↩
-
systemd project, udev(7): dynamic device management. Available: man7.org/linux/man-pages/man7/udev.7.html. ↩ ↩2
-
systemd project, udevadm(8): udev management tool. Available: man7.org/linux/man-pages/man8/udevadm.8.html. ↩
-
Linux man-pages project, capabilities(7): overview of Linux capabilities, man-pages 6.19. Available: man7.org/linux/man-pages/man7/capabilities.7.html. ↩ ↩2
-
Linux man-pages project, execve(2): execute program, man-pages 6.19. Available: man7.org/linux/man-pages/man2/execve.2.html. ↩
-
Linux man-pages project, path_resolution(7): how a pathname is resolved to a file, section “Permissions”, man-pages 6.19. Available: man7.org/linux/man-pages/man7/path_resolution.7.html. ↩
© 2026 Derek Molloy, Dublin City University. All rights reserved.