Lesson 4 - File & Directory Permissions

Progress 0%

Interactive Terminal Sim:

Why Permissions Matter

Linux was built from the ground up as a multi-user system. Many different people — and many different programs — can use the same computer, so the operating system needs a way to control who is allowed to read, change, or run each file.

That control is handled by permissions. Every file and directory has an owner, and rules for exactly what the owner, other members of a group, and everyone else are allowed to do with it.

Reading Permission Strings

You can see a file's permissions with the long listing format of ls:

ls -l

Each entry starts with something like -rw-r--r--. This string is split into three groups of three characters, one for the owner, one for the group, and one for others:

r w x  r--   r--
owner  group others

Each of the three letters means:

r  read
w  write
x  execute

A dash (-) means that permission is not granted. A directory that shows d at the very start (instead of -) needs the execute permission for users to be able to enter it.

Changing Permissions with chmod

chmod ("change mode") changes a file's permissions. Try making the deploy script in the projects folder executable:

chmod +x Documents/projects/deploy.sh

This adds the execute permission for everyone. You can remove a permission the same way, using a minus sign instead:

chmod -w Documents/projects/deploy.sh

Permissions can also be set using numbers, where each permission has a value: read = 4, write = 2, execute = 1. Add them together for each of the three groups:

chmod 755 Documents/projects/deploy.sh

755 means 7 (read+write+execute) for the owner, and 5 (read+execute) for the group and others. 644 is another common value, giving the owner read and write, and everyone else read-only.

Run ls -l again after each change to see the permission string update.

Changing Ownership with chown

Sometimes a file needs to belong to a different user or group entirely. That's what chown ("change owner") is for:

chown root Documents/projects/deploy.sh

You can set both the owner and the group at once by separating them with a colon:

chown root:root Documents/projects/deploy.sh

Together, chmod and chown give administrators fine control over exactly who can read, edit, or run every file on the system — one of the reasons Linux is trusted to run everything from personal laptops to shared servers.