chmod怎么用,Linux文件权限管理

本文翻译自Linux官网的Linux入门文章《File Permissions – chmod》,其中一些部分自作主张做了些修改

Linux从UNIX继承了文件所有权和权限的观念。这是因为它被认为是一个不同的人在上面使用不同程序和文件的网络操作系统。显而易见,它需要去保持系统里事物的条理和安全。我们不想让一个普通用户使用一个可能有着潜在威胁的程序破坏整个系统。这包含了一些隐私和安全的问题。

面对现实吧,我们不想让Bill读到Bob写给Janet的情书(因为Janet是Bill的未婚妻)。最后,最重要的是了解到什么是属于我的,什么是属于你的,什么是属于每个人的

正如我们在课程的开篇所提及的。Linux的最大优势就是它的多用户概念——许多人可以使用同一台计算机或一个人可以使用同一台计算机去做不同工作。文件权限管理系统可以在这种情况下提供帮助。

这篇文章将解释一些基础的观点,比如谁拥有文件,以及谁可以对文件做些什么。但这篇文章不会讲太多深入的细节,我们将把它留到Linux系统管理的课程中。

在这里,我们将向您展示如何理解文件权限符号,以及如何修改文件以使其更安全。

[En]

Here we will show you how to understand file permission symbols and how to modify a file to make it more secure.

如果你运行下面的命令

ls -l

在当前目录中,您将看到可能包含这些内容的文件列表

[En]

In the current directory, you will see a list of files that may contain these contents

-rw-r--r--  1  bob  users  1892  Jul 10  18:30 linux_course_notes.txt

从右到左简单解释一下:

linux_course_notes.txt文件是在7月10日18:30创建的,大小是 1892bytes,属于 users群组,属于 bob,是 1 文件,剩下的就是一些文件权限符号

让我们来看看这些符号的含义:

[En]

Let’s look at the meaning of these symbols:

破折号”-“: 将权限分成了三个部分

第一部分是所有者(本文中是bob)的权限

破折号在rw之前,表示这是一个包含任何类型数据的普通文件。如果列出来的是一个目录,那么在rw之前的破折号就会被 d所取代

rw表示bob可以读写(read/write)他自己的文件。这是一件合乎逻辑的事情。如果你拥有它,你就应该有权限用它做你想做的事情。

第二部分在第二个破折号之后,代表的是用户组的权限。Linux可以为文件权限建立不同的用户组。在一个家庭计算机上,让任何人都可以读取此文件,但不能修改它。这是一个完全正常的情况。

作为用户,您可能想要剥夺其他人对此文件的访问权限,我们稍后将向您演示如何做到这一点。

[En]

As a user, you may want to deprive others of access to this file, and we will show you how to do this later.

第三部分在后两个破折号之后(指的是 -rw-r之后的两个破折号。这里之所以有两个是因为用户组只有读取权限没有写入权限,因此用了一个破折号占住了原先写入权限的位置)是全体用户的权限。任何人有权限从内部或外部(有网络的情况下)访问此计算机读取此文件。

当然,如果我们愿意,我们可以消除人们阅读这份文件的可能性。

[En]

Of course, if we want, we can eliminate the possibility that people will read this document.

让我们来看些其他的例子,一个有趣的地方是/bin目录,在这里你可以查看到不同类型的文件的权限。这里有任何人都可以在Linux上使用的命令。

让我们来看看gzip命令,这是一个Linux上的文件压缩程序。

-rwxr-xr-x 1 root root     84912 Apr  8 01:33 gzip

正如我们所看到的,有一些不同之处。

[En]

As we can see, there are some differences.

该程序的名称、日期和大小的格式是标准的。虽然信息明显不同,但格式与以前相同。

[En]

The format of the name, date and size of this program is standard. Although the information is obviously different, the format is the same as before.

改变的地方在拥有者和用户组的部分。root用户拥有这个文件,并且这个文件在root用户组中。root用户实际上就是root用户组唯一的成员。

这个文件是一个可执行程序,这也是为什么文件权限中有x这个字符。

这个文件可以被任何人执行,它的拥有者(root),它所在群组的所有成员(root),以及所有能够访问这个计算机的人。

正如我们提到的,这个文件是一个程序,所以除了root之外的任何人都不需要”写入”该文件,因此,除了root用户以外,任何人都没有w(write)权限

chmod是Linux上的一个命令,它可以让你设置文件权限(读取、写入、执行)

chmod 权限 文件名

当我们使用chmod命令时,你需要意识到有三种类型的用户需要设置权限。因此,当设置权限时,你需要为你自己、你所在的用户组和其他人分配权限。

这三种类型的用户在技术上称为:

[En]

These three types of users are technically called:

Owner(拥有者)

Group(群组)

World(所有人)

因此,在为文件设置权限时,您需要分配三个级别的权限,而不仅仅是一个用户。

