Stage 03 · Lesson 04/14

Linux & Dev Environment · Lesson 04

Linux Permissions & Users

Est. 40–50 min

Why

Why “Permission denied” is the first wall every robot builder hits

You will meet this on day one: you plug in the arm’s USB cable, run your Python script and get PermissionError: [Errno 13] Permission denied: '/dev/ttyUSB0'. The fix is not sudo — it is understanding who owns the port and which group you need to join.

Concept

The big idea: owner, group, others × read, write, execute

Every file and device has an owner (a user), a group, and nine permission bits: read / write / execute for each of user, group and others.

Reading one line of ls -l · Text
-rwxr-x---  1  ashish  robotics  64  Oct 13 10:15  bringup.sh
│└┬┘└┬┘└┬┘     └─┬──┘  └──┬───┘
│ │  │  │        owner    group
│ │  │  └ others: ---  (nothing)
│ │  └ group:  r-x  (read + run)
│ └ user:   rwx  (read + write + run)
└ type: -  file   d  directory   c  character device (serial, camera)
What r, w, x mean
BitValueOn a fileOn a folder
r4Read the contents (cat)List names inside (ls)
w2Change the contentsCreate / delete files inside
x1Run it as a program (./bringup.sh)Enter it (cd)

digit = r(4) + w(2) + x(1) e.g. rwx = 7, r-x = 5, rw- = 6, r-- = 4

  • Three digits = user, group, others
  • chmod 750 bringup.sh → rwx r-x ---
  • chmod u+x file — symbolic way: give the user execute. g-w removes group write, o= clears others.
  • chown ashish:robotics file — change owner and group (needs sudo).
  • groups — which groups you belong to. id — the same with numbers.
  • sudo command — run one command as root. Powerful, logged, and easy to misuse.

How it works

How it actually works: how Linux decides “allowed” or “denied”

  1. Who are you?

    Your process carries your user id and your list of groups (id shows them).

  2. Are you the owner?

    If yes, Linux uses only the user bits — and stops there.

  3. Else: are you in the file’s group?

    If yes, it uses only the group bits.

  4. Else: you are “others”

    It uses the others bits.

  5. Check the action

    Reading needs r, writing needs w, running needs x. Missing bit → Permission denied.

Visual

Visualise it: file permission lab (rwx · octal · chmod)

Drag the user / group / other sliders (0–7) or tap r, w, x. The octal number, ls -l string and chmod command update together. Then pick who is knocking and see which row Linux uses.

Interactive · File permission lab (rwx · octal · chmod)

File: bringup.sh · owner ashish · group robotics. Drag a slider or tap the r/w/x keys, then choose who is trying to use the file.

octal

644

ls -l shows

-rw-r--r--

$ chmod 644 bringup.sh
$ ls -l bringup.sh
-rw-r--r-- 1 ashish robotics 64 Oct 13 10:15 bringup.sh

Who is knocking?

Linux checks: owner? → group member? → everyone else. For priya it uses the group bits: r--

  • read (cat / open)✔ ALLOWED
  • write (edit / send bytes)✗ Permission denied
  • execute (./run it)✗ Permission denied

Worked example

Worked example: the serial port that said no

Neha plugs in a CH340 USB-serial Arduino clone. Here is her debugging session on Ubuntu:

debug_serial_permission.sh (run on a real Ubuntu machine) · Shell
ls -l /dev/ttyUSB0
# crw-rw---- 1 root dialout 188, 0 Oct 13 10:15 /dev/ttyUSB0
#  owner root: rw   group dialout: rw   others: ---

groups
# neha adm cdrom sudo plugdev        <- no 'dialout', so Neha is "others"

sudo usermod -aG dialout $USER       # -a = append, -G = to this group
# now LOG OUT and log back in (or reboot) so the new group applies

groups
# neha adm cdrom sudo dialout plugdev  <- fixed, no sudo needed for Python
NoteThe exact numbers and date on your machine will differ. On many systems the group is dialout; on Arch-based distros it is often uucp. The serial_ports.py script below reads the real group from the device itself.

Code lab

Code lab: read, change and test permissions

Run bash perms_demo.sh. It creates two files in /tmp/aihub-lab/perms-demo, shows their modes with stat, fails to run the script, fixes it with chmod u+x, and locks the calibration file with 600.

