Full Byte Developer.

Linux permissions: chmod and chown

chmod sets mode bits (who can read, write, execute); chown changes owner and group under privilege rules.

Published here , updated · 3 sources

File mode bits say who may read, write, or execute; ownership says which user and group those "owner" bits apply to [1][2]. The kernel checks both when a process opens a path. Learn the nine bits and the privilege rules for chown before you reach for chmod 777 on a deploy box.

One minute: set two modes you will reuse

bash
mkdir -p /tmp/perm_lab && touch /tmp/perm_lab/notes.txt /tmp/perm_lab/run.sh /tmp/perm_lab/secret
chmod 644 /tmp/perm_lab/notes.txt
chmod 755 /tmp/perm_lab/run.sh
chmod 600 /tmp/perm_lab/secret
ls -l /tmp/perm_lab
stat -c '%a %n' /tmp/perm_lab/*

Captured output on this host (coreutils, 2026-12-03):

text
-rw-r--r-- 1 ubuntu ubuntu 0 Dec  3 12:13 notes.txt
-rwxr-xr-x 1 ubuntu ubuntu 0 Dec  3 12:13 run.sh
-rw------- 1 ubuntu ubuntu 0 Dec  3 12:13 secret
644 /tmp/perm_lab/notes.txt
755 /tmp/perm_lab/run.sh
600 /tmp/perm_lab/secret

644 is a normal shared text file. 755 is a script or directory others may traverse. 600 is private (keys, env files) [1].

Match octal to the nine bits

GNU chmod accepts symbolic modes (u+x, go-w) or octal modes [1]. Octal digits add 4 (read), 2 (write), 1 (execute) for owner, group, and others in order [1].

text
         owner        group       others
         r w x        r w x       r w x
octal    4 2 1        4 2 1       4 2 1

644  =  rw- r-- r--
755  =  rwx r-x r-x
600  =  rw- --- ---

Letters ugoa select whose bits change; operators +, -, and = add, remove, or set bits [1]. Symbolic form is easier when you only add execute for the owner: chmod u+x deploy.sh.

chmod 755 owner 7 = rwx 4+2+1 group 5 = r-x 4+0+1 others 5 = r-x 4+0+1 chown alice:devs file changes who the owner/group bits apply to (privilege rules) Directory execute means traverse; file execute means run.

Three octal digits encode owner, group, and others; chown picks which identities those digits refer to.

Change ownership with chown

bash
chown alice:devs deploy.sh

GNU chown sets owner and optional group (OWNER:GROUP) [2]. Only a privileged process may change the owner; an ordinary owner may move the group to a group they belong to [3]. sudo chown is the usual path when a deploy user must own files created as root.

A service running as www-data needs read on files your login user created only if ownership or group membership aligns. chmod cannot fix a group membership problem you created with usermod.

Directories are special

Execute on a directory means permission to search the directory (lookup names). Read means list names with ls. Write means create or delete entries in that directory (subject to sticky-bit rules on shared dirs) [1].

A common deploy trap: script is 755 but it lives in a directory with 700 owned by someone else. Others cannot traverse to reach the script no matter what the file bits say.

umask and defaults

New files get a default mode minus umask. umask 022 yields world-readable new files (644 for files with typical defaults). umask 077 keeps new files private. Check umask in shells that spawn daemons; a loose umask leaks logs and uploads.

bash
umask
touch /tmp/umask_demo && ls -l /tmp/umask_demo

Captured on this host:

text
0022
-rw-r--r-- 1 ubuntu ubuntu 0 Dec  3 12:13 /tmp/umask_demo

With umask 0022, the kernel subtracts group and other write bits from the default 666 file creation mode, leaving 644 [1].

Sticky bit and setuid (names only)

Setuid on an executable lets a program run with the owner's effective UID. Sticky on a world-writable directory (for example /tmp) restricts deletes to the file owner. This page does not teach setuid scripting; know the letters exist when ls -l shows s or t instead of a plain x.

chgrp without chown

An owner may chgrp to a group they belong to without root [3]. Useful when a deploy user and www-data share a group for read-only config. Changing owner still needs privilege [3].

Keep deploy scripts boring

Prefer the least mode that still works: private keys 600, shared config often 644, directories that must be entered need execute/search bits [1]. Fix ownership when a service user must read files your login user created [2][3]. Audit chmod -R before you run it on a tree you care about.

SSH requires tight ~/.ssh modes (ssh keys for developers). Everyday navigation commands live in essential Linux commands part 1. Who the kernel checks against is a process.

macOS versus Linux quirks

macOS ships BSD chmod plus ACL flags on some volumes. Linux GNU chmod differs slightly on symlinks and recursion defaults [1][2]. Scripts that must run on both should document which chmod variant you used. Docker bind mounts inherit host permissions; fixing modes inside the container does not help if the mounted file stays 600 on the host.

Audit with find when trees drift

bash
find /tmp/perm_lab -type f -perm 600 -ls

Use find on a teaching directory you control. System trees need sudo and care. Searching for unexpected 777 directories after a bad deploy script is the same idea with a mode you do not want.

ACLs and beyond this page

This page stays with classic owner/group/others bits [1][2]. POSIX ACLs (setfacl) extend who can access a file without changing primary owner. Capabilities and SELinux add labels this article does not cover. Reach for ACLs when one shared directory needs many users with different rights.

Common mistakes

chmod 777 to silence Permission denied. You mask the real owner or service user mismatch.

Forgetting execute on directories in the path to a script. The script bits do not matter if traversal fails.

Chowning everything to yourself on a shared server without coordinating groups. Services break when they no longer match unit file User=.

Running chmod -R 777 on vendor or node_modules trees. Huge, unnecessary, and hides mistaken uploads.

Treating group read as enough to run scripts. Execute is a separate bit; 646 does not run.

Using chown without privilege to take files from root. Only privileged processes change owner [3].

Treating chmod on a symlink path as chmod on the link when you meant the target. Verify with ls -l.

Read the error the kernel actually sent

Permission denied on ./deploy.sh can mean the file lacks execute, a directory in the path lacks execute/search, or the interpreter in the shebang is not executable for you. ls -l on the file alone is not enough; walk each path component with namei -l /full/path/to/deploy.sh when you have it, or manually ls -ld each directory. Mode 644 on a script looks readable and still will not run without the execute bit [1].

Operation not permitted on chown is the privilege rule talking [3]. Switching to chmod 777 does not grant ownership. Fix the user the service unit runs as, or use sudo with intent when a root-owned tree must move to a deploy user.

Recipes that stay boring on purpose

Private SSH key material: directory 700, key file 600, public key often 644 (ssh keys for developers).

Web content readable by the server user: files 644, directories 755, ownership either the deploy user with group read for the server group, or the server user with careful write isolation on upload directories only [1][2].

Shared drop directory: sticky bit on the directory so people cannot delete each other's files, plus group write if that is the collaboration model. Do not confuse sticky with setuid.

Containers and bind mounts

A file mode is a property of the inode on the filesystem that holds it. Bind-mounting a host file into a container preserves those bits. Changing modes inside the container namespace on a bind mount changes the host file. Changing ownership may require privileges that the container user does not have. Image USER directives do not rewrite modes on volumes you mount at runtime. Debug with ls -l from both sides of the mount before you rewrite Dockerfiles in a panic.

Numeric modes in Ansible and Docker

Playbooks and RUN chmod lines love octal because it is compact and reviewable in a diff [1]. Prefer explicit modes over relying on umask inside a build layer you do not control. When a Dockerfile copies a secret for a build step, delete it in a later layer and still assume older layers may exist in a cache until you use a proper secret mount mechanism. Mode 600 in a layer that remains in the image is still readable by anyone who pulls the image.

Teaching lab you can repeat

bash
rm -rf /tmp/perm_lab && mkdir -p /tmp/perm_lab/bin
printf '#!/bin/sh\necho ok\n' > /tmp/perm_lab/bin/hello.sh
chmod 644 /tmp/perm_lab/bin/hello.sh
/tmp/perm_lab/bin/hello.sh || true
chmod 755 /tmp/perm_lab/bin/hello.sh
/tmp/perm_lab/bin/hello.sh

The first run should fail without execute. The second should print ok. That single contrast is the whole execute-bit lesson for files. Repeat it for directories by removing execute from /tmp/perm_lab/bin and trying to run the script through that path [1].

Browse the Dev tools topic.

Send this to the friend who chmods everything to 777 to make the error go away.

Dark reflective floor with faint schematic lines. Left: a translucent teal glass cube ringed by three segmented horizontal bands (teal, gold, teal). Right: a solid teal disc, a thick gold 3D arrow, and a glowing gold disc showing a mode or ownership change.
The cube is the file; the rings are who may touch it. The teal-to-gold arrow is chmod or chown changing that state on purpose.

Keep this guide

By Full Byte Developer

Filed under Dev tools, Dev explainers

Questions about this guide

What does chmod 755 mean?

GNU chmod: octal digits add 4 read, 2 write, 1 execute for owner, then group, then others. 755 is rwx for owner, rx for group and others.

What is the difference between chmod and chown?

chmod changes permission bits. chown changes which user and/or group owns the file.

Who may change a file's owner?

Linux chown(2): only a privileged process (CAP_CHOWN) may change owner. An owner may change the group to a group they belong to.

Do symbolic links get chmod?

GNU chmod: it does not change symlink mode bits themselves; for links listed on the command line it changes the target file instead.

Why must directories be executable?

The execute bit on a directory means search/traverse (lookup names). Without it, you cannot cd in or list through paths that need that directory.

Sources

Next in this stack