File Permissions in Linux chmod, chown, and chgrp

By Raman Kumar

Updated on Oct 05, 2024

In this tutorial, we'll learn file permissions in Linux chmod, chown, and chgrp. 

File permissions in Linux are crucial for managing access control and ensuring that users can only access files or directories they are authorized to use. We’ll break down file permissions and how to manage them using the chmod, chown, and chgrp commands. An Advanced Guide to chmod, chown, and Access Control in Linux

Prerequisites

File Permissions in Linux

1. Understanding Linux File Permissions

Linux file permissions control which users can read, write, and execute files. There are three main components:

User (Owner): The owner of the file.
Group: A group of users who have shared access.
Others: All other users on the system.

Each file or directory has three sets of permissions:

  • Read (r): Allows a user to read the contents of a file.
  • Write (w): Allows a user to modify a file.
  • Execute (x): Allows a user to execute a file (or traverse a directory).

The permissions are represented in symbolic form (rwx) or numerical form using octal values (0-7).

To view file permissions, use the ls -l command:

ls -l

Output:

-rwxr-xr-- 1 user group 4096 Oct  5 12:00 file.txt

The first part -rwxr-xr-- shows the file type and permissions.
user is the owner of the file.
group is the group that the file belongs to.

2. Modifying Permissions with chmod

The chmod (change mode) command is used to modify the file or directory permissions.

Using Symbolic Mode

You can change permissions using the symbolic representation:

  • u: User (owner)
  • g: Group
  • o: Others
  • a: All (user, group, others)

Common operations:

  • +: Add permission
  • -: Remove permission
  • =: Set exact permissions

Example: Add execute permission for the user:

chmod u+x file.txt

Example: Remove write permission for others:

chmod o-w file.txt

Example: Set exact permissions for group (read and execute):

chmod g=rx file.txt

Using Numeric Mode

In numeric mode, each set of permissions is represented by a number from 0 to 7 as described earlier.

Example: Give the owner full permission (7), the group read and execute permission (5), and others read-only permission (4):

chmod 754 file.txt

3. Changing File Ownership with chown

The chown command is used to change the owner and group of a file or directory.

Syntax

chown [OPTION] USER[:GROUP] FILE
USER: The new owner of the file.
GROUP: The new group (optional).

Example: Change the owner of a file to newuser:

chown newuser file.txt

Example: Change the owner to newuser and the group to newgroup:

chown newuser:newgroup file.txt

Example: Change the group only:

chown :newgroup file.txt

Recursive Ownership Change: To change ownership of all files and directories within a directory, use the -R option.

chown -R newuser:newgroup /path/to/directory

4. Changing Group Ownership with chgrp

The chgrp command is used to change the group ownership of a file or directory.

Syntax

chgrp [OPTION] GROUP FILE

Example: Change the group ownership to newgroup:

chgrp newgroup file.txt

Recursive Group Change: To change the group ownership of all files and directories within a directory, use the -R option.

chgrp -R newgroup /path/to/directory

5. Practical Examples

Example 1: Setting Permissions for a Script

You have a script called backup.sh that you want only the owner to execute and others to have no access.

chmod 700 backup.sh

This command gives the owner full control (rwx), and no permissions for the group and others (---).

Example 2: Changing Ownership of a Project Directory

You want to transfer ownership of the /project directory to newuser and the group to developers.

chown -R newuser:developers /project

This command recursively changes the ownership of all files and subdirectories under /project.

Example 3: Changing Group Ownership for a Shared Directory

You have a directory /shared that multiple users in the group staff should be able to access.

chgrp -R staff /shared
chmod -R 770 /shared

This ensures that all users in the staff group have read, write, and execute permissions (rwx), while others have no access.

Advanced Topics on File Permissions in Linux

Once you’re comfortable with basic file permissions and commands like chmod, chown, and chgrp, there are several advanced topics that can help you handle more complex permission scenarios in Linux. These include setuid, setgid, the sticky bit, and access control lists (ACLs). Let’s dive into these topics to gain a deeper understanding of how permissions work in advanced use cases.

1. The setuid and setgid Bits

setuid (Set User ID)

The setuid (Set User ID) bit allows a file to be executed with the permissions of the file’s owner rather than the permissions of the user running the file. This is particularly useful for executable files that need elevated privileges to perform specific tasks.

How it works: When an executable file with the setuid bit is run, the process runs with the file owner’s privileges, typically root, instead of the privileges of the user who launched the process.

Setting setuid: You can set the setuid bit using the symbolic mode u+s or using the octal number 4.

Example: Set the setuid bit on an executable file:

chmod u+s /path/to/executable

Example: Using octal mode:

