
Linux permissions control how users access, modify, and execute files or directories on a system. Efficient management of user permissions improves system security and access control by preventing unauthorized access to sensitive data, files, and directories.
This article explains how to manage file and directory permissions in Linux. You will explore the available permission modes and use the chmod and chown commands to change permissions on your Linux workstation.
Linux permissions are categorized into read, write, execute (RWX), which define the user or group privileges to a file or directory. Each Linux permission provides a specific privilege level, as described below.
r): View the contents of a file or list the contents of a directory.w): Modify, add, remove, or rename files in a directory.x): Run a file or access the files in a directory.Linux permissions can be assigned to the following user classes:
If a user owns a file and belongs to its group, the owner's permissions override the group's permissions. If the owner has read permissions (r), but no write or execute permissions, they cannot write or execute a file even if the group has full permissions (rwx).
To grant users specific group permissions, you can add them to a group using the usermod or gpasswd command.
Files and directories use a 10-character format to represent the user permissions. You can view the string-based permissions on a file or directory by running a command such as ls -l. Within the output:
The first character represents the file type, such as:
-: Regular file.d: Directory.l: Symbolic link.b: Block device or special file, such as disks and partitions.c: Character device or special file, such as terminals and serial ports.p: Named pipe.s: Socket.D: Door.The next nine characters represent the user and group permissions on a file or directory in the following order:
Below is an example of a String-Based Permission.
For example:
In the above ls -l command output:
d: Specifies that example.com is a directory.rwx: Represents the owner permissions to the directory. The user has read, write, and execute (rwx) permissions to the directory based on the above output.rw-: Represents the Group permissions. The group has read and write (rw) permissions based on the above output.r--: Represents the permissions for other users. Other system users have read-only r permissions to the directory.Linux permissions use binary values to represent different access levels with the following format.
For example:
4 + 2 = 6 (binary: 110, symbol: rw-)4 + 1 = 5 (binary: 101, symbol: r-x)2 + 1 = 3 (binary: 011, symbol: -wx)4 + 2 + 1 = 7 (binary: 111, symbol rwx)Below are the common Linux permissions and the representation for the owner, group, and other users.
The owner can read and write the file based on the 644 representation while the group and other users can only read the file.
The owner has full permissions (read, write, and execute) while the group and other users have read and execute permissions based on the above 755 representation.
The owner, group, and other users have full permissions to the file based on the above 777 representation.
The root user can override all Linux user permissions on a file or directory. Using sudo while reading or executing a file enables the root superuser privileges, overriding any existing permissions.
Long listing a file or directory using the ls -l displays the file permissions, ownership, size, and modification time. Long listing a directory with the -d option displays its permissions instead of the contents. Follow the steps below to view the file and directory permissions using the ls command.
Create a new sample file such as file.txt and a directory such as /var/www/html.
Use the ls -l command to view the user permissions on a file.
Your output should be similar to the one below:
Within the above output:
-: Shows that file.txt is a regular file.rwx: Represents the owner's permissions.r-x Represents the group permissions.r-x: Represents other user permissions on the file.Use the ls -ld command to view the /var/www/html directory permissions.
Your output should be similar to the one below:
Within the output:
d: Shows that /var/www/html is a directory.rwx: Represents the directory owner's permissions.r-x: Represents the group permissions.r-x: Represents other user permissions on the directory.You can change file and directory permissions using the chmod command in two modes, the numeric and symbolic mode. Follow the steps below to change file and directory permissions using the chmod command.
Use the following syntax to change permissions using the chmod command.
You can assign permissions using three digits representing the owner, group, and others in numeric mode. Each digit is the sum of read (4), write (2), and execute (1) permissions as illustrated below.
r): 4w): 2x): 1-): 0For example:
Change the file.txt permissions to 755.
The above command sets the permissions file.txt permissions to 755 to allow all users to read and execute the file, but only the owner to modify it.
Change the /var/www/html directory permissions to 755.
The above command sets the /var/www/html directory permissions to 755, allowing all users to view the directory contents, but only the owner can add new files.
Recursively apply permissions to all files and subdirectories in a directory using the -R option.
The above command enables 755 permissions for all files and subdirectories in the /var/www/html directory, allowing all users to navigate the directory read and execute files, but only the owner can modify the contents.
Linux permissions are represented by symbols or letters in symbolic mode with the following classification.
u: Represents file or directory owner.g: Represents the group associated with a file or directory.o: Represents other system users who are neither the owner nor part of the group.a: Represents all users, including the owner, group, and others.Use the following operators to assign, remove, or set permissions:
+: Adds a permission.-: Removes a permission.=: Explicitly sets the permission and overwrites the active permissions.Use the following chmod command syntax to change permissions using the symbolic mode.
For example:
Grant the owner read (r), write (w), and execute (x) permissions on file.txt.
Remove execute (x) permission from the group on the /var/www/html directory.
Enable read (r) permissions for all users on file.txt.
Change the file owner's permission to read-only, removing write (w) and execute (x) permissions.
Use the -R option to apply permissions recursively on the /var/www/html directory.
The above command adds read (r) and execute (x) permissions for all users on the /var/www/html and its contents.
Modify multiple user categories in a single command.
The above command adds read (r) and write (w) permissions to both the owner and group on file.txt.
Modify different permissions for multiple user classes on file.txt in a single command.
The above command enables read (r), write (w), and execute (x) permissions for the owner, write (w) permission for the group, and execute (x) permissions to other users on file.txt.
Sticky bit, Set User ID (SUID), and Set Group ID (SGID) are special permissions that enable administrators to control file and directory access in multi-user environments. Follow the sections below to use special permissions on files and directories on your workstation.
Sticky bit applies only to directories and ensures that only the owner can rename, delete, or move files in a directory. Sticky bit supports both numeric and symbolic modes to enable special permissions. Follow the steps below to enable sticky bit permissions on the /var/www/html directory you created earlier.
Use the +t option to enable sticky bit on the /var/www/html directory.
Use the 1 option with the chmod command to enable sticky bit on a directory. For example, set the /var/www/html directory permissions to 755 and enable sticky bit.
Within the above command:
1: Enables the sticky bit.7: Grants read (r), write (w), and execute (x) permissions to the owner.5: Grants read (r) and execute (x) permissions to the group.5: Grants read (r) and execute (x) permissions to other users.List the directory permissions to verify the sticky bit changes.
Your output should be similar to the one below:
Sticky bit is active on the /var/www/html directory based on the t option in the above output.
Set User ID (SUID) enables the execution of a file with the privileges of its owner instead of the active user running it. This is useful for executable files such as scripts that may require elevated privileges. Follow the steps below to use the SUID permission on your Linux workstation.
Create a sample hello.sh script to use as an executable file.
Use the u+s option with chmod command to enable the SUID bit on hello.sh.
The above command allows all users to execute the hello.sh file with the permissions of the file owner.
Use numeric mode by adding 4 as the leading digit in the permission value to enable SUID.
Within the above command:
4: Enables the SUID bit.7: Grants read (r), write (w), and execute (x) permissions to the owner.5: Grants read (r) and execute (x) permissions to the group.5: Grants read (r) and execute (x) permissions to other users.View the hello.sh script permissions to verify that the SUID bit is active.
Your output should be similar to the one below:
The SUID bit is active on the hello.sh script based on the s value in the owner permissions (rws).
Set Group ID (SGID) applies to executable files and directories and enables file execution using the group's permissions instead of the user's permissions. Follow the steps below to enable SGID with files and directories on your Linux workstation.
Use the g+s option with the chmod command to enable SGID permissions. For example, enable SGID on the hello.sh script.
Enable SGID on the /var/www/html directory.
Use the 2 numeric mode to enable SGID while setting file permissions. For example, enable SGID and 755 permissions on the hello.sh script.
Within the above command:
2: Enables the SGID bit.7: Grants read (r), write (w), and execute (x) permissions to the owner.5: Grants read (r) and execute (x) permissions to the group.5: Grants read (r) and execute (x) permissions to other users.View the hello.sh file permissions and verify that SGID is active.
Your output should be similar to the one below:
The SGID bit is active on the hello.sh script based on the s option in the group permissions (-rwxr-sr-x) in the above output.
List the /var/www/html permissions and verify that SGID is active.
Your output should be similar to the one below:
The SGID bit is active on the /var/www/html directory based on the s option in the group's permissions (drwxr-sr-x) in the above output.
You can change user and group ownership permissions on files or directories using the chown command. Follow the steps below to change the user and group ownership permissions using the chown command on your Linux workstation.
Use the following chown command syntax when changing user and group ownership permissions:
For example:
Grant linuxuser ownership privileges to file.txt.
The above command sets linuxuser as the file.txt owner while keeping the group ownership unchanged.
Grant the www-data group ownership privileges to the /var/www/html directory.
The above command sets the www-data as the /var/www/html directory owner while the file ownership permissions remain unchanged.
Specify the user and group separated by : to change both the owner and group of a file or directory. For example, grant the www-data user and group ownership privileges to the /var/www/html directory.
Use the R option to recursively change the owner and group for all files and subdirectories. For example, grant the www-data user and group ownership privileges to the /var/www/html directory and all subdirectories.
Use the --reference option to copy the ownership permissions of one file to another. For example, copy the /var/www/html user and group permissions to file.txt.
View the owner and group permissions of file.txt and the /var/www/html directory to verify the changes.
Your output should be similar to the one below:
You have managed file and directory permissions on your Linux workstation. Permissions define the access level and how system users interact with files or directories. You can use the chmod and chown commands to set permissions for specific files or directories to set up user access levels. For more information, run the man chown and man chmod commands to view the respective manual pages on your Linux workstations respectively.
0 Comments
Be the first to comment and share your perspective with the community.