Home

File and directory permissions on Linux with chmod

Read, write, execute

Linux is multi-user, so files have access controls. Each file or directory has three permission bits:

For directories, the meaning is slightly different:

Without these controls, anyone could read or wipe anyone else's files. With them, you can scope access precisely.

chmod

chmod ("change mode") is the command for setting permissions on a file or directory. Only the file's owner or the superuser (root) can change its mode.

There are three categories of who the permissions apply to:

bash
u user (owner)g group (the file's group)o — other (everyone else)a — all (u + g + o)

And three permissions, always in the same order: rwx.

To see the permissions of files in a directory, use ls -l (or ls -all for hidden files too).

bash
ls -al

bash
drwxr-x---+ 51 oz   staff  1632 13 Şub 16:39 .drwxr-xr-x   5 root admin   160 27 Oca 03:42 ..-r--------   1 oz   staff     9  6 Oca 15:55 .CFUserTextEncoding-rw-r--r--@  1 oz   staff 12292 10 Sub 15:14 .DS_Storedrwxr-xr-x@  5 oz   staff   160 13 Oca 20:48 .IdentityServicedrwx------+ 17 oz   staff   160 13 Şub 16:15 .Trash

Reading the permission string

The first 10 characters look like drwxr-xr-x. Break that down:

bash
type | user  | group | other -   | rwx   | r-x   | r-x

The first character is the file type:

bash
-   regular filed   directoryl   symbolic linkb   block special filec   character special filep   named pipe (FIFO)s   socket

After that, three groups of three: owner, group, others. Each is rwx with - substituted for missing perms.

So drwxr-xr-x means:

A few more reads:

bash
drwx------   3 oz   staff    96  2 Şub 11:03 .cups

Directory, only the owner has any access. Even members of the staff group can't enter it.

bash
-rw-r--r--   1 oz   staff   122  1 Mar 09:30 readme.md

Regular file, owner can read and write, everyone else can only read.

Numeric (octal) mode

Each permission also has a numeric value:

bash
r = 4w = 2x = 1

Add them up to get the digit for each category. The full mode is three digits — owner, group, other.

bash
000  ---  no permissions100  --x  execute only200  -w-  write only400  r--  read only500  r-x  read + execute600  rw-  read + write700  rwx  full

A few common patterns:

Symbolic mode

The other syntax uses +, -, =:

Examples:

bash
chmod u+x script.sh           # owner gets executechmod go-rw notes.md          # group + others lose read & writechmod a+r public.txt          # everyone gets readchmod u=rw,go=r config.yml    # owner: rw, group: r, other: r

Recursive

For an entire directory tree, use -R:

bash
chmod -R u+rw project/

Be careful — this applies the same mode to files and directories. If you want directories to keep their x bit (so they're traversable) while removing it from files, that needs a find + chmod combo:

bash
# directories: rwx for owner, rx for othersfind project -type d -exec chmod 755 {} \;# files: rw for owner, r for othersfind project -type f -exec chmod 644 {} \;

Or in one shot with the X (capital) symbolic mode, which only adds x to directories and to files that already have x:

bash
chmod -R u+rwX,go+rX project/

Patterns I use a lot

bash
chmod 600 ~/.ssh/id_ed25519              # SSH private keychmod 644 ~/.ssh/id_ed25519.pub          # SSH public keychmod 700 ~/.ssh                         # SSH directorychmod +x deploy.sh                       # make a script executablechmod -R go-rwx /etc/secret-config       # lock down a config tree

Stick to numeric mode for canonical settings (anything documented somewhere) and symbolic mode for quick toggles like +x. Both work — pick whichever reads clearer in context.