chmod
command is used to change access permission of files and directories in Linux operating systems. chmod
stands for change mode. Access permissions specify whether a user account or group can read, write, or execute a given file and directory.
chmod
命令用于更改Linux操作系统中文件和目录的访问权限。 chmod
代表更改模式。 访问权限指定用户帐户或组是否可以读取,写入或执行给定的文件和目录。
chmod命令语法 (chmod Command Syntax)
Syntax of chmod
command is like below.
chmod
命令的语法如下。
chmod OPTIONS PERMISSIONS FILE
Linux文件和文件夹访问角色 (Linux File and Folder Access Roles)
While managing access permission for files and directories we should specify the roles. Permissions are set and bind to the given roles. In Linux following roles exist for access permissions.
在管理文件和目录的访问权限时,我们应该指定角色。 权限被设置并绑定到给定角色。 在Linux中,以下角色具有访问权限。
owner
specifies the file or directory owners. It can be listed withls -l
command.owner
指定文件或目录的所有者。 可以使用ls -l
命令列出它。group
specifies the file or directory group owners. Files and directories group owners can be listed withls -l
.group
指定文件或目录组的所有者。 可以使用ls -l
列出文件和目录组所有者。other
specifies all user accounts and groups which means all other world. While giving privilege to this role be cautious.other
指定所有用户帐户和组,这意味着所有其他世界。 在授予该角色特权时要谨慎。
Linux文件和文件夹访问模式 (Linux File and Folder Access Modes)
In Linux, there are three access modes. Access modes specify the way given user or group access to the file or directory.
在Linux中,有三种访问模式。 访问模式指定授予用户或组访问文件或目录的方式。
read
mode specifies reading file or directory.read
模式指定读取文件或目录。write
mode specifies writing and changing file or directorywrite
模式指定写入和更改文件或目录execute
mode specifies executing fileexecute
模式指定执行文件
列出文件和文件夹访问权限(List File and Folder Access Permissions)
Before starting to change permissions we need to list current roles and permissions about files and directories. We will use ls -l
command to list permissions. In this example we will list all current working directory file and folders permissions.
在开始更改权限之前,我们需要列出有关文件和目录的当前角色和权限。 我们将使用ls -l
命令列出权限。 在此示例中,我们将列出所有当前的工作目录文件和文件夹权限。
$ ls -l

