Skip to content

File Permissions

Terminal window
ls -la
# -rwxr-xr-- 1 krav users 4096 Mar 01 12:00 script.sh
# ╰──╯╰──╯╰──╯
# user group other
CharacterMeaning
rRead (4)
wWrite (2)
xExecute (1)
-No permission (0)

First character: file type (- file, d directory, l symlink).


Each permission group (user/group/other) is a digit 0–7:

OctalBinaryPermissions
7111rwx
6110rw-
5101r-x
4100r--
3011-wx
2010-w-
1001--x
0000---

Example: 755 = rwxr-xr-x (owner: full, group: read+execute, others: read+execute).


Terminal window
chmod 755 script.sh # rwxr-xr-x
chmod 644 config.txt # rw-r--r--
chmod 600 private.key # rw-------
chmod 700 ~/.ssh # rwx------
Terminal window
chmod u+x script.sh # Add execute for user
chmod g-w file.txt # Remove write for group
chmod o-rwx secret.txt # Remove all for others
chmod a+r public.txt # Add read for all (user+group+other)
chmod u=rwx,g=rx,o= file # Set explicitly
Terminal window
chmod -R 755 /var/www/html

Terminal window
chown krav file.txt # Change owner
chown krav:users file.txt # Change owner and group
chown :users file.txt # Change group only
chown -R krav:www-data /var/www # Recursive
Terminal window
chgrp users file.txt
chgrp -R www-data /var/www

OctalSymbolicUse case
644rw-r--r--Regular files (configs, text)
755rwxr-xr-xScripts, executables, directories
700rwx------Private directories (~/.ssh)
600rw-------Private keys, sensitive configs
400r--------Read-only private files (SSH keys)
666rw-rw-rw-World-writable file (avoid in production)
777rwxrwxrwxWorld-writable + executable (almost never correct)
750rwxr-x---Shared directory within a group

Permissions mean different things for directories:

PermissionFileDirectory
rRead contentsList contents (ls)
wModify contentsCreate/delete files inside
xExecute as programEnter (cd into) the directory

A directory needs x to be traversable — r alone lets you list but not access.


BitOctal prefixOn fileOn directory
SUID4Runs as the file owner, not the caller
SGID2Runs as the file’s groupNew files inherit directory’s group
Sticky1Only file owner can delete their files
Terminal window
# Set SUID
chmod 4755 /usr/bin/passwd # s in user execute: -rwsr-xr-x
# Set SGID on directory
chmod 2755 /shared # s in group execute: drwxr-sr-x
# Set sticky bit
chmod 1755 /tmp # t in other execute: drwxr-xr-t
# Find SUID binaries (security audit)
find / -perm -4000 -type f 2>/dev/null

Default permissions for new files are controlled by umask. Subtracted from 666 (files) or 777 (directories).

Terminal window
umask # Show current (e.g. 0022)
umask 0027 # New files: 640, new dirs: 750
umaskNew fileNew directory
0022644755
0027640750
0077600700