chmod Command Reference

The chmod (change mode) command is used to change the access permissions of files and directories in Unix and Linux systems. Permissions determine who can read, write, or execute a file or directory. This reference provides an overview of how to use chmod with both symbolic and numeric modes, along with examples to help you manage file permissions effectively.

Understanding File Permissions

Each file and directory has three types of permissions for three categories of users:

The permissions are:

Symbolic Mode

In symbolic mode, you specify permissions using letters and symbols.

Syntax

chmod [references][operator][modes] file

References

Operators

Modes

Examples

Command Description
chmod u+x file Add execute permission for the owner.
chmod g-w file Remove write permission for the group.
chmod o=r file Set read-only permission for others.
chmod a+rw file Add read and write permissions for all.
chmod u=rw,g=r,o= file Set read/write for owner, read for group, no permissions for others.
chmod +x file Add execute permission for all (if no reference is specified, a is assumed).

Numeric (Octal) Mode

In numeric mode, permissions are represented by a three-digit octal number, with each digit corresponding to user, group, and others.

Permission Values

Octal Digit Permission Description
0 --- No permissions
1 --x Execute only
2 -w- Write only
3 -wx Write and execute
4 r-- Read only
5 r-x Read and execute
6 rw- Read and write
7 rwx Read, write, and execute

Syntax

chmod [mode] file

Examples

Command Permissions Description
chmod 755 file rwxr-xr-x Owner can read, write, execute; group and others can read and execute.
chmod 644 file rw-r--r-- Owner can read and write; group and others can read.
chmod 700 file rwx------ Owner can read, write, execute; group and others have no permissions.
chmod 600 file rw------- Owner can read and write; group and others have no permissions.
chmod 666 file rw-rw-rw- All can read and write.
chmod 444 file r--r--r-- All can read only.

Special Permissions

Special permissions include the setuid, setgid, and sticky bits.

Setuid (Set User ID)

When set on an executable file, users executing the file will have the permissions of the file owner during execution.

# Set setuid bit
chmod u+s file

# Numeric mode (4XXX)
chmod 4755 file

Setgid (Set Group ID)

When set on a file, users executing the file will have the permissions of the file's group during execution. When set on a directory, new files created within inherit the directory's group.

# Set setgid bit
chmod g+s directory

# Numeric mode (2XXX)
chmod 2755 directory

Sticky Bit

When set on a directory, files within can only be deleted or renamed by their owner, the directory owner, or the root user.

# Set sticky bit
chmod +t directory

# Numeric mode (1XXX)
chmod 1755 directory

Combining Special Bits

Special bits are represented by an additional digit in the numeric mode:

Special Bit Numeric Value
Setuid 4
Setgid 2
Sticky Bit 1

Example:

# Set setuid and setgid bits
chmod 6755 file  # (4+2)=6, then regular permissions

# Permissions: rwxr-sr-x

Changing Permissions Recursively

Use the -R option to change permissions of directories and their contents recursively.

Examples

# Set permissions recursively
chmod -R 755 /path/to/directory

# Add execute permission for all directories recursively
find /path/to/directory -type d -exec chmod 755 {} \;

View Current Permissions

Use ls -l to view file permissions:

ls -l file
-rw-r--r-- 1 user group  1234 Oct 12 12:34 file

The first 10 characters represent the file type and permissions:

Common Permission Settings

Permission Numeric Mode Description
rwxr-xr-x 755 Common for executable files and directories.
rw-r--r-- 644 Default for text files; owner can read/write, others can read.
rwx------ 700 Private files and directories; only owner has full access.
rw-rw-r-- 664 Owner and group can read/write; others can read.
rwsr-xr-x 4755 Setuid executable; runs with owner's permissions.
rwxr-sr-x 2755 Setgid executable; runs with group's permissions.
rwxrwxrwt 1777 Sticky bit set on directory; e.g., /tmp.

Advanced Examples

Remove All Permissions for Others

chmod o= file

Give Execute Permission to User and Group

chmod ug+x file

Set Permissions Based on Another File

chmod --reference=source_file target_file

Using chmod with Find

Modify permissions for files matching certain criteria:

# Find all .sh files and make them executable
find /path -type f -name "*.sh" -exec chmod +x {} \;

# Change permissions of all directories to 755
find /path -type d -exec chmod 755 {} \;

# Change permissions of all files to 644
find /path -type f -exec chmod 644 {} \;

Security Considerations

Summary of chmod Options

Option Description
-R Recursively change permissions of directories and their contents.
-v Verbose output; show files as permissions are modified.
-c Like verbose but reports only when changes are made.
-f Suppress most error messages.
--reference=FILE Set permissions to match those of FILE.

Additional Tips

Return to Home