linux命令 chmod
In this guide, you will learn about the chmod command. Linux chmod command is one of the most commonly used commands especially by system administrators when assigning modifying file and folder permissions.
在本指南中,您将了解chmod命令。 Linux chmod命令是最常用的命令之一,尤其是系统管理员在分配修改文件和文件夹权限时尤其如此。
It’s usually used when installing and configuring various services and features in a Linux system. The command is usually used together with a set of octal notations or alphabetical characters to change file permissions.
通常在Linux系统中安装和配置各种服务和功能时使用。 该命令通常与一组八进制符号或字母字符一起使用,以更改文件权限。
Linux文件权限 (Linux File Permissions)
Each file on the Linux systems bears a set of permissions. There are 3 permission types that are associated with a file.
Linux系统上的每个文件都具有一组权限。 与文件相关联的3种权限类型。
- Read permissions identified by ‘r‘ 读取由“ r ”标识的权限
- Write permissions identified by ‘w‘ 写权限由“ w ”标识
- Execute permissions identified by ‘x‘ 执行权限由“ x ”标识
To check the file permissions of any file, use the ls command and the -l
option as shown below.
要检查任何文件的文件许可权,请使用ls命令和-l
选项,如下所示。
$ ls -l
For instance, to list permissions of file1.txt run the command.
例如,要列出file1.txt的权限,请运行命令。
$ ls -l file1.txt
Sample Output
样本输出
Let’s break down this output
让我们分解一下输出
The first hyphen (-) denotes that the permissions belong to a file, and not a folder. Permissions of a folder begins with a symbol d to denote a directory as shown.
第一个连字符(-)表示权限属于文件,而不是文件夹。 文件夹的权限以符号d开头,以表示所示的目录。
ls -l /var/www
Sample Output
样本输出
In the output, the string rwxr-xr-x
denotes the permissions of this file. This is divided into 3 sections as follows.
在输出中,字符串rwxr-xr-x
表示该文件的权限。 分为以下三个部分。
- user permissions
(rwx)
– This represents the permissions of the owner or user who created the file. Therwx
implies that the owner can read, write and execute the file. 用户权限(rwx)
–代表创建文件的所有者或用户的权限。rwx
表示所有者可以读取,写入和执行文件。 - Group permissions
(r-x)
-This represents permissions belonging to users in the file’s group. The r-x permissions imply that the group users can read and execute the file, but cannot write onto the file. 组权限(rx)
-这表示属于文件组中用户的权限。 rx权限表示组用户可以读取和执行文件,但不能写入文件。 - Other user permissions
(r-x)
– These are permissions for other users who neither belong to the two categories as discussed above. In this case, other users can only read the file. 其他用户权限(rx)
–这些权限是针对不属于上述两种类别的其他用户的权限。 在这种情况下,其他用户只能读取该文件。
权限模式 (Permission Modes)
There are 2 permission modes that can be passed to chmod
command:
可以将两种权限模式传递给chmod
命令:
- Octal notation 八进制表示法
- Alphabetical characters 字母字符
1.八进制表示法 (1. Octal notation)
Consider the permissions rwx
. This implies the following:
考虑权限rwx
。 这意味着以下内容:
r=4
w=2
x=1
so for instance, rwx
is the equivalent of 4+2+1 which is equal to 7.
例如, rwx
等于4 + 2 + 1等于7。
The corresponding numerical values to each of the alphabets are added to get the file permissions.
每个字母的相应数字值被添加以获得文件许可权。
For example, a file with permissions rwxr-xr--
will have an octal notation of 754
.
例如,具有权限rwxr-xr--
的文件的八进制表示法为754
。
Here’s why:
原因如下:
Calculation
计算方式
rwx = 4+2+1 = 7
r-x = 4+0+1 = 5
r-- = 4+0+0 = 4
As you may have noted, the hyphen takes the null value and is assigned 0.
您可能已经注意到,连字符采用空值并被分配为0 。
Let’s take another example:
让我们再举一个例子:
Say we have another file with permissions rwx-rw-rw-
.
假设我们还有一个权限为rwx-rw-rw-
。
The octal notation would be calculated as follows:
八进制符号的计算方式如下:
Calculation
计算方式
rwx = 4+2+1 = 7
r-x = 4+2+0 = 6
r-- = 4+2+0 = 6
Ultimately, this would give us 766
as the corresponding octal notation to rwx-rw-rw-
.
最终,这将给我们766
作为rwx-rw-rw-
的相应八进制符号。
使用chmod命令使用八进制表示法更改文件权限 (Changing file permissions with chmod command using octal notation)
To change file permissions of a file use the syntax below.
要更改文件的文件许可权,请使用以下语法。
chmod [octal value] file-name
For example, to change file permissions of a file file1.txt, to say rw-r--r--
execute:
例如,要更改文件file1.txt的文件许可权,例如说rw-r--r--
执行:
chmod 644 file1.txt
This is illustrated in the calculation below
在下面的计算中说明了这一点
(user) rw- = 4+2+0 = 6
(group) r-- = 4+0+0 = 4
(others)r-- = 4+0+0 = 4
2.字母符号 (2. Alphabetical Notation)
In alphabetical notation, the write permissions are segmented into 3 sections with each section bearing the rwx
sections.
用字母符号表示,将写许可权分为3个部分,每个部分带有rwx
部分。
From the left, we have the following notations:
从左侧开始,我们有以下符号:
u (user)
g (group)
o (others)
a (all)
This is better illustrated below:
如下所示:
使用chmod命令使用字母符号更改文件权限 (Changing file permissions with chmod command using alphabetical notation)
To change file permissions using alphabetical notation, use the syntax below.
要使用字母符号更改文件权限,请使用以下语法。
chmod [user type(u/g/o/a)] [add/revoke(+/-)] [permission type(r/w/x)]
For instance to change permissions of the owner of a file to read and write, execute:
例如,要更改文件所有者的读写权限,请执行:
chmod u+rw file1.txt
To give write permissions to everyone, execute:
要向所有人授予写权限,请执行:
chmod a+w file1.txt
To remove the write permission for all other users, we run:
要删除所有其他用户的写许可权,我们运行:
chmod o-w file1.txt
To change the permissions of a directory, we run:
要更改目录的权限,我们运行:
chmod [permission] [directory name]
To change the permissions of a directory with its files and sub-directories recursively, we run:
要递归更改目录及其文件和子目录的权限,我们运行:
chmod -R [permission] [directory name]
For example, to set the permission to 755
recursively to /var/www/
diirectory execute the command.
例如,要将/var/www/
diirectory的权限递归设置为755
,请执行以下命令。
chmod -R 755 /var/www
We hope this article was insightful and helped you with the basics of the chmod command usage. As always, your feedback is most welcome.
我们希望本文很有见地,并可以帮助您了解chmod命令用法的基础知识。 与往常一样,我们非常欢迎您提供反馈。
翻译自: https://www.journaldev.com/30675/linux-chmod-command-examples
linux命令 chmod