We can see that owner user and group is ismail
and the first column like --rw-r--r--
shows user, group and other privileges.
我们可以看到所有者用户和组是ismail
,第一列--rw-r--r--
显示了用户,组和其他特权。
-
is user or group bit-
是用户位还是组位rw-
is owner permissions. Hereread
andwrite
permission givenrw-
是所有者权限。 这里read
和write
权限给予r--
is group permission. Onlyread
permission givenr--
是组权限。 仅授予read
权限Last
r--
is other permission. Onlyread
permission given.最后一个
r--
是其他权限。 仅授予read
权限。
更改文件和文件夹用户访问权限(Change File and Folder User Access Permission)
Now those provided information to understand file and directory permissions is enough. We will change single directory permission in this example. We will change user permission of file named ping.txt
. We will use u
to specify user. +
means add permission. x
mean execute permission.
现在,那些提供的了解文件和目录权限的信息就足够了。 在此示例中,我们将更改单个目录权限。 我们将更改名为ping.txt
用户权限。 我们将使用u
来指定用户。 +
表示添加权限。 x
表示执行权限。
$ chmod u+x ping.txt
更改文件和文件夹组访问权限 (Change File and Folder Group Access Permission)
We can change a file or directory permission with g
option. In this example, we will remove the group read permission of file ping.txt
.
我们可以使用g
选项更改文件或目录的权限。 在此示例中,我们将删除文件ping.txt
的组读取权限。
$ chmod g-r ping.txt
g
used to specify groupg
用于指定组-
is used to remove given permission-
用于删除给定的权限r
is used to specify read permissionr
用于指定读取权限
更改文件和文件夹的多个组和用户访问权限(Change File and Folder Multiple Group and User Access Permissions)
We can change in a single chmod command multiple roles permissions. We will just delimit them with a command.
我们可以在单个chmod命令中更改多个角色的权限。 我们将使用命令来分隔它们。
$ chmod g-r,u+x ping.txt
递归更改文件和文件夹访问权限 (Change File and Folder Access Permission Recursively)
Changing file and directory permissions one by one is a trivial task. We can use -r
option to take effect all subdirectories recursively. In the following example, we change all given directory named test and its subdirectories permissions.
一步一步地更改文件和目录权限是一项微不足道的任务。 我们可以使用-r
选项使所有子目录递归生效。 在下面的示例中,我们更改了所有给定的名为test的目录及其子目录的权限。
$ chmod -r g-r test
添加文件和文件夹访问权限 (Add File and Folder Access Permission)
We have already seen but I want to emphasize add permission operation. +
between role and permission used to add permission to the given role. In this example we add permission to the user to execute ping.txt
我们已经看过,但是我想强调添加权限操作。 角色和权限之间的+
,用于向给定角色添加权限。 在此示例中,我们向用户添加了执行ping.txt的权限
$ chmod u+x ping.txt
删除文件和文件夹访问权限 (Remove File and Folder Access Permission)
-
between role and permission is used to remove permission for the given role. In this example we remove users execute permission from file ping.txt
-
在角色和权限之间用于删除给定角色的权限。 在此示例中,我们从文件ping.txt中删除用户执行权限
$ chmod u-x ping.txt
从其他文件复制权限 (Copy Permissions From Other File)
If we have already set some file permissions we can use this file as a reference point for permission. We can copy given file permissions to the specified file. We will use --reference
option and the reference file name. In this example, we will use run file permissions as a reference to apply to run2
.
如果我们已经设置了一些文件权限,则可以将该文件用作权限的参考点。 我们可以将给定的文件权限复制到指定的文件。 我们将使用--reference
选项和参考文件名。 在此示例中,我们将使用运行文件权限作为应用于run2
的参考。
$ chmod --reference=run run2
使用数字访问权限 (Using Numeric Access Permissions)
Up to now we have user u
, g
and o
for roles and x
, w
,r
for permissions. There is another presentation for these. We will use three digits for u
,g
, o
role specification like 540 . Here 5
is for user , 5
is for group for others.
到现在u
,我们有角色的用户u
, g
和o
,以及权限的x
, w
和r
。 这些还有另一个介绍。 u
, g
, o
角色说明将使用三位数,例如540。 这里5
是用户, 5
是组为他人。
r
value is4
r
值为4
w
value is2
w
值为2
x
value is1
x
值为1
So if we want to give r
and w
we will sum their numeric values which is 6
.
因此,如果我们要给r
和w
我们将求和它们的数值为6
。
Here some examples
这里有一些例子
x w
equal to 3xw
等于3r x
equal to 5rx
等于5
为Chmod使用数值 (Using Numeric Values For Chmod)
We can use previously explained numeric values for chmod
. We will only provide related value to the command. In this example we only want the user to read , write, and execute permissions and others not.
我们可以将先前解释的数值用于chmod
。 我们只会为命令提供相关的值。 在此示例中,我们只希望用户读取,写入和执行权限,而其他用户则不希望。
$ chmod 700 executable
将所有权限授予所有角色 (Give All Permissions To All Roles)
We can give all permission to all roles which means user, group and others can read, write and execute. This is very insecure and dangerous action. We will use 777
我们可以授予所有角色所有权限,这意味着用户,组和其他人可以读取,写入和执行。 这是非常不安全和危险的行动。 我们将使用777
$ chmod 777 file
翻译自: https://www.poftut.com/linux-chmod-command-tutorial-examples-change-permission-files-folders/