Skip to content
Tutorials Center
My learning

Topics

Lesson 20 of 37 ยท Linux on the Pi

Users, Groups and Permissions on the Pi

Understand Linux accounts on the Pi: the gpio, i2c and spi groups, sudo, file modes like 640 and 755, and why hardware scripts should not run as root.

Every file and every process on a Pi belongs to a user. Permissions decide who may read, change or run something. They are the reason a script that works with sudo fails without it, and the reason "just run everything as root" is a bad fix. This lesson explains the model so you can fix permission errors properly.

On Raspberry Pi OS you choose a username during setup (older guides assume pi). Hardware access is not a special power of root. It is controlled through groups: members of gpio may use the GPIO pins, members of i2c may open the I2C bus, and so on. Add your user to the right groups and your Python scripts run without sudo. The shell commands here are read-only; the runnable blocks model the permission rules in Python.

Who am I?

Terminal
whoami            # your username
id                # uid, gid and every group you belong to
groups            # just the group names

A typical id line on a Pi lists groups such as sudo, video, gpio, i2c, spi, dialout and plugdev. Each exists to guard a class of device:

Group Grants access to
gpio GPIO character devices (/dev/gpiochip*)
i2c, spi /dev/i2c-* and /dev/spidev*
video Camera and display devices
dialout Serial ports such as /dev/ttyUSB0
sudo Permission to run commands as root with sudo

To add yourself to a group, then log out and in again so the change takes effect:

Terminal
sudo usermod -aG i2c,spi $USER

The -a means append. Leaving it out replaces all your groups and can lock you out of sudo, which is one of the classic Linux mistakes.

Reading a file mode

ls -l shows a mode such as -rwxr-x---. The first character is the type (- file, d directory); then three triples for the owner, the group and everyone else. Each triple is read, write, execute, which are worth 4, 2 and 1. So rwxr-x--- is 7, 5, 0, written 750.

Terminal
ls -l ~/sensor.py
chmod 750 ~/sensor.py       # owner all, group read+execute, others nothing
chmod +x ~/sensor.py        # make it runnable
sudo chown pi:pi ~/sensor.py

For directories, execute means "may enter". A directory without x for you cannot be opened even if you can list its name.

This stand-in decodes octal modes the way ls shows them, so you can practice reading them.

Python Stand-in: decode a file mode

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Checking access like the kernel does

The kernel's rule is simple: if you are the owner, the owner triple applies; otherwise, if you are in the file's group, the group triple applies; otherwise the "others" triple. The first match wins, even if it is stricter. The model below applies exactly that rule to a few files, including a device node guarded by the gpio group.

Python Stand-in: can this user open the file?

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Notice that ben can read and write /dev/gpiochip0 only because he is in gpio, and ana cannot. That is the fix for "permission denied" on GPIO: join the group, do not elevate.

sudo and root

sudo runs one command with root's rights, after checking you are listed in /etc/sudoers (members of the sudo group are). Use it for system administration: apt, editing /etc, restarting services. Do not use it for your own Python programs. A script run with sudo creates files owned by root in your home folder, which later your normal user cannot edit, and a bug in it can damage the whole system.

Terminal
sudo systemctl restart ssh      # fine: system service
python3 blink.py                # fine: gpio group is enough
sudo python3 blink.py           # avoid: runs everything as root
sudo -u www-data whoami         # run as another user for testing

If sudo asks for a password you never set, or an old tutorial tells you the default pi/raspberry login, treat it as outdated: current images have no default password and you choose it in Raspberry Pi Imager. Never expose a Pi with a guessable password to the internet.

Service accounts

Programs that run in the background, like a web server, should not use your login. Linux creates system users (for example www-data) with no password and no shell. For your own long-running project you can create one:

Terminal
sudo adduser --system --group --home /opt/weather weather
sudo adduser weather gpio

Now a service running as weather can read the sensors but cannot touch your documents or install software. Systemd, covered in the next lesson, makes the service use that account with one line.

Common permission errors

  • PermissionError: [Errno 13] on a /dev/... file: you are missing a group, or the interface is disabled in raspi-config.
  • Permission denied when running a script: the file lacks x; chmod +x or run it with python3 script.py.
  • A file you cannot edit after using sudo: fix with sudo chown $USER: file.
  • A secrets file readable by everyone: chmod 600 it.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What numeric mode does rwxr-x--- correspond to?

    rwx = 7, r-x = 5, --- = 0.
  2. 2. Your script cannot open /dev/i2c-1 and you are not root. What is the right fix?

    Group membership grants device access without giving a script full root rights.
  3. 3. What is the danger of usermod -G sudo,gpio you instead of usermod -aG ...?

    -a appends; leaving it out sets the list to exactly what you typed.
  4. 4. A file is owned by ana, group staff, mode 640. Ben is in staff. What can Ben do?

    Ben matches the group triple r-- (the 4 in 640's middle digit).

Summary

  • Permissions have three triples (owner, group, others) of read 4, write 2 and execute 1.
  • Hardware access comes from groups such as gpio, i2c, spi, video and dialout.
  • Add groups with usermod -aG, then log out and in.
  • Use sudo for administration, not for your own scripts.
  • Run long-lived programs under a dedicated service account with only the groups they need.

Next: Services with systemd