chmod

file permissionslinux
The chmod command is one of the most frequently used commands in Linux/Unix-like operating systems. chmod Change file mode bits (permissions)

Quick Reference

Command Name:

chmod

Category:

file permissions

Platform:

linux

Basic Usage:

chmod 755 filename.sh

Common Use Cases

  • 1

    Permission management

    Change file and directory permissions

  • 2

    Security configuration

    Set appropriate permissions for security

  • 3

    System administration

    Manage access permissions for files and directories

  • 4

    Multi-user setup

    Configure permissions for shared access

Syntax

chmod [OPTION]... MODE[,MODE]... FILE...
chmod [OPTION]... OCTAL-MODE FILE...
chmod [OPTION]... --reference=RFILE FILE...

Options

Option Description
-c, --changes Like verbose but report only when a change is made
-f, --silent, --quiet Suppress most error messages
-v, --verbose Output a diagnostic for every file processed
--no-preserve-root Do not treat '/' specially (the default)
--preserve-root Fail to operate recursively on '/'
--reference=RFILE Use RFILE's mode instead of specifying a MODE value
-R, --recursive Change files and directories recursively
--help Display help message and exit
--version Output version information and exit

Permission Modes:

Mode Description
u User who owns the file
g Group that owns the file
o Others (not owner or group)
a All (user, group, others)
r Read permission
w Write permission
x Execute permission
s Set user or group ID on execution
t Sticky bit
+ Add the specified permissions
- Remove the specified permissions
= Set the specified permissions and remove all others

Examples

How to Use These Examples

The examples below show common ways to use the chmod command. Try them in your terminal to see the results. You can copy any example by clicking on the code block.

Basic Examples:

Give the owner read, write, and execute permissions
chmod u+rwx filename.sh
Remove write permission for group and others
chmod go-w important_file.txt
Set read and execute for all (user, group, others)
chmod a+rx script.sh
Use octal notation to set permissions (rwx for owner, rx for group and others)
chmod 755 program.bin
Add execute permission to a script
chmod +x install.sh

Advanced Examples:

Set permissions recursively for a directory and its contents
chmod -R 755 /var/www/html
Add sticky bit to a directory (only file owners can delete their files)
chmod +t /home/shared
Add setuid bit (execute as file owner)
chmod u+s /usr/bin/passwd
Add setgid bit (execute as file group)
chmod g+s /var/www/uploads
Copy permissions from one file to another
chmod --reference=reference.txt target.txt
Change permissions but don't follow symbolic links
chmod -h 644 symlink.txt
Set exact permissions (remove any other bits) using symbolic notation
chmod =rw file.txt
Only change permissions if they match a specific pattern
chmod --from=u=r,g=r,o= u+w,g+w data.txt

Try It Yourself

Practice makes perfect! The best way to learn is by trying these examples on your own system with real files.

Understanding Syntax

Pay attention to the syntax coloring: commands, options, and file paths are highlighted differently.

Notes

Key Features:

The chmod command is one of the most important Linux/Unix commands, used to change the permissions (or "mode") of files and directories:

  • Control who can read, write, or execute files
  • Set special permission bits (setuid, setgid, sticky bit)
  • Use either symbolic or octal notation to specify permissions
  • Apply changes recursively to directory structures
  • Match permissions between files using reference files

Permission Notation:

Symbolic Notation:

Consists of three parts: who, operation, and permissions

  • Who: u (user/owner), g (group), o (others), a (all)
  • Operation: + (add), - (remove), = (set exactly)
  • Permissions: r (read), w (write), x (execute), s (setuid/setgid), t (sticky bit)

Octal (Numeric) Notation:

Uses three or four digits to represent permissions

  • First digit (optional): Special permissions (setuid, setgid, sticky bit)
    • 4 = setuid
    • 2 = setgid
    • 1 = sticky bit
  • Second digit: Owner permissions
  • Third digit: Group permissions
  • Fourth digit: Others permissions
  • For each of the last three digits:
    • 4 = read
    • 2 = write
    • 1 = execute
    • 0 = no permission

Common Permission Values:

  • 777 (rwxrwxrwx): Full permissions for everyone (rarely appropriate)
  • 755 (rwxr-xr-x): Owner can read/write/execute, others can read/execute
  • 644 (rw-r--r--): Owner can read/write, others can read
  • 700 (rwx------): Owner has full access, no access for others
  • 600 (rw-------): Owner can read/write, no access for others
  • 4755 (rwsr-xr-x): Same as 755 but with setuid bit set

Special Permission Bits:

  • Setuid (s): When set on an executable, it runs with the permissions of the owner
  • Setgid (s): When set on an executable, it runs with the permissions of the group. When set on a directory, new files created in the directory inherit the directory's group
  • Sticky bit (t): When set on a directory, files within that directory can only be deleted by their owners, the directory owner, or root

Important Considerations:

  • You must be the owner of a file or have appropriate privileges to change its permissions
  • Be careful with recursive changes, especially on system directories
  • Use the --preserve-root option to prevent accidental permission changes to the root directory
  • The default umask on your system affects the initial permissions of new files
  • Directory permissions work differently than file permissions:
    • r: List directory contents
    • w: Create/delete files in the directory
    • x: Access files and subdirectories within the directory

Common Use Cases

Permission management

Change file and directory permissions

Security configuration

Set appropriate permissions for security

System administration

Manage access permissions for files and directories

Multi-user setup

Configure permissions for shared access

Script execution

Make scripts executable and set appropriate permissions

Related Commands

These commands are frequently used alongside chmod or serve similar purposes:

Use Cases

1

Permission management

Change file and directory permissions

2

Security configuration

Set appropriate permissions for security

3

System administration

Manage access permissions for files and directories

4

Multi-user setup

Configure permissions for shared access

5

Script execution

Make scripts executable and set appropriate permissions

Learn By Doing

The best way to learn Linux commands is by practicing. Try out these examples in your terminal to build muscle memory and understand how the chmod command works in different scenarios.

$ chmod
View All Commands