Linux

chmod

Change file mode bits (permissions).

#linux #permissions #security #file-management

Basic Usage

CommandDescription
chmod 755 [file]rwx for owner, rx for group/others.
chmod 644 [file]rw for owner, r for group/others.
chmod +x [file]Add execute permission for all.
chmod u+x [file]Add execute permission for owner only.
chmod -R 755 [directory]Recursively change permissions.

Symbolic Modes

More readable and flexible than octal modes.

SyntaxDescription
u=rwx,g=rx,o=Set specific permissions for user, group, others.
ug+wAdd write permission for user and group.
o-rwxRemove all permissions for others.
a+xAdd execute for all (same as +x).
g+sSet Set-Group-ID (SGID) bit.
u+sSet Set-User-ID (SUID) bit.
+tSet Sticky bit.

Advanced Examples

Reference Permissions

Apply the same permissions as another file.

chmod --reference=[reference_file] [file]

Conditional Execute

Add execute permission only if it's a directory or already has execute for someone.

chmod -R a+X [directory]

Special Bits (SUID, SGID, Sticky)

ModeOctalDescription
SUID4000Run as owner.
SGID2000Run as group / New files inherit group.
Sticky1000Only owner can delete files in directory.

Set SUID.

chmod u+s [file]
chmod 4755 [file]

Set SGID on directory (for shared folders).

chmod g+s [directory]
chmod 2775 [directory]

Set Sticky bit (like /tmp).

chmod +t [directory]
chmod 1777 [directory]

Bulk Changes with Find

Change directories to 755 recursively.

find [directory] -type d -exec chmod 755 {} +

Change files to 644 recursively.

find [directory] -type f -exec chmod 644 {} +

Preserve Root

Fail if recursively operating on /.

chmod --preserve-root -R 777 /

Verbose Output

Show changed files only.

chmod -c -R 755 [directory]

Related Links