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:
- Read (r) — view the contents (or list the directory)
- Write (w) — modify or delete the file (or create/delete entries in the directory)
- Execute (x) — run the file as a program (or
cdinto the directory)
For directories, the meaning is slightly different:
r— list the contentsw— create, rename or delete entriesx— enter the directory (use it as part of a path)
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:
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).
ls -aldrwxr-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 .TrashReading the permission string
The first 10 characters look like drwxr-xr-x. Break that down:
type | user | group | other - | rwx | r-x | r-xThe first character is the file type:
- regular filed directoryl symbolic linkb block special filec character special filep named pipe (FIFO)s socketAfter that, three groups of three: owner, group, others. Each is rwx with - substituted for missing perms.
So drwxr-xr-x means:
- It's a directory
- The owner can read, write, execute
- The group and everyone else can read and execute (but not write)
A few more reads:
drwx------ 3 oz staff 96 2 Şub 11:03 .cupsDirectory, only the owner has any access. Even members of the staff group can't enter it.
-rw-r--r-- 1 oz staff 122 1 Mar 09:30 readme.mdRegular file, owner can read and write, everyone else can only read.
Numeric (octal) mode
Each permission also has a numeric value:
r = 4w = 2x = 1Add them up to get the digit for each category. The full mode is three digits — owner, group, other.
000 --- no permissions100 --x execute only200 -w- write only400 r-- read only500 r-x read + execute600 rw- read + write700 rwx fullA few common patterns:
chmod 644 file—rw-r--r--(owner can edit, others can read)chmod 600 file—rw-------(owner-only — what you want for SSH private keys)chmod 755 dir—rwxr-xr-x(owner full, others can enter and list)chmod 700 dir—rwx------(owner-only directory)chmod 777 file— full perms for everyone (almost always wrong on real systems)
Symbolic mode
The other syntax uses +, -, =:
+— add the permission-— remove the permission=— set exactly this (clears the others)
Examples:
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: rRecursive
For an entire directory tree, use -R:
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:
# 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:
chmod -R u+rwX,go+rX project/Patterns I use a lot
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 treeStick 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.