Chmod Command - Linux File Permissions Guide

Learn how to use the chmod command in Linux to manage file permissions. Understand symbolic and octal notations for setting user, group, and global read, write, and execute permissions.

Chmod Command Guide

The chmod command is a fundamental utility in Linux and Unix-like operating systems used to change the access permissions of files and directories. Understanding how to use chmod is crucial for system administration and secure file management.

Understanding File Permissions

File permissions determine who can read, write, and execute a file. These permissions are categorized into three types of users:

  • User (u): The owner of the file.
  • Group (g): Members of the group associated with the file.
  • Others (o): All other users on the system.

For each user category, there are three types of permissions:

  • Read (r): Allows viewing the content of a file or listing the contents of a directory.
  • Write (w): Allows modifying or deleting a file, or creating/deleting files within a directory.
  • Execute (x): Allows running a file as a program or script, or entering a directory.

Using Symbolic Mode

Symbolic mode uses letters to represent user categories, operators, and permissions. The operators are:

  • +: Adds a permission.
  • -: Removes a permission.
  • =: Sets the permissions exactly as specified, overwriting existing ones.

Examples:

# Add execute permission for all users (myscript.sh)
chmod a+x myscript.sh

# Set user to read/write/execute, group/global to read only (myscript.sh)
chmod u=rwx,go=r myscript.sh 

# Remove write permission from user, group, and global (myscript.sh)
chmod a-w myscript.sh

# Remove all permissions from user, group, and global (myscript.sh)
chmod = myscript.sh

Using Octal Notation

Octal notation represents permissions using numbers, where each digit corresponds to a user category (user, group, others). The numeric values for permissions are:

  • 7: Full permissions (read, write, execute - rwx)
  • 6: Read and write permissions (rw-)
  • 5: Read and execute permissions (r-x)
  • 4: Read only permission (r--)
  • 3: Write and execute permissions (-wx)
  • 2: Write only permission (-w-)
  • 1: Execute only permission (--x)
  • 0: No permissions (---)

Examples:

# Set user to read/write, group and global to read only (myscript.sh)
chmod 644 myscript.sh

# Set user to read/write/execute, group and global to read/execute (myscript.sh)
chmod 755 myscript.sh

# Set user, group, and global to read/write (myscript.sh)
chmod 666 myscript.sh

Advanced Usage (ACLs)

Access Control Lists (ACLs) provide more granular control over file permissions. The following example shows how to remove ACL entries on macOS.

# Delete ACL entry with number 0 (MacOS):
# See: `man -M /usr/share/man chmod`
/bin/chmod -a# 0 /path/to/file

Further Reading

For more in-depth information on file permissions and the chmod command, consult the official documentation: