在过去的几个月里,我一直在学习Linux课程,而(Linux)Linux的一个方面总是让我感到困惑,那就是权限是如何工作的。例如,当一次将文件上传到我的网络服务器并出现错误时,我的网络主机告诉我将文件权限更改为 755。
我不知道这意味着什么,即使更改权限解决了问题。我现在意识到Linux权限并不是那么复杂,您只需要了解系统即可。在本文中,我将在较高级别上讨论Linux权限,并向您展示如何使用 chmod 命令更改文件和文件夹的权限。
Linux 权限和级别
在Linux中,基本上您通常需要担心三种权限:读取、写入和执行。所有这三个都是不言自明的。现在,当这些权限应用于文件时,它们会按级别应用。
Linux中的权限分为三个级别:所有者、组和其他。所有者是拥有文件/文件夹的用户,该组包括文件组中的其他用户,而其他仅代表不是所有者或组中的所有其他用户。
读取(Read)、写入和执行以符号字符或八进制数表示。例如,如果您在包含一些文件的目录中执行 ls -l,您将看到权限的符号字符表示。
权限的写法如下:第一位是破折号或字母 d。破折号(Dash)表示它是一个文件,d代表目录。请注意,如果文件名是链接,则第一位也可以是l 。接下来,有三组三位。每组中的第一位用于读取,第二位用于写入,第三位用于执行。前三位用于所有者,后三位用于组,第三位用于其他。这是一个更直观的解释。
如果您在字母位置看到破折号,则表示所有者、组或所有其他用户没有该权限。在上面的示例中,所有者、组和其他所有人都具有读写和执行权限。
如果您查看 ls -l 命令的输出,您会注意到我的练习文本文件具有以下权限:
-rw-rw-rw-
这意味着每个人都只有文件的读/写权限。这是另一个例子:
drwxr--r--
查看第一位,我们可以看到权限是针对目录的。所有者拥有读/写/执行权限,但组和其他用户只有读权限。
八进制数表示
这就是在Linux(Linux)中使用符号显示权限的方式。表示相同权限的第二种方法是使用八进制数。当我们稍后使用 chmod 命令时,您会看到可以使用符号或八进制数来更改权限。
那么Linux如何使用八进制数来表示读、写和执行呢?基本上,它只是为每个权限分配一个数字,如下所示。
读权限用 4 表示,写权限用 2 表示,执行权限用 1 表示。您只需将它们相加即可获得八进制权限。例如,让我们以上面每个人都拥有所有权限的示例为例:
-rwxrwxrwx
所有者有 rwx,所以我们将添加 4 + 2 + 1 得到值 7。我们对 group 做同样的事情,对 other 做同样的事情。最终的八进制值为 777。让我们看一下我们只赋予读/写权限的示例:
-rw-rw-rw-
第一个八进制数将是 4 + 2,因为我们正在添加读写。第二个将与第三个八进制数相同。这里我们有一个最终的八进制值 666。
所以现在让我们换一种方式试试。假设我们想知道755代表什么权限?嗯,很容易弄清楚你是否按个别数字分解它。第一个数字是 7,我们只能通过 4 + 2 + 1 相加得到,即拥有者拥有读/写/执行权限。5 只能加 4 + 1 得到,表示该组和其他用户具有读取和执行权限。
希望这是对如何在(Hopefully)Linux中使用八进制数表示权限的一个很好的解释。总体来说非常简单。
使用 chmod 修改权限
现在我们了解了如何读取权限,让我们谈谈如何更改它们。用于此目的的最简单实用程序是 chmod 命令。这是它的工作原理。解释该命令的最佳方式是通过一个示例。
让我们从我们上面谈到的权限开始,即:
-rw-rw-rw-
如果我们想为所有者、组和其他添加执行权限,我们可以通过两种方式进行。我们可以使用符号方法或八进制方法。对于符号方法,我们将执行以下操作,如下所示:
确切的命令是
chmod a+x filename
语法如下:代表所有者(u)、组(g)、其他(o)或全部(a)的一个或多个字母,后跟一个 +表示添加权限或a-(–)表示取消权限,然后是字母for权限(r表示读取,w表示写入,x表示执行)。
在上面的示例中,我为所有用户添加了执行权限。如您在上面的屏幕截图中看到的结果是所有者、组和其他的x。现在假设我只想删除组和其他用户的写入和执行权限。
正如您在此处看到的,我使用以下命令来完成此操作:
chmod go-wx filename
由于我想更改 group 和 other 的权限,我使用字母g和字母o。我想删除权限,所以我使用-号。最后,我想删除写入和执行权限,所以我使用w和x。这是一个方便的符号使用小表:
这就是使用符号方法的全部内容。现在让我们谈谈八进制方法,我发现它更容易一些。八进制(Octal)很好,因为您可以一次性添加或删除权限。
如果我们从文件的以下权限开始,让我们看看如何使用八进制方法更改它们:
-rw-rw-rw-
上面(Above),你可以看到我使用了以下命令:
chmod 744 filename
这基本上说所有者获得读/写/执行权限,而组和其他人仅获得读权限。如您所见,只需一个简单的命令即可轻松添加或删除权限。让我们继续说我想再次更改权限。
现在我使用了以下命令,又是一个非常简单的命令:
chmod 640 filename
这里我们给了所有者读/写权限,组只有读权限,其他组没有权限。您使用零表示没有权限。很简单吧?
总之,这是对Linux权限的一个非常简单的概述,它可能会比这复杂得多,但对于初学者来说,这是一个很好的起点。将来我会发布更多关于更高级权限的文章。如果您有任何问题,请随时发表评论。享受!
Understanding Linux Permissions and chmod Usage
I’ve been taking a course on Linux for the last fеw months and one aspect of Linux that always confυsed me was how permissions worked. For example, when uplоading a file to my web server once and getting an error, I was told by mу web host to change the file pеrmissions to 755.
I had no clue what that meant, even though changing the permissions fixed the problem. I’ve now realized Linux permissions aren’t all that complicated, you just have to understand the system. In this article, I’ll talk about Linux permissions on a high-level and show you how to use the chmod command to change permissions for files and folders.
Linux Permissions & Levels
In Linux, there are basically three permissions that you will normally have to worry about: read, write and execute. All three of these are pretty self-explanatory. Now when these permissions are applied to a file, they are applied in levels.
There are three levels of permissions in Linux: owner, group and other. The owner is the user who owns the file/folder, the group includes other users in the file’s group and other just represents all other users who are not the owner or in the group.
Read, write and execute are represented as either symbolic characters or as octal numbers. For example, if you do a ls -l in a directory with some files, you’ll see the symbolic character representation of the permissions.
The permissions are written as follows: the first bit is either a dash or the letter d. Dash means it’s a file and d stands for directory. Note that the first bit can also be an l if the file name is a link. Next, there are three groups of three bits. The first bit in each group is for read, the second bit is for write and the third bit is for execute. The first three bits are for the owner, the second three bits are for the group and the third three bits are for other. Here’s a more visual explanation.
If you see a dash in place of a letter, it means that the owner, group or all other users do not have that permission. In the example above, the owner, group and everyone else has read write and execute permissions.
If you look at the output from the ls -l command, you’ll notice that my practice text file has the following permissions:
-rw-rw-rw-
This means that everyone only has read/write permissions for the file. Here’s another example:
drwxr--r--
Looking at the first bit, we can see that the permissions are for a directory. The owner has read/write/execute permissions, but the group and other users only have read permission.
Octal Number Representation
So that’s how permissions are displayed in Linux using symbols. The second way to represent the same permissions is by using octal numbers. When we use the chmod command later on, you’ll see that you can change the permissions using either symbols or octal numbers.
So how does Linux represent read, write and execute using octal numbers? Basically, it just assigns a number to each permission as shown below.
The read permission is represented by 4, write by 2 and execute by 1. All you have to do is add them up to get the octal permission. For example, let’s take the example above where everyone has all permissions:
-rwxrwxrwx
The owner has rwx, so we will add 4 + 2 + 1 to get a value of 7. We do the same thing for group and the same thing for other. The final octal value is 777. Let’s take a look at the example where we only gave read/write permissions:
-rw-rw-rw-
The first octal number will be 4 + 2 since we are adding read and write. The second one will be the same as will the third octal number. Here we have a final octal value of 666.
So now let’s try it the other way. Say we want to know what permissions 755 represents? Well, it’s pretty easy to figure out if you break it down by individual numbers. The first number is 7, which we can only get by adding 4 + 2 + 1, meaning the owner has read/write/execute permission. Five can only be gotten by adding 4 + 1, meaning the group and other users have read and execute permissions.
Hopefully, that’s a good explanation for how to represent permissions in Linux using octal numbers. It’s pretty straight-forward overall.
Using chmod to Modify Permissions
Now that we understand how to read permissions, let’s talk about how we can change them. The easiest utility to use for this purpose is the chmod command. Here’s how it works. The best way to explain the command is to go through an example.
Let’s start with the permissions we talked about above, namely:
-rw-rw-rw-
If we wanted to add the execute permission for owner, group and other, we could go about it in two ways. We could use the symbol method or the octal method. For the symbol method, we would do the following, as shown below:
The exact command is
chmod a+x filename
The syntax is as follows: the letter or letters representing the owner (u), group (g), other (o) or all (a) followed by a + for adding permissions or a – for taking away permissions and then the letter for the permission (r for read, w for write and x for execute).
In the above example, I added the execute permission for all users. The result as you can see in the screenshot above is an x for owner, group and other. Now let’s say I wanted to remove the write and execute permissions for only the group and other users.
As you can see here, I used to the following command to accomplish this:
chmod go-wx filename
Since I want to change the permissions for group and other, I use the letter g and the letter o. I want to remove permissions, so I use the – sign. Finally, I want to remove the write and execute permissions, so I use w and x. Here’s a handy little table for symbol usage:
So that’s all there is to using the symbol method. Now let’s talk about the octal method, which I find to be a bit easier. Octal is nice because you can add or remove permissions all in one go.
If we start with the following permissions on a file, let’s see how we can change them using the octal method:
-rw-rw-rw-
Above, you can see I used the following command:
chmod 744 filename
This basically says the owner gets read/write/execute permission and the group and other gets read permission only. As you can see, it’s easy to add or remove permissions in one simple command. Let’s keep going and say I want to change permissions again.
Now I used the following command, again a very simple one:
chmod 640 filename
Here we have given the owner read/write permissions, the group read permission only and the other group no permissions. You use a zero to denote no permissions. Pretty simple, eh?
In conclusion, this is a very simple overview of Linux permissions and it can get a lot more complicated than this, but for beginners, it’s a good place to start. I’ll be posting more articles on more advanced permissions in the future. If you have any questions, feel free to comment. Enjoy!