Please enable JavaScript to use CodeHS

Cybersecurity AP Level

Linux File Permissions

Ten little characters at the start of every line of ls -l decide who can read your files, who can change them, and who gets shut out. Learn to read those characters, do the math behind them, and set them correctly.

11 min read AP & High School
01

Why File Permissions Exist

Linux was built from day one for machines that many people share. A school server, a web server, a lab computer, even a Raspberry Pi running a robotics club project: all of them can hold accounts for dozens of different people, plus accounts that belong to programs rather than to humans.

That raises an obvious question. If your lab partner has an account on the same server, what stops them from opening your files? The answer is access control: a set of rules the operating system checks every single time anyone tries to open, change, or run a file. On Linux those rules are called file permissions, and every file and folder on the system carries its own set.

Where this fits in security class

Cybersecurity courses organize security goals into the CIA triad:

  • Confidentiality: only the right people can read the data.
  • Integrity: only the right people can change the data, so you can trust that it says what it is supposed to say.
  • Availability: the people who need the data can actually get to it.

File permissions are the everyday tool for the first two. The read setting protects confidentiality. The write setting protects integrity. Set them too tightly and you damage availability, because people cannot do their work. Good administration means finding the setting that is exactly right, not the setting that is easiest.

You already do this

When you set a shared document to "anyone with the link can view" instead of "can edit," you are making an access control decision: read yes, write no. Linux asks the same question about every file on the machine, just with shorter names.

02

Users, Groups, and the Root Account

Before you can say who is allowed to do what, Linux needs a way to talk about "who." It uses three ideas.

Users

A user is one account on the system. Behind the scenes each account has a number called a UID (user ID); the name you type is just a friendly label for that number. Two commands tell you who the system thinks you are:

who am I?
$ whoami
maya
 
$ id
uid=1001(maya) gid=1001(maya) groups=1001(maya),1004(students),27(sudo)

Groups

A group is a named collection of user accounts. Groups exist so an administrator can say "the whole robotics team can read this folder" once, instead of naming twelve people one at a time. In the output above, maya belongs to three groups: a personal group named after her, a students group, and the sudo group.

Here is the part that matters for permissions: every file has exactly one owning user and exactly one owning group. Not a list. One of each. Everyone who is neither the owner nor a member of that group falls into a third bucket that Linux calls other, which really means "everybody else with an account on this machine."

Root, the superuser

One account is special. root is the administrator account, and it always has UID 0. Permission bits do not restrict root. If root tries to read a file, the file opens, no matter what its permissions say. That is what makes root necessary (someone has to install software and fix broken settings) and what makes root dangerous.

Root ignores the rules

Setting a file to rw------- does not hide it from root. Permissions control which normal accounts can reach a file; they are not encryption, and they do not protect a hard drive that someone physically removes and plugs into another machine.

03

Reading an ls -l Line

The command ls lists the names of the files in a folder. Add the -l flag (a lowercase L, for "long") and you get one line per file with the permissions spelled out.

bash
$ ls -l
total 12
-rw-r--r-- 1 jordan students 2048 Mar 14 09:22 lab_report.txt
drwxr-x--- 2 jordan students 4096 Mar 12 15:04 project
-rwxr-xr-x 1 jordan students 814 Mar 10 11:47 backup.sh

Everything after the highlighted part is useful but ordinary: the link count, then the owning user, the owning group, the size in bytes, the date it was last modified, and the file name. The security information is in those first ten characters.

Ten characters, four pieces

Take the middle line, drwxr-x---, and split it up:

  • dCharacter 1: the file type. A dash means an ordinary file. A d means a directory (a folder). An l means a symbolic link, which is a pointer to another file. This character is not a permission at all, so do not include it in the math later.
  • rwxCharacters 2 to 4: the user triad. What the owning user is allowed to do. Here: read, write, and execute.
  • r-xCharacters 5 to 7: the group triad. What members of the owning group are allowed to do. Here: read and execute, but not write.
  • ---Characters 8 to 10: the other triad. What every other account gets. Here: nothing at all.

The three letters always appear in the same order inside a triad: r then w then x. A dash in a slot means that permission is turned off. So r-x means read yes, write no, execute yes, and it can never mean anything else.

First match wins, and only that one counts

Linux picks exactly one triad for you and ignores the other two. If you are the owner, you get the user triad, full stop. If you are not the owner but you are in the group, you get the group triad. Otherwise you get other. The triads are never added together, which means an owner can actually have less access than the group does.

