Linux Chmod命令教程,其中包含更改文件和文件夹权限的示例

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 with ls -l command.

    owner指定文件或目录的所有者。 可以使用ls -l命令列出它。

  • group specifies the file or directory group owners. Files and directories group owners can be listed with ls -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 directory

    write模式指定写入和更改文件或目录

  • execute mode specifies executing file

    execute模式指定执行文件

列出文件和文件夹访问权限(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 -lcommand to list permissions. In this example we will list all current working directory file and folders permissions.

在开始更改权限之前,我们需要列出有关文件和目录的当前角色和权限。 我们将使用ls -l命令列出权限。 在此示例中,我们将列出所有当前的工作目录文件和文件夹权限。

$ ls -l
List Permissions
List File and Folder Access Permissions
列出文件和文件夹访问权限

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. Here read and write permission given

    rw-是所有者权限。 这里readwrite权限给予

  • r-- is group permission. Only read permission given

    r--是组权限。 仅授予read权限

  • Last r-- is other permission. Only read permission given.

    最后一个r--是其他权限。 仅授予read权限。

LEARN MORE  Windows User Management With Net User Like Creating, Deleting, Setting Password
了解更多使用Net用户的Windows用户管理,例如创建,删除,设置密码

更改文件和文件夹用户访问权限(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 group

    g用于指定组

  • - is used to remove given permission

    -用于删除给定的权限

  • r is used to specify read permission

    r用于指定读取权限

更改文件和文件夹的多个组和用户访问权限(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 ,我们有角色的用户ugo ,以及权限的xwr 。 这些还有另一个介绍。 ugo角色说明将使用三位数,例如540。 这里5是用户, 5是组为他人。

  • r value is 4

    r值为4

  • w value is 2

    w值为2

  • x value is 1

    x值为1

So if we want to give r and w we will sum their numeric values which is 6 .

因此,如果我们要给rw我们将求和它们的数值为6

LEARN MORE  What Is User In Computing, Windows, Linux, Google?
了解更多什么是计算,Windows,Linux,Google中的用户?

Here some examples

这里有一些例子

  • x w equal to 3

    xw等于3

  • r x equal to 5

    rx等于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/

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值