Skip to content

Commit 016660c

Browse files
authored
[NEW] Chmod guide update (#1449)
* [Update] Added octal notation section * Rephrased sentence * chmod copy edits (#1)
1 parent c0779a0 commit 016660c

File tree

1 file changed

+94
-40
lines changed

1 file changed

+94
-40
lines changed

docs/tools-reference/tools/modify-file-permissions-with-chmod.md

Lines changed: 94 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -6,101 +6,155 @@ description: 'Use the chmod command to modify file permissions on your Linode.'
66
keywords: ["TAGS=chmod", "commands", "reference", "file permissions"]
77
license: '[CC BY-ND 4.0](https://creativecommons.org/licenses/by-nd/4.0)'
88
aliases: ['linux-tools/common-commands/chmod/','tools-reference/modify-file-permissions-with-chmod/']
9-
modified: 2011-07-07
9+
modified: 2018-01-24
1010
modified_by:
11-
name: Linode
11+
name: Sam Foo
1212
published: 2010-07-01
1313
title: Modify File Permissions with chmod
1414
external_resources:
1515
- '[Manage File Permission with Users and Groups](/docs/tools-reference/linux-users-and-groups)'
1616
- '[Administration Basics](/docs/using-linux/administration-basics)'
1717
---
1818

19-
Unix-like systems, including the Linux systems that run on the Linode platform, have an incredibly robust access control system that allows systems administrators to effectively permit multiple users access to a single system without giving every user access to every file on the file system. The `chmod` command is the best and easiest way to modify these file permissions.
19+
![Modify File Permissions with chmod](/docs/assets/modify_file_permissions_with_chmod_smg.png "Modify File Permissions with chmod")
20+
21+
## chmod Lets You Change Read and Write Permissions in Linux
2022

21-
![Title graphic](/docs/assets/modify_file_permissions_with_chmod_smg.png)
23+
Unix-like systems, including the Linux systems that run on the Linode platform, have an incredibly robust access control system that allows systems administrators to effectively permit multiple users access to a single system without giving every user access to every file on the file system. The `chmod` command is the best and easiest way to modify these file permissions.
2224

23-
This document provides a brief overview of file permissions and the operation of the `chmod` command in addition to a number of practical examples and applications of `chmod`. If you find this guide helpful, please consider our [basic administration practices guide](/docs/using-linux/administration-basics) and the [Linux users and groups guide](/docs/tools-reference/linux-users-and-groups/).
25+
This guide provides a brief overview of file permissions and the operation of the `chmod` command in addition to a number of practical examples and applications of `chmod`. If you find this guide helpful, please consider our [basic administration practices guide](/docs/using-linux/administration-basics) and the [Linux users and groups guide](/docs/tools-reference/linux-users-and-groups/) next.
2426

25-
## Using Chmod
27+
## How to Use chmod
2628

2729
In this guide, `chmod` refers to recent versions of `chmod` such as those provided by the GNU project. By default, `chmod` is included with all images provided by Linode, and as part of the common "base" selection of packages provided in nearly all distributions of Linux-based operating systems.
2830

29-
<div class="wistia_responsive_padding" style="padding:56.25% 0 0 0;position:relative;"><div class="wistia_responsive_wrapper" style="height:100%;left:0;position:absolute;top:0;width:100%;"><iframe src="https://fast.wistia.net/embed/iframe/h5sfokgpgm?videoFoam=true" title="Linode - How to use the chmod command" allowtransparency="true" frameborder="0" scrolling="no" class="wistia_embed" name="wistia_embed" allowfullscreen mozallowfullscreen webkitallowfullscreen oallowfullscreen msallowfullscreen width="100%" height="100%"></iframe></div></div>
30-
<script src="https://fast.wistia.net/assets/external/E-v1.js" async></script>
31+
### Linux File Permission Basics
32+
33+
All file system objects on Unix-like systems have three main types of permissions: read, write, and execute access. Permissions are bestowed upon three possible classes: the user, the usergroup, and all system users.
34+
35+
To view the file permissions of a set of files, use:
3136

32-
### File Permission Basics
37+
ls -lha
3338

34-
All file system objects on Unix-like systems have three main types of permissions: read, write, and execute access. Furthermore, permissions are bestowed upon three possible classes: the user that owns the file system object, the user group that owns the file system object, and all system users. To view the file permissions of a set of files, use the `ls -lha` command. The output will resemble the following:
39+
In the first column of the output, there are 10 characters that represent the permission bits. To understand why they are called permission bits, see the section on [octal notation](#octal-notation) below.
3540

36-
drwxr-xr-x 2 username username 4.0K 2009-08-13 10:16 docs
37-
-rw-r--r-- 1 username username 8.1K 2009-07-09 16:23 roster.py
38-
lrwxrwxrwx 2 username username 4.0K 2009-08-13 10:16 team.docs
41+
drwxr-xr-x 2 user group 4.0K 2009-08-13 10:16 docs
42+
-rw-r--r-- 1 user group 8.1K 2009-07-09 16:23 roster.py
43+
lrwxrwxrwx 2 user group 4.0K 2009-08-13 10:16 team.docs
3944

40-
The first block of data contains information regarding the file permissions and settings, and we'll focus on that in this section. The first column specifies the type of file system object. `d` indicates that the object is a directory. `-` indicates that the object is a normal file. `l` indicates that the object is a symbolic link.
45+
A way to understand the meaning of this column is to divide the bits into groups.
4146

42-
The remaining characters represent the core permissions. In groupings of three, these characters represent read, write, and execute permissions. The first grouping represents the owners permissions, the second grouping represents the usergroup that owns the file, and the final grouping represents the permissions of all users on the system.
47+
File type | User | Group | Global
48+
--------------------|-------|-------|---------
49+
`d` Directory | `rwx` | `r-x` | `r-x`
50+
`-` Regular file | `rw-` | `r--` | `r--`
51+
`l` Symbolic Link | `rwx` | `rwx` | `rwx`
4352

44-
Any object on the file system may have any combination of permissions. Note, access to the files targeted by symbolic links is controlled by the permissions of the targeted file, not the permissions of the link object. There are [additional file permissions](/docs/tools-reference/linux-users-and-groups#additional-file-permissions) that control other aspects of access to files.
53+
The first character represents the type of file. The remaining nine bits in groups of three represent the permissions for the user, group, and global respectively. Each stands for:
4554

46-
### The Chmod Command
55+
* `r`: **R**ead
56+
* `w`: **W**rite
57+
* `x`: e**X**ecute
4758

48-
Consider the following invocation of `chmod`:
59+
Note that access to files targeted by symbolic links is controlled by the permissions of the targeted file, not the permissions of the link object. There are [additional file permissions](/docs/tools-reference/linux-users-and-groups#additional-file-permissions) that control other aspects of access to files.
60+
61+
### chmod Command Syntax and Options
62+
63+
The format of a `chmod` command is:
64+
65+
chmod [who][+,-,=][permissions] filename
66+
67+
Consider the following `chmod` command:
4968

5069
chmod g+w ~/group-project.txt
5170

52-
This grants all members of the usergroup that owns the file `~/group-project.txt` write permissions. To remove this permission later, switch the `+` sign to a `-`, as in the following example.
71+
This grants all members of the usergroup that owns the file `~/group-project.txt` write permissions. Other possible options to change permissions of targeted users are:
72+
73+
Who (Letter) | Meaning
74+
-------------|---------
75+
u | user
76+
g | group
77+
o | others
78+
a | all
79+
80+
The `+` operator grants permissions whereas the `-` operator takes away permissions. Copying permissions is also possible:
5381

54-
chmod g-w ~/group-project.txt
82+
chmod g=u ~/group-project.txt
5583

56-
You can specify multiple permissions by separating them with a comma, as in the following example:
84+
The parameter `g=u` means grant group permissions to be same as the user's.
85+
86+
Multiple permissions can be specified by separating them with a comma, as in the following example:
5787

5888
chmod g+w,o-rw,a+x ~/group-project-files/
5989

60-
This adds write permissions to the usergroup members, and removes read and write permissions from the "other" users of the system. Finally the `a+x` adds the execute permissions to all categories. This value may also be specified as `+x`. If no category is specified, the permission is added or subtracted to all permission categories. In this notation the owner of the file is referred to as the `user` (e.g. `u+x`).
90+
This adds write permissions to the usergroup members, and removes read and write permissions from the "other" users of the system. Finally the `a+x` adds the execute permissions to all categories. This value may also be specified as `+x`. If no category is specified, the permission is added or subtracted to all permission categories.
91+
92+
In this notation the owner of the file is referred to as the `user` (e.g. `u+x`).
6193

6294
chmod -R +w,g=rw,o-rw, ~/group-project-files/
6395

64-
The `-R` option applies the modification to the permissions recursively to the directory specified and all of its contents. You may also specify file permissions using the `=` sign rather than the `+` or `-` operators to signify only the specified permissions if you need to specify a set of permissions without relation to the current state of the file's permission.
96+
The `-R` option applies the modification to the permissions recursively to the directory specified and to all of its contents.
97+
98+
### How to Use Octal Notation for File Permissions
99+
100+
Another method for setting permissions is through octal notation.
101+
102+
Here is example of a file permission that is equivalent to `chmod u=rwx,go=rx`.
103+
104+
chmod 750 ~/group-project.txt
65105

66-
The notation used in this document thus far can be confusing for particularly complex file permission requirements. `chmod` provides an alternate "octal" notation that you may find more sensible:
106+
The permissions for this file are `- rwx r-x ---`.
67107

68-
0 --- indicates no permissions
69-
1 --x indicates execute permissions
70-
2 -w- indicates write permissions
71-
3 -wx indicates write and execute permissions
72-
4 r-- indicates read permissions
73-
5 r-x indicates read and execute permissions
74-
6 rw- indicates read and write permissions
75-
7 rwx indicates read, write, and execute permissions
108+
Disregarding the first bit, each bit that is occupied with a `-` can be replaced with a `0` while `r`, `w`, or `x` is represented by a `1`. The resulting conversion is:
76109

77-
Each digit is independent of the other two. Therefore, 777 creates read, write, and execute privileges for all users. 744, which is a typical default permission, allows read, write, and execute permissions for the owner, and read permissions for the group and world users. To chmod the "roster.py" file so that the owner can read, write, and execute the file, the group can read and execute the file, and the world can execute the file, issue the following command:
110+
111 101 000
78111

79-
chmod 751 ~/roster.py
112+
This is called octal notation because the binary numbers are converted to base-8 by using the digits 0 to 7:
80113

81-
Either notation is equivalent, and you may chose to use whichever form is more able to clearly express your desires for the permissions.
114+
Binary | Octal | Permission
115+
-------|-------|-----------
116+
000 | 0 | ---
117+
001 | 1 | --x
118+
010 | 2 | -w-
119+
011 | 3 | -wx
120+
100 | 4 | r--
121+
101 | 5 | r-x
122+
110 | 6 | rw-
123+
111 | 7 | rwx
124+
125+
Each digit is independent of the other two. Therefore, `750` means the current user can read, write, and execute while the group and others cannot write.
126+
127+
`744`, which is a typical default permission, allows read, write, and execute permissions for the owner, and read permissions for the group and "world" users.
128+
129+
Either notation is equivalent, and you may choose to use whichever form more clearly expresses your permissions needs.
82130

83131
## Making a File Executable
84132

85-
Issue the following command to change the file permissions so that any user can execute the file "~/group-project.py":
133+
The following examples changes the file permissions so that any user can execute the file "~/group-project.py":
86134

87135
chmod +x ~/group-project.py
88136

89137
## Restore Default File Permissions
90138

91-
In many cases the default permissions for files on a Unix system are often `600` or `644`. Permissions of `600` mean that the owner has full read and write access to the file, while no other user can access the file. Permissions of `644` mean that the owner of the file has read and write access, while the group members and other users on the system only have read access. Issue one of the following commands to achieve these "default" permissions:
139+
The default permissions for files on a Unix system are often `600` or `644`. Permissions of `600` mean that the owner has full read and write access to the file, while no other user can access the file. Permissions of `644` mean that the owner of the file has read and write access, while the group members and other users on the system only have read access.
140+
141+
Issue one of the following examples to achieve these "default" permissions:
92142

93143
chmod 600 ~/roster.txt
94144
chmod 644 ~/gigs.txt
95145

96-
For executable files, the equivalent settings would be `700` and `755` which correspond to `600` and `644` except with execution permission. Issue one of the following commands to achieve these executable "default" permissions:
146+
For executable files, the equivalent settings would be `700` and `755` which correspond to `600` and `644` except with execution permission.
147+
148+
Use one of the following examples to achieve these executable "default" permissions:
97149

98150
chmod 700 ~/generate-notes.py
99151
chmod 755 ~/regenerate-notes.py
100152

101-
## Removing all Group and World Permissions
153+
## Restrict File Access: Remove all Group and World Permissions
154+
155+
There are a number of cases where administrators and users should restrict access to files, particularly files that contain passwords and other sensitive information. The configuration files for msmtp and Fetchmail (`~/.msmtprc` and `~/.fetchmailrc`) are two common examples.
102156

103-
There are a number of cases where administrators and users would be wise to restrict access to files, particularly files that contain passwords and other sensitive information. The configuration files for msmtp and fetchmail (`~/.msmtprc` and `~/.fetchmailrc`) are two common examples. You can remove all access to these files with commands in one of the following forms:
157+
You can remove all access to these files with commands in one of the following forms:
104158

105159
chmod 600 .msmtprc
106160
chmod g-rwx,o-rwx .fetchmail

0 commit comments

Comments
 (0)