
The Linux chmod (Change Mode) command allows you to change permissions for files and directories. Chmod is crucial for managing user privileges on specific files and directories to ensure strict security and accessibility to specific authorized users. The command allows users to control who can access or modify particular files while maintaining the flexibility for collaboration in multi-user environments.
This article explains how to use the chmod command in Linux and provides practical examples to help you effectively manage file and directory permissions.
Every Linux file and directory has associated users and permissions that determine who can read, write, or execute the file as explained below.
Linux permissions involve three types of users:
Linux uses the following combination of letters or octal numbers to represent permissions:
r: Read: Allows users to view files and directory contents.w: Write: Allows users to write, edit, and modify files and directory contents.x: Execute: Allows users to execute a file. For example, a script or binary program.Linux displays files and directories permissions using a string of r,w, and x letters depending on the active permissions. For example, rwxr-xr-- represents:
rwx: The owner has read, write, and execute permissions.r-x: All group users have read and execute permissions.r--: All system users (others) have read permissions.Use the long list command (ls -l) to view the active permissions for a file or directory in a detailed format. For example, run the following command to view the active permissions for the sample.txt file.
Output:
In the above output:
rwx) permissions.r-x) permissions.r--) permissions.chmod Command SyntaxThe following is a basic chmod command syntax:
In the command:
[options]: Modifies the command's behavior. This flag is optional.mode: Specifies the new permissions mode.file/directory: Sets the file or directory affected by the permissions.chmod Command OptionsThe following are the most common chmod command options:
This article uses the following sample files and directories to test permissions. Follow the steps below to create the samples.
Create three sample .txt files.
Create directory, directory1, dir, shared, and shared/directory sample directories.
Create a new backup.sh file.
When running the chmod command, you can specify permissions using symbolic or numeric modes. Follow the sections below to use the chmod command with symbolic and numeric modes.
This mode represents permissions using letters:
u: User.g: Group.o: Others.a: All (owner, group, and others).To change permissions in symbolic mode, use the following operators:
+: Adds a permission.-: Removes a permission.=: Sets a permission (removes all other permissions not specified).For example:
u+r: Sets read permission for the owner.g-w: Removes write permission for the group.o=x: Sets execute permission for others.chmod Command in Symbolic ModeGrant execute permissions to the owner of the file.txt file.
The u represents the user (owner) and +x adds the execute permission.
Remove write permissions for a group.
The g represents the group, and -w removes the write permissions.
Grant read and write permissions to others.
The o refers to others while =rw sets the read and write permissions.
This mode allows you to set permissions using a three-digit octal number representing read, write, or execute permissions. The following are the numeric values for each permission:
4: Read.2: Write.1: Execute.You can also combine the numbers to form a specific group of permissions as follows:
7 (rwx): Read (4), write (2), and execute (1).6 (rw-): Read (4) and write (2).5 (r-x): Read (4) and execute (1).4 (r--): Read only.3 (-wx): Write (2) and execute (1).2 (-w-): Write only.1 (--x): Execute only.0 (---): No permissions.For example:
777: Grants full read, write, and execute permissions for all users.755: Grants full access for the owner and sets only read and execute permissions for the group and other users.644: Grants read and write permissions for the owner and read-only permissions for the group and other users.chmod Command in Numeric ModeGrant 755 permissions to the file.txt file.
The above command sets read, write, and execute permissions (7) for the file owner only and read and execute permissions (55) for the group and others.
Set the file permissions to 644.
The above command sets read and write permissions (6) for the file owner and only read permissions (44) for the group and others.
Change the permissions mode for the directory1 and all files in the subdirectories to 755.
The above command grants the directory owner full permissions and sets read and execute permissions for the group and others.
Change the file.txt permissions and display a detailed progress.
The -v option displays detailed processing information.
Change permissions for the sample directory1 and display a detailed progress.
The -R option sets a recursive mode, and the -v option displays detailed processing information.
Change the permissions on multiple files at once.
The above command grants read and write permissions (6) to the file owner and read-only permissions (44) to the group and other users.
chmod with Special SUID, SGID, and Sticky Bit PermissionsLinux offers special permissions that allow enhanced control when executing files and managing directories in addition to the standard read, write, and execute permissions. Follow the steps below to use the chmod command with special Linux permissions.
When you set the SUID bit on an executable file, the file runs with the owner's permissions, regardless of the user who executes the file. SUID is useful when standard users must run files that require elevated privileges. Follow the steps below to use SUID with the chmod command.
Set the SUID bit on. For instance, to set the SUID bit on for the file1.txt file, run the following u+s command.
The above command allows all users to execute the file with the owner's permissions.
The SGID bit applies to both files and directories:
Set the SGID bit on a directory such as directory1.
The above command allows any files created within the directory to inherit the directory's group instead of the user's group.
The sticky bit applies to directories and ensures that only the file owner or the root user can delete or rename files within the directory. Sticky bit disables modification privileges even if the group or other users have write permissions on a directory. Use this permission with shared directories like /tmp.
Set a directory's sticky bit on. For instance, directory.
The above command ensures that only the file owner or the root user can delete or rename files in the directory.
chmod Command UsageFind all .txt files and set the file permissions to 644.
The above command finds all .txt files in your working directory . and executes the chmod 644 command to apply new permissions to each file.
Set specific permissions for all files in a directory but keep the directory unchanged.
The above command finds all regular files and sets their permissions to 644.
Set directory permissions that allow all users in a group to add files and limit deleting files to the file owner or the root user.
In addition to the sticky bit, the above command grants read, write, and execute permissions for the subdirectory's owner and group.
Allow only the backup.sh file owner to execute the file and restrict other users.
The above command grants the file owner read, write, and execute permissions and removes all permissions to the group and other users.
You have used the chmod command in Linux to manage file and directory permissions effectively. The chmod command provides access control for your Linux system. For more flexibility, security, and advanced permissions, use the SUID, SGID, and sticky bit permissions.
0 Comments
Be the first to comment and share your perspective with the community.