04

What r, w, and x Actually Do

Everyone learns "r is read, w is write, x is execute" in about four seconds. The part that trips people up is that those three letters mean something different on a directory than they do on a file. This is the single most common source of confusion in the whole topic, so it is worth slowing down.

On a regular file

  • r (read): you can open the file and see its contents, or copy it.
  • w (write): you can change the contents of the file.
  • x (execute): you can run the file as a program.

Two details worth knowing. First, a script (a text file of commands, like a .sh file) needs both r and x, because the shell has to read the text before it can follow it. A compiled program needs only x. Second, and this surprises everyone: deleting a file is not controlled by the file at all. It is controlled by the write permission on the directory that contains it, because deleting means removing a name from that directory's list.

On a directory

  • r (read): you can list the names inside it, which is what ls does.
  • w (write): you can create, rename, and delete entries inside it. This one only does anything if you also have x.
  • x (execute): you can enter the directory, which includes cd into it and reaching any file inside it by name. People also call this "search" or "traverse" permission.

Because these are separate switches, you can end up with odd combinations. They are worth recognizing:

r-x

The normal read-only folder. You can see the names and open the files. You cannot add or delete anything.

r--

Read without execute. You can see the file names and nothing else. Trying to open any of them fails, and ls -l cannot even show you their sizes.

--x

Execute without read. You cannot browse the folder, but if someone tells you an exact file name inside it, you can open that file. Administrators use this on purpose to let a program reach one file without exposing the whole folder.

-w-

Write without execute. Useless. You cannot create a file inside a folder you are not allowed to enter.

You own a folder called project and you run chmod u-x project, removing your own execute bit. What can you no longer do?

Execute on a directory is permission to enter it. Without it you cannot cd in and you cannot reach anything inside, even a file you own. You can still run plain ls and see the names, because that is the read bit and you still have it. Renaming the folder itself depends on the write bit of its parent folder, not on this folder at all.

05

Octal Notation and the 4/2/1 Math

Typing out rwxr-xr-x gets old fast, so Linux people usually say "755" instead. Those are the same thing. Here is how the shorthand works.

Each triad has three switches, and each switch gets a number:

  • read = 4
  • write = 2
  • execute = 1

To get the digit for a triad, add up the values of the switches that are on. Because 4, 2, and 1 are powers of two, every possible combination adds up to a different number, and every digit from 0 through 7 maps to exactly one pattern. That is all "octal" means here: base 8, the digits 0 through 7.

All eight possible triads
Digit Math Letters Meaning on a file
00---No access
11--xExecute only
22-w-Write only
32 + 1-wxWrite and execute
44r--Read only
54 + 1r-xRead and execute
64 + 2rw-Read and write
74 + 2 + 1rwxRead, write, and execute

Worked conversions

A full permission set is three digits, always in the order user, group, other.

rwxr-xr-x rwx = 4 + 2 + 1 = 7, r-x = 4 + 1 = 5, r-x = 4 + 1 = 5 755
rw-r--r-- rw- = 4 + 2 = 6, r-- = 4, r-- = 4 644
rw------- rw- = 4 + 2 = 6, --- = 0, --- = 0 600
rwxr-x--- rwx = 7, r-x = 4 + 1 = 5, --- = 0 750
rwxrwxrwx rwx = 7, rwx = 7, rwx = 7 777

It works just as well backwards. Given 640: the 6 is 4 + 2, so the owner gets rw-. The 4 is read only, so the group gets r--. The 0 is nothing, so other gets ---. Put it together and you have rw-r-----, a file the owner can edit, the group can read, and nobody else can touch.

Why 777 is almost always a mistake

777 means read, write, and execute for absolutely everyone with an account on the machine, including accounts that belong to background services rather than people. Students reach for it because it makes a "permission denied" error go away, and it does, the same way removing the lock from a door makes a jammed key go away.

The integrity problem is the serious one. If a 777 file is a script that some other program runs automatically, then whoever can edit that file effectively controls what that program does. A file that only needs to be readable should be 644. A script or a folder should be 755, or 750 if outsiders have no business there. If you find yourself typing 777, the real answer is almost always to fix the file's owner or group instead.

A file shows up in ls -l as -rw-r--r--. What is that in octal?

