Skip to content

Try “polymorphism”, “TCP handshake” or “git rebase”.

    Computer Basics / The Linux Command Line

    Permissions

    Every file has an owner (a user) and a group (a named set of users).

    Chapter 4 of 125 min read

    The Plain Picture#

    An office document has three kinds of people: the owner, people in the owner's team, and everyone else. For each kind you decide: may they read it, change it, run it (if it's a program)?

    The Real Names#

    • Every file has an owner (a user) and a group (a named set of users).

    • There are three permission bits for each of three classes:

      -rwxr-xr--
       └┬┘└┬┘└┬┘
        │  │  └─ others:  r--   read only
        │  └──── group:   r-x   read and execute
        └─────── owner:   rwx   read, write, execute
    LetterOn a fileOn a directory
    r readview the contentslist the names inside
    w writechange the contentscreate, delete or rename files inside
    x executerun it as a programenter it (cd) and reach files inside
    • root is the superuser (user ID 0). Permissions don't apply to root — it can read, change or delete anything.

    • sudo ("superuser do") runs one command as root, after checking you're allowed: sudo systemctl restart nginx.

    Under the Hood — numbers#

    Each class's three bits form a binary number (How Computers Work Ch 2): r = 4, w = 2, x = 1.

    Output
      rwx = 4+2+1 = 7        r-x = 4+0+1 = 5        r-- = 4        rw- = 4+2 = 6        --- = 0

    So three digits describe everything:

    NumberLettersTypical use
    755rwxr-xr-xprograms, scripts, directories — everyone may run/enter, only the owner may change
    644rw-r--r--normal files — everyone reads, only the owner writes (your access.log above had exactly this)
    600rw-------secrets: SSH private keys, .env files — only the owner
    700rwx------a private directory, e.g. ~/.ssh
    777rwxrwxrwxeveryone can do everything — almost always a mistake
    Terminal
    chmod 600 ~/.ssh/id_ed25519       # set by number
    chmod +x deploy.sh                # add execute for everyone (letters form)
    chmod u+x,g-w file                # owner +execute, group −write
    chown priya:devs report.txt       # change owner and group (needs sudo unless you're root)
    id                                # who am I, and which groups am I in?

    Default permissions: new files get their permissions from a mask called umask. The usual umask is 022, meaning "remove write for group and others", so new files come out as 644 and new directories as 755.

    Gotchas

    • "Permission denied" on a script you just wrote → it isn't executable: chmod +x script.sh.

    • SSH refuses a private key with "UNPROTECTED PRIVATE KEY FILE" if its permissions are too open → chmod 600.

    • chmod -R 777 to "fix" a permission error is the classic dangerous shortcut: now any user or compromised process can modify your files. Find the real owner/permission problem instead.

    • Don't run your app as root. If it's hacked, the attacker becomes root. Run it as its own low-privilege user (principle of least privilege, Networking 2: TCP, UDP and Ports Ch 11). This matters again inside Docker (Docker (coming soon)).

    Check yourself

    4 questions
    1

    What does chmod 754 run.sh allow for owner, group and others?

    Show answer

    Owner rwx (7), group r-x (5), others r-- (4).

    2

    What permissions should an SSH private key have?

    Show answer

    600 — read/write for the owner only.

    3

    On a directory, what does the x bit allow?

    Show answer

    Entering it and accessing the files inside.

    4

    Why is running your web app as root dangerous?

    Show answer

    If the app is compromised, the attacker gets full control of the whole machine.