Summary of Linux file permissions

One of the most basic tasks in Linux is setting file permissions. Understanding how they are implemented is your first step into the Linux world. As you might expect, this basic operation is more or less the same in UNIX-like operating systems. In fact, the Linux file permissions system is taken directly from UNIX file permissions (www.lampbrother.net).
But don't assume that understanding file permissions takes a long time to learn. It's actually pretty simple, let's take a look at what you need to know and how to use them.

Basic Concepts The first thing
you need to understand is what file permissions can be used for. What happens when you set permissions for a group? Let's expand it, the concept is really much simpler. So what exactly are permissions? What is grouping?

3 permissions you can set:
read - allow the group to read the file (denoted by r)
write - allow the group to write the file (denoted by w)
execute - allow the group to execute (run) the file (denoted by x)
for the better Explain how this applies to a group, for example, you allow a group to read and write a file, but not execute. Alternatively, you can allow a group to read and execute a file, but not write. You can even allow a group to have read, write, and execute all permissions, or you can remove all permissions to remove the group's permissions.

Now, what is a group? There are the following 4:
user - the actual owner of the file
group - the user group the user belongs to
others - other users outside the user group
all - all users In
most cases, you will only perform operations on the first 3 groups Actions, all This group is just a shortcut (I'll explain it later).
Simple so far, right? Next we'll go one level deeper.
If you open a terminal and run the command ls -l, you will see a line-by-line listing of all the files and folders in the current working directory.
You will notice that the leftmost column is something like -rw-rw-r– Such.
Actually the list should look like this:
rw- rw- r –
As you can see, the list divides it into 3 parts as follows:
rw-
rw-
r
– The order of permissions and groups is important, the order is always:
owner belongs to Group Others - Group
Read Write Execute - Permissions
In the permission list in our example above, the owner has read/write permissions, the group to which they belong has read/write permissions, and the others users only have read permissions. If execution permission is granted in these groups, it is represented by an x.

Equivalent Numerical Values
​​Next let's make it a little more complicated, and each permission can be represented by a number. The numbers are:
read - 4
write - 2
execute - 1
value instead of one by one, you can't do something like this:
-42-42-4 -
you should add up the values ​​for each group, give the user read and write permissions , you should use 4 + 2 to get 6. Give the same permissions to the user group, and use the same values. If you only want to give read permissions to other users, set it to 4. Now expressed as a numerical value:
664
If you want to give a file 664 permissions, you can use the chmod command, such as:
chmod 664 FILENAME
FILENAME is the file name.

Change permissions
Now that you understand file permissions, it's time to learn how to change those permissions. Is to use the chmod command to achieve. The first step is to know if you can change file permissions, you must be the owner of the file or have permission to edit the file (or get permission via su or sudo). Because of this, you can't switch directories and change file permissions at will.
Continue with our example (-rw-rw-r–). Suppose this file (named script.sh) is actually a shell script that needs to be executed, but you only want to give yourself permission to execute the script. At this point, you might be thinking: "I need permissions on files like -rwx-rw-r–". To set the x permission bits, you can use the chmod command like this:
chmod u+x script.sh
At this point, the listing should read -rwx-rw-r–.
If you want to give both the user and the group to which it belongs to have execute permissions, the command should be like this:
chmod ug+x script.sh See
how this works? Let's make it a little more interesting. For whatever reason, you accidentally gave all groups execute permissions on the file (-rwx-rwx-rx in the list).
If you want to remove the execute permission of other users, just run the command:
chmod ox script.sh
If you want to completely remove the executable permission of the file, you can use two methods:
chmod ugo-x script.sh
or
chmod ax script. sh
The above is all that makes the operation more efficient. I would like to avoid operations that might cause some problems (eg you accidentally chmod command a-rwx on script.sh).

Directory permissions
You can also execute the chmod command on a directory. When you create a new directory as a user, usually the new directory has the following permissions:
drwxrwxr-x
Note: The d at the beginning indicates that this is a directory.
As you can see, both the user and its group have operation permissions on the folder, but this does not mean that the files created in this folder also have the same permissions (the created files use the default system permissions -rw -rw-r–). But if you want to create files in a new folder and remove write permissions from the user group, you don't have to change to that directory and use chmod command on all files. You can use the chmod command with the parameter R (meaning recursive) to change the permissions of the folder and all files in it at the same time.
Now, suppose there is a folder TEST with some scripts in it, all of which (including the TEST folder) have permissions -rwxrwxr-x. If you want to remove the write permission of the user group, you can run the command:
chmod -R gw TEST
run the command ls -l, and you will see that the permission information for the TEST folder listed is drwxr-xr-x. The user group is stripped of write permissions (as are all files in its directory).

Summary By
now , you should have a solid understanding of basic Linux file permissions. It's easy to learn more advanced stuff like setgid, setuid and ACL. Without a good foundation, you can quickly get confused.
Linux file permissions haven't changed much since the early days, and probably won't change in the future either.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326803533&siteId=291194637