CodeTheRobot

Ubuntu Linux for Robotics · Lesson 07 of 96

File permissions: chmod, chown and sudo

Read, write and execute for user, group and others; octal modes; ownership; when sudo is right.

Estimated time · 45–60 min

Learning goals

What you will understand

  • Explain file permissions: chmod, chown and sudo in your own words
  • Use it in a small workstation example
  • Verify the result instead of assuming it worked

Understand the idea first

The mental model

Every file has an owner, a group and permissions for three classes (user, group, others), each with read (r=4), write (w=2) and execute (x=1). ls -l shows -rwxr-x--- meaning: user rwx (7), group r-x (5), others none (0), so mode 750. chmod changes the mode, chown changes owner and group.

sudo runs one command with administrator rights. It is not a fix for 'permission denied': first decide who should own the file. Use chmod +x to make a script executable and avoid chmod 777, which lets anyone change the file.

Guided lesson

Work through these steps

  1. 1.

    Open a terminal and record the current state before changing anything.

  2. 2.

    Read the file permissions: chmod, chown and sudo commands and predict what each one does.

  3. 3.

    Run the commands one at a time and compare each output with your prediction.

  4. 4.

    Write the commands that worked in a setup note you can repeat later.

Worked example

Read it before you run it

touch run.sh
# Create an empty file.
ls -l run.sh
# Show its owner and mode (-rw-r--r--).
chmod u+x run.sh
# Add execute permission for the owner only.
chmod 640 notes.txt
# Owner read/write, group read, others nothing.
stat -c '%a %U:%G' notes.txt
# Print the octal mode and owner:group.
sudo chown $USER:$USER notes.txt
# Give the file back to your user (only needed if root created it).

What the example is doing

  1. 1.Lines without sudo only inspect or change files you own; sudo authorizes a system-wide change.
  2. 2.Read command output before moving to the next line. Stop if names, versions, disks, or paths differ from the lesson.
  3. 3.Comments beginning with # explain safety checks and are not executed by the shell.

Watch it run · The robot's computer

See what this code does

The terminal types this lesson's practice solution and runs it. The scene shows the kind of robot behaviour that code drives. It plays by itself and loops.

robot computercpumemdiskdriverstoppedplannerstoppedloggerstoppedsafetystoppedTHE ROBOT'S COMPUTER

Demo for File permissions: chmod, chown and sudo. Expected result: 640

Practice code

Now type it yourself

Open a terminal, run the starter line by line (or save it as practice.sh and run bash practice.sh), and replace every TODO with your own command.

f=$(mktemp)
# TODO: set the mode of the file to 640 (owner read/write, group read)
stat -c %a "$f"

Expected output

640
Stuck? Show the full solution (try it yourself first)
f=$(mktemp)
chmod 640 "$f"
stat -c %a "$f"

Expected result

How you know it worked

  • Each command completes without an unexplained error.
  • Your notes contain the command, the important output, and what it proves.

Practical exercise

Apply the lesson

Create a private note (mode 600), a script you can run (mode 750), and a folder shared by a group. Show each mode with ls -l and stat, and explain why chmod 777 is unnecessary.

Before you continue

  • You recorded the starting state
  • Every command and output is understood
  • The change can be repeated from your notes

If something goes wrong

A command says permission denied.

Check ownership and permissions first. Do not add sudo unless the command changes a documented system location.

A command or package is not found.

Check spelling, Ubuntu version, enabled repositories, and whether the previous installation command succeeded.