How Do You Change File Permissions on Linux?

1 min read

File permissions are a fundamental concept in Unix-like operating systems such as Linux and macOS. They control who can read, write, or execute a file or directory. Understanding and managing these permissions is crucial for system security and smooth collaboration.

Understanding File Permissions

Each file and directory has three types of permissions:

  • Read (r): Allows viewing the contents.
  • Write (w): Allows modifying the contents.
  • Execute (x): Allows running the file as a program or accessing a directory.

Permissions are set for three categories of users:

  • Owner: The user who owns the file.
  • Group: Users in the file’s group.
  • Others: Everyone else.

You can view permissions with the ls -l command:

ls -l myfile.txt

Example output:

-rwxr-xr--

This means: - Owner: read, write, execute - Group: read, execute - Others: read

Changing Permissions with chmod

The chmod (change mode) command is used to modify permissions.

Symbolic Method

You can add or remove permissions using letters:

  • u (user/owner), g (group), o (others), a (all)
  • + (add), - (remove), = (set exactly)

Examples:

  • Add execute permission for the owner:

    chmod u+x myfile.txt
    
  • Remove write permission for others:

    chmod o-w myfile.txt
    
  • Set read and write for group, remove all for others:

    chmod g=rw,o= myfile.txt
    

Numeric (Octal) Method

Permissions can also be set using numbers:

  • Read = 4, Write = 2, Execute = 1

Add the numbers for each category:

Owner Group Others
7 (rwx) 5 (r-x) 4 (r--)

Example:

chmod 754 myfile.txt

This sets: - Owner: read, write, execute - Group: read, execute - Others: read

Changing Ownership

To change the owner or group, use chown:

sudo chown alice:developers myfile.txt

Best Practices

  • Only grant necessary permissions.
  • Avoid giving write/execute to others unless required.
  • Use groups for collaborative projects.

Summary:
Changing file permissions is essential for security and collaboration. Use chmod with symbolic or numeric notation to set permissions appropriately.

You Might Also Like