Drop the leading dash (that is the file type, not a permission) and read three at a time: rw- is 4 + 2 = 6, r-- is 4, and r-- is 4. That gives 644, the standard setting for an ordinary file that everyone may read and only the owner may change.

Where 644 and 755 come from

You did not choose those numbers, yet new files keep getting them. That is the umask, a per-user setting that subtracts permissions from the defaults. With the common umask of 022, new files land at 644 and new directories at 755. Run umask with no arguments to see yours.

06

Try It: The Permission Builder

Click the nine switches below and watch three things change at once: the octal number, the exact string ls -l would print, and the command you would type to get there. You can also type an octal value and watch the switches move to match.

Interactive: permission calculator
Setting permissions on:
Read 4 Write 2 Execute 1 Digit User the owner 7 Group the owning group 5 Other everyone else 5
Octal 755
ls -l shows -rwxr-xr-x
Command to type chmod 755 script.sh

Common real-world modes

Now read a real line

Here is a single line of ls -l output. Click any of the four highlighted pieces to see what it controls, then work through the questions underneath before you reveal the answers.

Mini exercise: read the ls -l line

 1 jordan students 2048 Mar 14 09:22 lab_report.txt

  • File typeA dash means this is an ordinary file, not a directory and not a link. It is never part of the octal math.
  • User triad: rw-Applies only to jordan, the owner listed later on the line. Jordan can read and edit the file but cannot run it as a program.
  • Group triad: r--Applies to anyone in the students group who is not jordan. They can read the file. They cannot change it.
  • Other triad: r--Applies to every other account on the machine. Read only. In octal the whole line is 644.
  1. Maya is in the students group but does not own this file. Can she edit it?

    No. She is not the owner, so Linux uses the group triad, and that is r--. She can open the file and she can make her own copy somewhere she does have write access, but she cannot change this file.

  2. Sam has an account on the server, does not own the file, and is not in students. What can Sam do?

    Read it, and nothing more. Sam falls into other, which is r--. This is exactly why a file holding anything private should not be 644.

  3. What would chmod 640 lab_report.txt change?

    It closes the file to outsiders. 6 is rw-, 4 is r--, and 0 is ---, so the line becomes -rw-r-----. Jordan still edits it, the students group can still read it, and everyone else loses access.

  4. Tricky one. Suppose the line read -r--rw-r-- instead, and jordan is also a member of students. Can jordan edit the file?

    No, and this is the classic trap. Jordan is the owner, so Linux stops at the user triad and applies r--. Being in the group does not add anything, because the triads are never combined. Jordan can fix it (owners are allowed to run chmod on their own files), but until then the answer is no. That mode is 464 in octal.

07

Changing Permissions: chmod, chown, and chgrp

Three commands cover almost everything. The rule for all of them: you may change a file you own, and root may change anything.

chmod, the octal form

chmod is short for "change mode," and mode is just another word for the nine permission bits. Give it three digits and a file name and it sets all nine at once:

chmod 644 lab_report.txt
chmod 755 backup.sh
chmod 600 secrets.env
chmod 750 team_folder

The octal form is absolute. Whatever the file had before is gone, replaced by exactly what you typed. That is great when you know the final answer and risky when you only meant to change one thing.

chmod, the symbolic form

The symbolic form adjusts one piece and leaves the rest alone. Each instruction has three parts: who, an operator, and the permissions.

  • Who: u user, g group, o other, a all three.
  • Operator: + add, - remove, = set to exactly this and clear the rest.
  • Permissions: any of r, w, x.
chmod u+x backup.sh Give the owner permission to run it. Everything else stays exactly as it was.
chmod go-w notes.txt Take write away from the group and from other, in one command.
chmod a=r policy.txt Everyone gets read and only read. Equivalent to 444.
chmod g+rw,o-rwx report.txt Two instructions separated by a comma with no space. The group gains read and write, other loses everything.

Use symbolic form when you want a small, obvious edit ("this script should be runnable"). Use octal when you want to state the whole answer ("this is a 600 file").

before and after
$ ls -l backup.sh
-rw-r--r-- 1 jordan students 814 Mar 10 11:47 backup.sh
 
$ ./backup.sh
bash: ./backup.sh: Permission denied
 
$ chmod u+x backup.sh
$ ls -l backup.sh
-rwxr--r-- 1 jordan students 814 Mar 10 11:47 backup.sh

chown and chgrp

