Linux Comm Chmod
# Linux chmod Command
[ Linux Command Manual](#)
The Linux `chmod` (short for "change mode") command is used to control user permissions for files.
`chmod` (change mode) is a command in Linux systems used to change the permissions of files or directories. It controls the access permissions of the file owner, the owning group, and other users to the file.
Only the file owner and the superuser can modify the permissions of a file or directory.
The file access permissions in Linux/Unix are divided into three levels: File Owner (Owner), User Group (Group), and Other Users (Other Users).
!(#)
You can specify file permissions using absolute mode (octal numeric mode) or symbolic mode.
!(#)
**Required Permissions**: All users
### Syntax
```bash
chmod mode file...
chmod --reference=reference_file file...
### Common Options
* `-c` : Only display the change action if the file permissions were indeed changed.
* `-f` : Do not display error messages if the file permissions cannot be changed.
* `-v` : Display detailed information about the permission changes.
* `-R` : Apply the same permission changes to all files and subdirectories under the current directory (i.e., change recursively).
* `--help` : Display help information.
* `--version` : Display version information.
`mode` : The permission setting string, formatted as follows:
### Permission Modes
#### 1. Symbolic Mode (ugoa+/-permissions)
[ugoa...][[+-=]...][,...]
Where:
* `u` represents the owner of the file, `g` represents users in the same group as the file owner, `o` represents all other users, and `a` represents all three (equivalent to `ugo`).
* `+` adds permissions, `-` removes permissions, and `=` sets the exact permissions.
* `r` stands for read, `w` for write, `x` for execute, and `X` for execute only if the file is a directory or already has execute permission set for some user.
Using symbolic mode, you can set multiple items: `who` (user type), `operator` (operation), and `permission` (permission). Each item's setting can be separated by a comma.
The `chmod` command will modify the access permissions of the user types specified by `who`. The user type is indicated by one or more letters in the `who` position, as shown in the symbolic mode table for `who`:
| who | User Type | Description |
| --- | --- | --- |
| u | user | File owner |
| g | group | Group of the file owner |
| o | others | All other users |
| a | all | All users, equivalent to _ugo_ |
Operator symbolic mode table:
| Operator | Description |
| --- | --- |
| + | Add permissions for the specified user type |
| - | Remove permissions for the specified user type |
| = | Set the permissions for the specified user type, resetting all permissions for that user type |
Permission types:
| Mode | Name | Description |
| --- | --- | --- |
| r | read | Set read permission |
| w | write | Set write permission |
| x | execute | Set execute permission |
| X | special execute | Set execute permission only if the file is a directory or if other types of users already have execute permission |
| s | setuid/gid | When the file is executed, set the setuid or setgid permission for the file based on the user type specified by the `who` parameter |
| t | sticky bit | Set the sticky bit. Only the superuser can set this bit, and only the file owner (`u`) can use this bit |
#### 2. Octal Syntax
The `chmod` command can use octal numbers to specify permissions.
| Number | Permission |
| --- | --- |
| 4 | read (r) |
| 2 | write (w) |
| 1 | execute (x) |
**Combination Method**:
* Owner permissions (first digit)
* Group permissions (second digit)
* Other users' permissions (third digit)
**Common Combinations**:
* 755: `rwxr-xr-x`
* 644: `rw-r--r--`
* 700: `rwx------`
The permission bits of a file or directory are controlled by 9 permission bits, grouped in threes. They represent the read, write, and execute permissions for the File Owner (User), the User Group (Group), and Other Users (Other), respectively.
| # | Permission | rwx | Binary |
| --- | --- | --- | --- |
| 7 | read + write + execute | rwx | 111 |
| 6 | read + write | rw- | 110 |
| 5 | read + execute | r-x | 101 |
| 4 | read only | r-- | 100 |
| 3 | write + execute | -wx | 011 |
| 2 | write only | -w- | 010 |
| 1 | execute only | --x | 001 |
| 0 | none | --- | 000 |
For example, 765 is interpreted as:
* Owner permissions in digits: The sum of the three permission bits for the owner. For `rwx`, it's 4+2+1, which equals 7.
* Group permissions in digits: The sum of the permission bits for the group. For `rw-`, it's 4+2+0, which equals 6.
* Other users' permissions in digits: The sum of the permission bits for others. For `r-x`, it's 4+0+1, which equals 5.
### Examples
Set file `file1.txt` to be readable by everyone:
```bash
chmod ugo+r file1.txt
Set file `file1.txt` to be readable by everyone:
```bash
chmod a+r file1.txt
Set files `file1.txt` and `file2.txt` to be writable by the owner and their group, but not by others:
```bash
chmod ug+w,o-w file1.txt file2.txt
Add execute permission for the owner of file `ex1.py`:
```bash
chmod u+x ex1.py
Set all files and subdirectories under the current directory to be readable by everyone:
```bash
chmod -R a+r *
Additionally, `chmod` can use numbers to represent permissions, like:
```bash
chmod 777 file
The syntax is:
```bash
chmod abc file
Where `a`, `b`, and `c` are numbers representing the permissions for User, Group, and Other, respectively.
#### r=4, w=2, x=1
* To get `rwx` permissions, use 4+2+1=7;
* To get `rw-` permissions, use 4+2=6;
* To get `r-x` permissions, use 4+1=5.
```bash
chmod a=rwx file
and
```bash
chmod 777 file
have the same effect.
```bash
chmod ug=rwx,o=x file
and
```bash
chmod 771 file
have the same effect.
Using `chmod 4755 filename` can give this program root privileges.
### More Explanations
| Command | Description |
| --- | --- |
| `chmod a+r _file_` | Add read permission for all users on _file_ |
| `chmod a-x _file_` | Remove execute permission for all users on _file_ |
| `chmod a+rw _file_` | Add read and write permissions for all users on _file_ |
| `chmod +rwx _file_` | Add read, write, and execute permissions for all users on _file_ |
| `chmod u=rw,go= _file_` | Set read and write permissions for the owner of _file_, and clear all permissions for the group and other users (space means no permission) |
| `chmod -R u+r,go-r _docs_` | Add read permission for the user on directory _docs_ and all files in its subdirectory hierarchy, while removing read permission for the group and other users |
| `chmod 664 _file_` | Set read and write permissions for the owner and group of _file_, and set read permission for other users |
| `chmod 0755 _file_` | Equivalent to u=rwx (4+2+1), go=rx (4+1 & 4+1). The leading 0 has no special mode. |
| `chmod 4755 _file_` | The 4 sets the set-user-ID bit, the rest is equivalent to u=rwx (4+2+1), go=rx (4+1 & 4+1). |
| `find path/ -type d -exec chmod a-x {} ;` | Remove execute permission for all users on directory `path/` and all its subdirectories (not files). Use `-type f` to match files. |
| `find path/ -type d -exec chmod a+x {} ;` | Allow all users to browse or traverse directory `path/` |
[ Linux Command Manual](#)
YouTip