File permissions are an important aspect of Linux security, allowing you to control who can access, modify, or execute files on your system. In this tutorial, we will cover everything you need to know about Linux file permissions, including the chmod
command and all the permission values and their privileges.
Contents
Recommended Books
I can highly recommend these books on Linux Administration if you want to go further.
- The Linux Bible 10th Edition
- The Linux Command Line, 2nd Edition: A Complete Introduction
- Linux Administration: The Linux Operating System and Command Line Guide for Linux Administrators
Understanding Linux File Permissions
Every file on a Linux system has three types of permissions: read, write, and execute. These permissions are assigned to three categories of users: the file owner, the file group, and everyone else (others). The file owner is the user who created the file, the file group is a collection of users who have been granted permission to access the file, and everyone else includes all other users who are not the owner or in the group.
There are three different types of file permissions that can be assigned to each of the three categories of users:
- Read permission (
r
): Allows users to read the contents of a file or directory. - Write permission (
w
): Allows users to modify or delete a file or directory. - Execute permission (
x
): Allows users to execute a file or access the contents of a directory.
Each permission can be enabled or disabled for each of the three categories of users, using a combination of numbers and letters. The letters represent the categories of users, and the numbers represent the permission values.
Permission Values
In Linux, permission values are represented by three numbers: the owner permission value, the group permission value, and the others permission value. Each permission value is a combination of read (4), write (2), and execute (1), with the numbers added together to create the desired permission value.
For example, the permission value 7 represents read, write, and execute permissions for a user, while the permission value 6 represents read and write permissions for a user.
The following table shows all the possible permission values and their corresponding privileges:
Permission Value | Binary | Octal | Permissions |
---|---|---|---|
0 | 000 | 0 | No permissions |
1 | 001 | 1 | Execute permission |
2 | 010 | 2 | Write permission |
3 | 011 | 3 | Write and execute permissions |
4 | 100 | 4 | Read permission |
5 | 101 | 5 | Read and execute permissions |
6 | 110 | 6 | Read and write permissions |
7 | 111 | 7 | Read, write, and execute permissions |
Using the chmod Command
The chmod
command is used to change the file permissions for a file or directory. The command takes the form:
chmod [options] [permission value] [file/directory name]
The options/flags are used to modify the behavior of the command, and the permission value is the desired permission value. The file/directory name is the name of the file or directory that the permissions are being changed for.
Here are some common options (flags) for the chmod
command:
-c
: Displays a message only if the permissions are changed.-f
: Suppresses error messages.-R
: Changes the permissions recursively, for all files and subdirectories in a directory.-v
: Displays a message for every file that the permissions are changed for.
To change the file permissions using chmod, you need to specify the permission value, which is a combination of the letters and numbers that represent the permissions for the owner, group, and others. Here are some examples:
File permissions
This command sets the file permissions for myfile.txt
to read and write permissions for the owner, and read-only permissions for the group and others.
chmod 644 myfile.txt
This command sets the file permissions for myscript.sh
to allow the owner to read, write, and execute the file, while allowing the group and others to only execute the file.
chmod 755 myscript.sh
Directory permissions
This command sets the directory permissions for mydir
to allow the owner, group, and others to read, write, and execute the directory.
chmod 777 mydir
This command sets the directory permissions for mydir
to allow the owner, group, and others to read, write, and execute the directory. The -R
option stands for recursive, which means that the command will apply the specified permission changes to all files and directories within the target directory and its subdirectories.
chmod -R 777 mydir
Directories only
You can use the find
command along with chmod
to restrict the permissions change to directories only. The find
command can be used to search for files and directories based on various criteria, and it can be combined with the -type
option to restrict the search to only directories.
To chmod 777
a directory and recursively set the same permissions for all directories underneath it, while leaving the files untouched, you can use the following command:
find /path/to/directory -type d -exec chmod 777 {} \;
Files only
To chmod 644
a directory and recursively set the same permissions for all files underneath it, while leaving the directories untouched, you can use the following command:
find /path/to/directory -type f -exec chmod 644 {} \;
In this command, -type f
restricts the find
command to only search for regular files, and the -exec
option executes the chmod
command on each file that is found. The {}
placeholder is replaced with the name of each file that is found, and the backslash and semicolon \;
are used to terminate the command.
This command will set read and write permissions for the owner and read-only permissions for the group and others on all files within the target directory and its subdirectories, while leaving the directory permissions unchanged. As with any permission change, it is important to consider the potential risks and use this command with caution.
Using letters for chmod
In addition to using the numerical values to set file permissions, you can also use letters to specify which permissions to enable or disable. Here are the letters you can use:
r
: Read permissionw
: Write permissionx
: Execute permissionu
: User/ownerg
: Groupo
: Othersa
: All (user, group, and others)
To add or remove permissions for a specific user, group, or others, you can use the following format:
chmod [u/g/o][+/-][r/w/x] [file/directory name]
For example, to add write permission for the owner of a file, you can use the following command:
chmod u+w myfile.txt
To remove execute permission for the group of a file, you can use the following command:
chmod g-x myfile.txt
Conclusion
Linux file permissions are a powerful tool for controlling access to your system’s files and directories. By using the chmod command, you can easily set the appropriate permissions for your files and directories, ensuring that only authorized users can access, modify, or execute them. Remember that file permissions are an important aspect of system security, so it’s important to use them correctly and consistently to protect your system and data.
See Also
- The Ultimate Web Server on Ubuntu 22.04
- Top 10 Keyboard Shortcuts for Linux
- Easily Unzip Files in Linux
- Top 15 Linux Bash Commands
Comments
Richard Brereton (Author) #
test message
Comment