Curious Pandas

Who may touch this file

Nine little letters decide who can read, change and run every file. Learn to read them, grant exactly one missing bit, and see why chmod 777 is never the fix.

15 min+35 XP for finishing

Linux was built for many people sharing one machine, and every file still carries the answer to one question: who may do what to me? ls -l shows it as ten characters at the start of each line:

-rwxr-xr--  1 panda dev  64 Oct 30 09:00 deploy.sh

The first character is the type (- a file, d a directory). The next nine are three groups of three, for three kinds of people:

Letters Who In this example
rwx the owner (panda) read, write and run it
r-x the file’s group (dev) read and run, not change
r-- everyone else read only

Each letter is a bit: r = 4, w = 2, x = 1. Add them per group and you get the three digits people type: rwxr-xr-- is 754.

Visual guideWhich three bits apply to you
Which three bits apply to youWhen you open a file, Linux first asks whether you own it; if so, the owner bits decide and nothing else is consulted. If not, it asks whether you are in the file's group; if so, the group bits decide. Otherwise the bits for everyone else decide. Only one group of three bits is ever used.you open a fileread, write or rundo you own it?owner bitsand only thosein its group?group bitsother bitseveryone elseyesnoyesnoWhich three bits apply to youWhen you open a file, Linux first asks whether you own it; if so, the owner bits decide and nothing else is consulted. If not, it asks whether you are in the file's group; if so, the group bits decide. Otherwise the bits for everyone else decide. Only one group of three bits is ever used.you open a fileread, write or rundo you own it?owner bitsand only thosein its group?group bitsother bitseveryone elseyesnoyesno
  1. Every open, write or run starts the same check.
  2. If you own the file, the owner bits decide - and that is the end of it. If the owner has no r, the owner cannot read, even when everyone else can.
  3. Not the owner? If you belong to the file's group, the group bits decide.
  4. Otherwise, the bits for everyone else. Exactly one group of three is ever used.

Grant exactly what’s missing

chmod changes the bits, either as digits (chmod 754 deploy.sh) or as a change to make: who (u owner, g group, o others, a all), then +, - or =, then the letters. chmod u+x deploy.sh adds one bit, run-for-the-owner, and touches nothing else.

A script that will not runa toy Linux in your browser
You are here

    Check yourself+10 XP

    A file is -r--r--rw- and you own it. Can you write to it?

    Folders have the same bits, with different meanings

    On a directory, r lets you list the names in it, w lets you create, delete and rename entries in it, and x lets you go into it and reach anything inside.

    That gives one surprise worth remembering: deleting a file needs write permission on the folder, not on the file. A read-only file in your own folder can still be deleted by you. A file you could rewrite completely can’t be deleted if you lack write permission on its folder.

    Check yourself+10 XP

    Only who can chmod a file, whatever its current bits say?

    Do it for real · your machine+20 XP bonus

    Everything above ran in your browser. Now try it in a real terminal: any Linux machine, a Mac's Terminal app, or Windows with WSL. Nothing to install beyond that.

    On Linux, a Mac or WSL:

    printf 'echo it runs\n' > hello.sh
    ls -l hello.sh        # -rw-r--r-- (or -rw-rw-r--): no x, nobody may run it
    ./hello.sh            # Permission denied
    echo $?               # 126: found, but not executable
    bash hello.sh         # runs: bash only needs to READ the file
    chmod u+x hello.sh && ./hello.sh
    ls -l /etc/shadow     # Linux/WSL: password hashes, unreadable to you
    Self-reported. We can't see your machine, and we don't want to. This is a bonus, never required.

    Sources

    Finished reading?

    Missions and quizzes above earn their own XP. This marks the lesson done.