Phase 2Master bash

#5 Permissions and users

chmod, chown, ls -l

Understanding permissions

In Linux, every file has permissions that control who can read, modify, or execute it. When you type ls -l, the first column displays these permissions:

View permissions
$ ls -l
-rw-r--r-- 1 user user  42 mar  3 10:00 notes.txt
-rwxr-xr-x 1 user user 128 mar  3 10:05 script.sh
drwxr-xr-x 2 user user  40 mar  3 09:30 projects

The rwxr-xr-x format

The permission string is read in four parts. The first character indicates the type (- for a file, d for a directory). The next nine characters are divided into three groups of three:

Breakdown
- rwx r-x r-x
│ │   │   └── Others (others) : r-x
│ │   └────── Group (group)   : r-x
│ └────────── Owner (user)    : rwx
└──────────── Type             : - (file)

Each group contains three letters:

  • r (read) -- read permission
  • w (write) -- write permission
  • x (execute) -- execute permission

A dash - means the permission is not granted. For example, r-- means read-only.

chmod -- Change permissions

The chmod (change mode) command changes the permissions of a file. There are two notations: numeric (octal) and symbolic.

In numeric notation, each permission has a value:r=4, w=2, x=1. You add the values together for each group:

Numeric notation
7 = rwx (4+2+1)  — all permissions
5 = r-x (4+0+1)  — read + execute
4 = r-- (4+0+0)  — read only
6 = rw- (4+2+0)  — read + write
0 = --- (0+0+0)  — no permissions
Example: chmod 755
$ chmod 755 script.sh
# user=rwx (7), group=r-x (5), others=r-x (5)

In symbolic notation, you use + to add and - to remove a permission:

Symbolic notation
$ chmod +x script.sh     # Add execute permission
$ chmod u+w file.txt  # Add write for the owner
$ chmod go-w file.txt # Remove write for group and others

chown -- Change ownership

The chown (change owner) command changes the owner and/or group of a file. The format is user:group:

Change ownership
$ chown alice:dev file.txt
# The file now belongs to alice, group dev

$ chown alice file.txt
# Changes only the owner

$ chown :dev file.txt
# Changes only the group

In practice, chown often requires administrator privileges (sudo).

Summary

Summary
ls -l                    # View permissions
chmod 755 file           # Permissions in numeric notation
chmod +x file            # Add execute permission
chown user:group file    # Change owner and group

Your turn

Try ls -l to view permissions, then modify them with chmod and chown in the terminal below.

terminal — bash
user@stemlegacy:~$