[En]

So, when setting permissions for a file, you need to assign three levels of permissions, not just one user.

因此,chmod的语法准确的说是下面这个样子

chmod owner group world FileName

您已经知道您正在为三个用户设置权限,现在您只需要知道您可以设置哪些权限。

[En]

You already know that you are setting permissions for three users, now you only need to know the permissions you can set.

Linux下,每个文件有三种类型的权限

read(读取)
write(写入)
execute(执行)

在通俗易懂的术语中,如果您希望使文件对所有人都是只读的,并且只有您可以修改该文件,则可以使用以下代码

[En]

In layman’s terms, if you want to make a file read-only to everyone, and only you can modify the file, you can use the following code

chmod 644 myDoc.txt

当我在这里看到很多第一次接触的人时,他们会大喊:“婷婷,怎么会有数字呢?”

[En]

When I see many people who come into contact for the first time here, they will shout: “Tingting, why are there numbers?”

如果你问为什么,那是因为计算机更喜欢数字而不是字母。

[En]

If you ask why, it is because computers prefer numbers to letters.

坏消息是,您只能以这种方式操作文件权限。

[En]

The bad news is that you can only manipulate file permissions in this way.

好消息是,这并不难理解。

[En]

The good news is, it’s not difficult to understand.

私以为其实是因为这里用字母会比较难以表达,并且使用起来特别不方便
如果要使用字母表示权限,则必须按如下方式输入它们

[En]

If you want to use letters to represent permissions, you have to enter them like this

chmod rwxrwx–x FileName //当然实际上没有这种写法,如果真这么写会报错的
因此,在设计中,我们选择增加理解难度,降低使用难度。

[En]

Therefore, in the design, we choose to increase the difficulty of understanding to reduce the difficulty of use.

我们需要把下面的短语转换成数字并写下来

[En]

We need to convert the following phrases into numbers and write them down

read(读取) ==> 4
write(写入) ==> 2
execute(执行) ==> 1

如果我只想授予读取权限,则为4。

[En]

If I just want to give read permission, it’s 4.

如果我只想授予写权限,那么它就是2。

[En]

If I just want to give write permission, then it’s 2.

如果我只想授予执行权限,那么它就是1。

[En]

If I just want to give execution permission, then it’s 1.

如果我不想给任何许可,它就是0。

[En]

If I don’t want to give any permission, it’s 0.

// chmod 拥有者 用户组 其他人 文件名

chmod 440 FileName

很明显,在上面的命令中,文件所有者和用户组被授予对文件的读访问权限,而其他人则什么都不能做。

[En]

It is clear that in the above command, the file owner and user group are given read access to the file, while others can do nothing.

但是,如果只有读取权限,则无法修改文件,但如果将4更改为2,则无法读取文件。

[En]

However, if there is only read permission, there is no way to modify the file, but if you change 4 to 2, there is no way to read the file.

总不能修改完后又执行一遍chmod命令将2改回4吧,那未免也太愚蠢了,即使是Windows也不会这么设计的!

Linux给出了办法,那就是将数字相加

6 = 4 + 2 = 读取 + 写入
7 = 4 + 2 + 1 = 读取 + 写入 + 执行

因此,我们现在可以清楚地理解以下命令的含义

[En]

Therefore, we can now clearly understand the meaning of the following command

chmod 644 myNovel.txt

对于该文件,文件所有者有读写权限,文件所有者所属的用户组和其他人只有读权限

[En]

For this file, the owner of the file has read and write permissions, and the user group to which the file owner belongs and others only have read permission

一些小细节

看完文章之后,关于 ls -l列出来的信息其实已经大差不差的了解了

以下是一些细节。

[En]

Here are some details.

drwxrwx---  2 friland friland   20480 Apr 16 20:58 maoArticle

这里的 drwxrwx---其实可以拆开分成四个部分

“d rwx rwx —“

因为是文件夹,第一个部分的破折号变成d,即directory。如果不是文件夹,第一部分的d就会变回破折号

后三部分由三个短划线组成,三个短划线的位置对应三种权限。如果缺少某个权限,则会使用该破折号来占用空间。

[En]

The last three parts are composed of three dashes, and the positions of the three dashes correspond to three kinds of permissions. If a certain permission is missing, the dash will be used to occupy the space.

例如,如果一个文件,文件拥有者、群组成员和其他人都可以读写并执行这个文件,那么这个文件在 ls -l中的显示应该是这样的

-rwxrwxrwx

除了文件夹和文件的第一部分仍然是虚线外,虚线的所有其他部分都将被替换。

[En]

Except that the first part of the folder and the file is still a dash, all the other parts of the dash are replaced.

Original: https://www.cnblogs.com/hellofriland/p/16304057.html
Author: hellofriland
Title: chmod怎么用,Linux文件权限管理

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/523521/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球