chown changes which user owns a file. chgrp changes the owning group. Often the real fix for a permission problem is not looser permissions at all: it is the correct owner or the correct group.

sudo chown maya report.txt
sudo chown maya:students report.txt
chgrp students report.txt

The first line hands the file to maya. The second sets the owner and the group at once, separated by a colon. The third changes only the group, and you can run it without sudo if you own the file and you belong to the group you are naming.

Handing a file to a different user requires root, which is why the first two lines start with sudo. That rule exists so nobody can quietly dump files into someone else's name.

Be careful with -R

All three commands take -R for "recursive," meaning apply this to everything inside a folder. It is genuinely useful and it is also how people wreck a system in one keystroke. Note that chmod -R 755 marks every data file executable, because directories and files want different modes. When you need both, target them separately: find . -type d -print0 | xargs -0 chmod 755 and then find . -type f -print0 | xargs -0 chmod 644.

08

sudo, Least Privilege, and Why Any of This Matters

What sudo does

sudo stands for "superuser do." Put it in front of a command and that one command runs as root, then you are back to being yourself. It asks for your password, not the root password, and it records what you ran in the system log.

sudo chown maya:students report.txt
sudo systemctl restart nginx
sudo -l

That last one, sudo -l, lists what your account is allowed to run with sudo. Being able to use sudo is itself a permission, granted by an administrator (on many systems, by adding your account to a group such as sudo or wheel).

Why not just log in as root?

It would be simpler. Every command would work. That is exactly the problem.

  • Accountability. If five administrators share one root login, the logs say "root deleted the database" and nobody can tell which person that was. With sudo, every command is tied to a real account.
  • Blast radius. As root, an ordinary typo can delete system files instead of failing with a harmless error. Typing sudo makes the dangerous moments deliberate and rare.
  • Exposure. Any program you open in a root session inherits root's power, including a browser tab or a downloaded script that turns out to be hostile. Most distributions now disable direct root login for this reason.

The principle of least privilege

All of this is one idea with a name. The principle of least privilege says: give every person, program, and service the minimum access it needs to do its job, and nothing more. It is one of the oldest ideas in security and it shows up on essentially every cybersecurity exam.

In practice it sounds like a checklist:

  • Start from the tightest setting that works, then open up only where something actually breaks.
  • Prefer fixing the owner or the group over widening the permissions.
  • Keep root for the moments that truly need root.
  • Remove access when a person or a project is done with it, not "eventually."

What this looks like in the real world

Two examples make the whole topic concrete.

A world-writable script

Imagine a backup script on a shared server set to 777, and the server runs it automatically every night. Any account on that machine, including a service account nobody thinks about, can rewrite the contents of that file. Whatever is in the file tomorrow morning is what the server obediently runs. Nothing was "hacked," and nothing had to be. The permissions simply said yes. Set it to 755, or 750 if only the owning group has any business there, and that entire path closes.

A 600 private key

SSH is the standard way to log into a remote Linux machine, and it can authenticate you with a key file instead of a password. That private key file is the credential: anyone who can read it can log in as you. OpenSSH takes this so seriously that it refuses to use a key file other people can read:

ssh
$ ssh maya@lab-server.example.edu
Permissions 0644 for '/home/maya/.ssh/id_ed25519' are too open.
It is required that your private key files are NOT accessible by others.
This private key will be ignored.
 
$ chmod 700 ~/.ssh
$ chmod 600 ~/.ssh/id_ed25519
$ ssh maya@lab-server.example.edu
Welcome to Ubuntu 24.04.2 LTS

The fix is the two chmod commands: 700 on the .ssh directory so only you can enter it, and 600 on the key itself so only you can read it. That is least privilege in two lines, and it is a good habit to recognize the error message on sight, because you will meet it.

The short version

  • Ten characters: one file type, then three triads for user, group, and other.
  • Read is 4, write is 2, execute is 1. Add them per triad.
  • On a directory, x means enter and r means list, which is not the same thing.
  • Linux applies the first matching triad only. It never combines them.
  • chmod changes permissions, chown and chgrp change who they apply to.
  • Use sudo for single commands, keep 777 out of your habits, and default to the least access that works.

Go Further Than One Command

Access control is one lesson inside a full cybersecurity pathway. CodeHS courses cover the Linux command line, networking, cryptography, digital forensics, and system administration, with hands-on labs and everything a teacher needs to run the class.