04. Files, Permissions, and Users¶
Why this matters¶
Linux is a multi-user system. Several people, several programs, and several services can all work on one server. To keep them from getting in each other's way and from accessing each other's data, Linux uses a permissions system.
As a Python/Django developer, you'll regularly run into Permission denied errors. To fix them, you need to understand how permissions work.
A simple explanation¶
Imagine that every file is a folder of documents. Permissions are the locks: who can read, who can edit, who can run.
Every file and directory in Linux has: - an owner (user/owner) — usually whoever created the file - a group (group) — a set of users with shared permissions - permissions (permissions) — what can be done with the file
How to read permissions¶
Run ls -la and you'll see lines like this:
-rwxr-xr-- 1 student www-data 4096 Jun 10 12:00 manage.py
^ ^ ^ ^ ^ ^
type+perms links owner group size name
The first character — the file type¶
| Character | Type |
|---|---|
- |
Regular file |
d |
Directory |
l |
Symbolic link |
The next 9 characters — the permissions¶
rwxr-xr--
^^^ — owner permissions (owner)
^^^ — group permissions (group)
^^^ — permissions for everyone else (others)
What r, w, x mean¶
| Character | Name | For a file | For a directory |
|---|---|---|---|
r |
read | Can read the contents | Can see the list of files |
w |
write | Can modify and delete | Can create/delete files |
x |
execute | Can run it as a program | Can enter it with cd |
- |
no permission | — | — |
A worked example¶
-rwxr-xr--
- = regular file
rwx = the owner can: read, write, execute
r-x = the group can: read, execute (write — NO)
r-- = everyone else can: only read
Numeric permissions (octal)¶
Permissions are often written as numbers:
| Character | Number |
|---|---|
r |
4 |
w |
2 |
x |
1 |
- |
0 |
The permissions for each set are the sum of the numbers:
Typical combinations:
| Number | Characters | Meaning |
|---|---|---|
755 |
rwxr-xr-x |
Owner: everything; Group and Others: read and execute |
644 |
rw-r--r-- |
Owner: read+write; Everyone: read only |
600 |
rw------- |
Only the owner can read and write |
777 |
rwxrwxrwx |
Everyone can do everything (dangerous!) |
The chmod command — changing permissions¶
chmod +x script.sh # add the execute permission for everyone
chmod -x script.sh # remove the execute permission
chmod 755 script.sh # set specific permissions
chmod 644 settings.py # typical for config files
chmod -R 755 /var/www/ # recursively for a directory
Explanation:
chmod — change mode. +x — add (+) the execute permission (x). Without it, Linux will refuse to run the file as a script.
A private SSH key must have 600 permissions — only the owner can read it. If the permissions are broader, SSH will refuse to connect.
The chown command — changing the owner¶
chown student file.txt # change the owner
chown student:www-data file.txt # change the owner and group
chown -R www-data /var/www/ # recursively
When you need it:
If you copied files via scp or sudo, root may become the owner. Then Nginx or Gunicorn won't be able to read the files.
What sudo is¶
sudo — Superuser Do. Lets you run a command with superuser (root) privileges, even if you're a regular user.
Imagine you're renting an apartment. Regular commands are what you can do in your own room.
sudois asking the landlord (root) to do something in the shared areas.
Why you shouldn't work as root all the time:
- A mistake in a command can destroy the system without warning
- Programs started as root have access to everything
- If a service is hacked, the attacker gets root privileges
- The normal working mode is as a regular user
The whoami and id commands¶
whoami # the current user's name
id # uid, gid, and all of the user's groups
id student # the groups of a specific user
A practical example — a Django project on a server¶
# View the project files' permissions
ls -la /var/www/myapp/
# Make the startup script executable
chmod +x /var/www/myapp/start.sh
# Hand the files over to the Nginx user
sudo chown -R www-data:www-data /var/www/myapp/static/
# Make the config accessible only to the owner
chmod 600 /var/www/myapp/.env
Common beginner mistakes¶
Mistake 1: Permission denied when running a script
Run
chmod +x script.sh
Mistake 2: Permission denied when working with Nginx/static
Check that the Nginx process (
www-data) has read permission on the directory
Mistake 3: Setting chmod 777 on all files "just to make sure it works"
This is dangerous. Any program on the server will be able to modify your files. Use the minimum necessary permissions.
Mistake 4: The SSH key isn't accepted
Check:
chmod 600 ~/.ssh/id_rsaandchmod 700 ~/.ssh/
Practical exercise¶
Task 1¶
What permissions does the newly created file have? What does each character mean?Task 2¶
chmod +x test_file.py
ls -la test_file.py
chmod 644 test_file.py
ls -la test_file.py
chmod 755 test_file.py
ls -la test_file.py
Task 3¶
mkdir secret_config
touch secret_config/.env
echo "SECRET_KEY=abc123" > secret_config/.env
chmod 700 secret_config/
chmod 600 secret_config/.env
ls -la secret_config/
ls -la secret_config/.env
Self-check¶
- [ ] I can read the output of
ls -laand explain each permission character - [ ] I understand the difference between
r,w,xfor files and directories - [ ] I know the numeric form of permissions:
755,644,600 - [ ] I can change permissions with
chmod - [ ] I can change the owner with
chown - [ ] I understand what
sudois and why you shouldn't be root all the time
Short summary¶
Permissions protect files from unauthorized access. Every file has an owner, a group, and permissions (r/w/x) for three categories. chmod changes permissions, chown changes the owner, sudo grants temporary root privileges. The next step is processes, ports, and services.