chmod 4755 /path/to/executable

Here, 4 sets the setuid bit, and 755 gives the file owner rwx permissions and others rx permissions.

Security Note: The setuid bit can pose a security risk if used improperly, so use it with caution, especially on scripts and binaries.

setgid (Set Group ID)

The setgid (Set Group ID) bit allows files or directories to inherit the group ownership of the directory in which they are created.

Files: When set on an executable file, the process runs with the group permissions of the file’s group.

Directories: When set on a directory, new files created in that directory inherit the group ownership of the directory, rather than the user’s default group.

Setting setgid: You can set the setgid bit using g+s or using the octal number 2.

Example: Set the setgid bit on a directory:

chmod g+s /path/to/directory

Example: Using octal mode:

chmod 2755 /path/to/directory

Here, 2 sets the setgid bit, and 755 sets the permissions for the owner, group, and others.

Use Case: The setgid bit is often used in shared project directories, ensuring that all files created within the directory belong to the same group.

2. The Sticky Bit

The sticky bit is mainly used on directories to control the deletion of files. When the sticky bit is set on a directory, only the file owner, directory owner, or root can delete or rename files within that directory, regardless of the directory’s write permissions.

Setting the Sticky Bit: You can set the sticky bit using the symbolic mode o+t or using the octal number 1.

Example: Set the sticky bit on a shared directory:

chmod o+t /path/to/shared-directory

Example: Using octal mode:

chmod 1777 /path/to/shared-directory

Here, 1 sets the sticky bit, and 777 gives read, write, and execute permissions to everyone.

Use Case: The sticky bit is commonly used on directories like /tmp where multiple users have write permissions but should not be allowed to delete each other’s files.

3. Access Control Lists (ACLs)

While traditional Linux permissions are limited to specifying user, group, and others, Access Control Lists (ACLs) provide more granular control, allowing you to define permissions for multiple users or groups.

Checking ACL Support

To check if a file system supports ACLs, you can use the following command:

tune2fs -l /dev/sda1 | grep "Default mount options"

If acl is listed, your file system supports ACLs.

Setting Up ACLs

You can use the setfacl command to set ACLs on files or directories.

Granting Additional Permissions You can give specific users or groups permissions on a file or directory, beyond the basic chmod permissions.

Example: Give the user john read and write access to a file:

setfacl -m u:john:rw file.txt

Example: Give the group developers read and execute access to a directory:

setfacl -m g:developers:rx /path/to/directory

Viewing ACLs

To view ACLs on a file or directory, use the getfacl command:

getfacl /path/to/file

Removing ACLs

To remove a specific ACL entry, use:

setfacl -x u:john /path/to/file

To remove all ACL entries, use:

setfacl -b /path/to/file

Recursive ACLs

You can apply ACLs recursively to all files and subdirectories:

setfacl -R -m u:john:rw /path/to/directory

Use Case: ACLs are extremely useful when multiple users need different levels of access to shared files or directories, going beyond the standard user/group/other permission model.

4. Managing Default ACLs

Default ACLs ensure that new files and directories inherit the ACLs of the parent directory.

Setting Default ACLs

You can set a default ACL on a directory to automatically apply permissions to all new files and subdirectories created within it.

Example: Set default ACLs for the developers group on a project directory:

setfacl -d -m g:developers:rw /project-directory

This command ensures that every file or directory created under /project-directory will inherit rw permissions for the developers group.

5. Using umask to Set Default Permissions

The umask (user file creation mask) defines the default permissions that are set when new files or directories are created. It effectively "masks" permissions to ensure that certain rights are removed by default.

Understanding umask Values

The umask value is subtracted from the default permissions (777 for directories and 666 for files) to determine the permissions of newly created files or directories.

Example: A umask value of 022 results in new files with permissions 644 and new directories with permissions 755.

Setting a Temporary umask

You can temporarily set a umask value in a session:

umask 027

This command results in new files with permissions 640 and new directories with 750.

Making umask Permanent

To make the umask permanent for a user, you can add the umask command to the user's shell configuration file (~/.bashrc, ~/.bash_profile, or /etc/profile).

Conclusion

Understanding file permissions in Linux is essential for managing access control in a multi-user environment. With the chmod, chown, and chgrp commands, you can easily modify who can read, write, or execute your files. By practicing these commands, you'll gain better control over file access and security in Linux.

By mastering advanced topics such as setuid, setgid, the sticky bit, ACLs, and umask, you can gain fine-grained control over file and directory permissions in Linux. These advanced techniques allow you to manage access in complex multi-user environments, ensuring that sensitive data is protected and only accessible to the right users. Understanding and applying these tools will make you much more proficient in managing Linux systems.