perms_demo.sh · Shell
#!/usr/bin/env bash
# perms_demo.sh — read, change and test permissions (Lesson 4)
# Safe: works only inside /tmp/aihub-lab/perms-demo
mkdir -p /tmp/aihub-lab/perms-demo && cd /tmp/aihub-lab/perms-demo
printf '#!/usr/bin/env bash\necho "bring-up script running"\n' > bringup.sh
echo "joint1_offset_deg: 2.5" > calib.yaml
chmod 644 bringup.sh calib.yaml
stat -c '%A  %a  %n' bringup.sh calib.yaml
./bringup.sh 2>/dev/null || echo "./bringup.sh -> Permission denied (no x bit yet)"
chmod u+x bringup.sh          # give the owner the execute key
chmod 600 calib.yaml          # only the owner may read/write calibration
stat -c '%A  %a  %n' bringup.sh calib.yaml
./bringup.sh

Expected output

-rw-r--r--  644  bringup.sh
-rw-r--r--  644  calib.yaml
./bringup.sh -> Permission denied (no x bit yet)
-rwxr--r--  744  bringup.sh
-rw-------  600  calib.yaml
bring-up script running

Now a Python helper you will reuse with every Arduino. It finds USB serial ports and tells you whether you can open them — and the exact fix if not.

serial_ports.py · Python
# serial_ports.py — find USB serial devices and check if YOU may open them (Lesson 4)
import glob
import grp
import os

ports = sorted(glob.glob("/dev/ttyUSB*") + glob.glob("/dev/ttyACM*"))
if not ports:
    print("No USB serial ports found. Plug in the Arduino/ESP32 and run again.")

for port in ports:
    group = grp.getgrgid(os.stat(port).st_gid).gr_name
    can_rw = os.access(port, os.R_OK | os.W_OK)
    print(f"{port}: group={group}, you can read+write: {can_rw}")
    if not can_rw:
        print(f"  fix: sudo usermod -aG {group} $USER   (then log out and log in)")

Output with no board plugged in (with a board you will see one line per port)

No USB serial ports found. Plug in the Arduino/ESP32 and run again.

Arduino link

Try it with hardware: Arduino → Linux serial

If you have an Arduino Uno / Nano, upload this sketch from the Arduino IDE, then plug it into your Ubuntu laptop.

hello_serial.ino · Arduino C++
// hello_serial.ino — tiny Arduino sketch to test the Linux serial link (Lesson 4)
void setup() {
  Serial.begin(115200);          // must match the baud rate on the Linux side
}

void loop() {
  Serial.println("AIHUB arm ready");
  delay(1000);                   // one line every second
}
find_and_read_port.sh · Shell
ls /dev/ttyUSB* /dev/ttyACM*        # genuine Uno → ttyACM0, CH340 clones → ttyUSB0
sudo dmesg | tail -n 5               # kernel message names the new port
python3 serial_ports.py              # can you read+write it?
pip install pyserial                 # inside your venv (Lesson 11)
python3 -m serial.tools.miniterm /dev/ttyACM0 115200   # Ctrl+] to quit

What you should see in miniterm (one line per second)

AIHUB arm ready
AIHUB arm ready
AIHUB arm ready

Real world

Where permissions bite in the robot lab

  • Serial ports (/dev/ttyUSB0, /dev/ttyACM0) — usually group dialout.
  • Cameras (/dev/video0) — usually group video.
  • Scripts in scripts/ need x to run as ./bringup.sh.
  • SSH private keys must be 600 — the SSH client refuses keys that others can read (Lesson 13).
  • udev rules in /etc/udev/rules.d/ can give a device a fixed name and group — a later, more advanced fix.

Mistakes

Common mistakes and debugging

Hardware & safety

Hardware lab and safety notes

The most common Linux problem in a robot lab is “Permission denied” on the Arduino port. This is where you fix it properly.

  • Use the hello_serial.ino sketch above (it compiles for Arduino Uno: 1,638 bytes).
  • Fix access with sudo usermod -aG dialout $USER, then log out and back in — not with sudo python3 or chmod 777.
  • Only one program can hold a serial port at a time: close the Arduino IDE Serial Monitor before running Python.
Arduino Uno R3 board
Arduino Uno — its serial port /dev/ttyACM0 belongs to the dialout group.Photo: SparkFun Electronics · CC BY 2.0 · Source: Wikimedia Commons · resized, converted to WebP

Debugging

Debugging: when it doesn’t work

Symptom → likely cause → fix
SymptomLikely causeFix
Permission denied: '/dev/ttyACM0'You are not in the dialout groupsudo usermod -aG dialout $USER, then log out and in
Still denied after usermodGroup change not applied yetLog out fully (or reboot); check with groups
./bringup.sh: Permission deniedScript has no execute bitchmod u+x bringup.sh
Device or resource busySerial Monitor or another script holds the portClose the other program; fuser /dev/ttyACM0 shows who
NoteRead the last line of an error first — it usually names the problem. Change one thing at a time and re-run.

Think

Think about it

Think

Why is `sudo python3 robot.py` a bad habit even if it “works”?

Experiment

Try it in the lab: be the watchman

Use the permission lab. For each step, predict ALLOWED / denied before you click.

Experiment

Be the watchman

You will need

  • Permission lab
  • Notebook

Steps

  1. Click the 644 preset. As `guest`, can you run the script? Can you read it?
  2. Click 755. Now can `guest` run it? Write the ls -l string you see.
  3. Click 660 (serial-port style). Select `priya`, then untick “priya is in the group”. What changes, and why?
  4. Set user = 0, group = 4, others = 4 (mode 044). Select the owner `ashish`. Can he read his own file?
  5. Click 777 and read the warning. Write one sentence on why 750 is better for a bring-up script.
  6. On a real Ubuntu machine, run `perms_demo.sh` and `serial_ports.py`.

Interactive · File permission lab (rwx · octal · chmod)

File: bringup.sh · owner ashish · group robotics. Drag a slider or tap the r/w/x keys, then choose who is trying to use the file.

octal

644

ls -l shows

-rw-r--r--

$ chmod 644 bringup.sh
$ ls -l bringup.sh
-rw-r--r-- 1 ashish robotics 64 Oct 13 10:15 bringup.sh

Who is knocking?

Linux checks: owner? → group member? → everyone else. For priya it uses the group bits: r--

  • read (cat / open)✔ ALLOWED
  • write (edit / send bytes)✗ Permission denied
  • execute (./run it)✗ Permission denied

The highlighted row is the only one that counts. Being the owner with no owner bits means denied, even if others are allowed.

Knowledge check

Check your understanding

Knowledge check

1. Permission denied on a serial device usually means…

2. The best long-term fix is…

3. `ls -l` helps you see…

4. `chmod 750 bringup.sh` gives…

5. Why must you use `usermod -aG` and not `usermod -G`?

Mini assignment

Hands-on assignment

Mini assignment

Permissions memo

Write a half-page memo for your lab team: how to fix serial and camera access the right way.

Deliverables

  • Explain user / group / others with the watchman analogy
  • A table of 5 robot files with the mode you would give each (octal + rwx) and why
  • The exact commands to diagnose and fix `/dev/ttyUSB0` access

Do this, step by step

  1. In the permission lab, set bringup.sh to 750 and read the rwx string aloud.
  2. Run perms_demo.sh and match each ls -l line with its octal number.
  3. If you have an Arduino: upload hello_serial.ino, run ls -l /dev/ttyACM0 and groups, and fix access the right way.
  4. Write your memo with a 5-file table (octal + rwx + reason).

Stuck? Hints, one at a time

NoteTry the task first. Read one hint, try again, and only then read the next one.
  1. Hint 1: r = 4, w = 2, x = 1. Add them per group: rwx = 7, r-x = 5, --- = 0.
  2. Hint 2: The serial port line looks like crw-rw---- root dialout — so group members can read and write.
  3. Hint 3: -a in usermod -aG means append; without it you lose your other groups.

Challenges (pick by level)

  • 🟢 Easy: Make a secrets.env readable only by you (600).
  • 🟡 Medium: Read hello_serial output with python3 -m serial.tools.miniterm at 115200 baud.
  • 🟠 Hard: Find your camera’s group with ls -l /dev/video0 and explain how to give a user access.

Next step

Look back: Lesson 3: Files & Directories. Next: Lesson 5: Processes & System Monitoring. Related: Stage 0 — Power & Communication and Stage 4 — Electronics.

Sources

Self-attested for now — keep artifacts in your lab notebook.

Complete

Today's goal

0 of 1 done today

0-day streak
Daily goal:lessons, quizzes or assignments a day

Mark this lesson complete when you have worked the experiment, knowledge check, and assignment (even roughly).

Stuck somewhere?

Ask a doubt about this lesson

Saved with this lesson's id and your email so we can reply. Privacy · or email contact@aihubstation.in