What the hell is chmod 777??? After reading this you will fully understand!

0X00 Foreword

Perhaps for Linux novices, one thing that is very puzzling is:
sometimes when accessing file xxx, there are always permissions problems, but when I check it online, the big guys say directly:
chmod 777 xxx is
done!
I believe your mood at the time is like this:
Insert picture description here
I am a magical
and then I will follow the chmod 777 of the gourd painting when I encounter the authority, which is also a good thing.
However, as a programmer who breaks the casserole, how can he know chmod 777!
Of course, when it comes to chmod, we must first make clear the user management of Linux

0X01 Linux users and groups

1. User

We know that Linux has a super user- root , which is the complete control of the entire Linux system. As long as root wants to do it, there is no root that cannot do it.
Therefore, for security and other considerations, Linux designers have designed ordinary users . For example, when Linux is just installed, a user will be created as an ordinary user (assuming it is called j). Of course, you can use the adduser command to add a new user (of course Super permission required):

sudo adduser rjs

At this time, Linux has three users root, j and rjz, among which root is the super user, j and rjs are ordinary users

2. Group

Now that there are many users, there is the concept of groups . Because you know more people, you are naturally grouped. Some are family members, some are friends, some are classmates, and so on. Family, friends, and classmates are groups:
Insert picture description here
With the concept of users and groups, we can talk about permissions

0X01 Linux file permissions

1. Permission description

Since there are users, the files created by a user (such as: j) will generally not be tampered with by other users (such as: rjs), that is, they cannot be written (w) to others, of course ( r) It’s okay, don’t be so stingy, right? 23333
So for the same file in Linux, different users have different permissions. The permissions are: read (r), write (w), run (x)
Insert picture description here
we You can use the ls -l command to view file permissions:

ls	-l

You will get the following information:
Insert picture description here
you can see that there is a folder in this directory: Tencent Files, a document: wget-log,
each file (folder) has this description before:
drwxr-xr-x
-rwx---- ---

This is the authority of the file to the user!
What does this mean? And look down

2. File permissions

There are a total of 10 digits in the above description, of which the first digit indicates the attributes of the file:
d: indicates that it is a folder
l: indicates that it is a link
-: indicates that it is an ordinary file
Note: this is the first and the following not same

The following is our protagonist today-the authority description.
It can be seen that it is composed of a series of read (r), write (w), and run (x), so why are there so many r, w, and x repeated in a row?
That's because access permissions are divided according to users :
Insert picture description here
that is, starting from the second digit, every three digits represent the permissions of the file owner, group users, and other users. For
example:
drwxr-xr-x
represents the folder Tencent Files For owner j, it is readable, writable and executable (rwx), for users in the same group, it is writable, unreadable and executable (rx), and it is also writable, unreadable and executable (rx) for other users ( rx)
With this, our protagonist today- chmod is finally about to debut!

0X10 chmod command: modify file access permissions

chmod is used to modify file permissions and access permissions! That's right, the permissions mentioned above!
What is 777? ? ?
Don't worry, please look down

1. Use numbers to assign permissions: absolute usage of chmod

In fact, the Linux system assigns corresponding numbers for each permission (r, w, and x):

Authority digital
r 4
w 2
x 1

Therefore, if we want to merge these permissions, we need to do a simple addition: add the corresponding numbers .

If we want to assign read and write permissions, then we have to use 4+2, which equals 6. The number 6 means that it has read and write permissions .

The following are possible combinations:

Authority digital Calculation
0 0 + 0 + 0
r– 4 4 + 0 + 0
-w- 2 0 + 2 + 0
–x 1 0 + 0 + 1
rw- 6 4 + 2 + 0
-wx 3 0 + 2 + 1
r-x 5 4 + 0 + 1
rwx 7 4 + 2 + 1

Therefore, for the three groups of access permissions (owner’s permissions, group user permissions, and other user permissions), we only need to add them separately, and then connect the three sums.

For example, 640 means:

  • The owner of the file has read and write permissions;
  • Other users in the group where the file is located have read permission;
  • Other users do not have any permissions.

Therefore, the broadest permission we can give is 777: the owner, group users, and other users have read, write, and run permissions. In this way, everyone can "do whatever they want" with this file.

On the contrary, if the permission is 000, then no one can do anything with this file. Of course, root can do anything except root.
Seeing now is there a feeling of sudden enlightenment:
Insert picture description here
But now that I see this, it’s better to continue to look down

2. Use letters to assign permissions: the relative usage of chmod

We can also use letters to assign permissions. The principle is similar, but there is no need to write out all three sets of permissions. Sometimes it’s more flexible.
Let’s first look at the meaning of different letters:

  • u: short for user, which means owner;

  • g: the abbreviation of group, which means group users;

  • o: the abbreviation of other, which means other users;

  • a: The abbreviation of all, which means all users.

    There are several symbols that match these letters:

  • +: plus sign, means adding permission;

  • -: Minus sign, indicating removal of permissions;

  • =: Equal sign, which means distribution authority.

For example

#文件 file.txt 的所有者增加读和运行的权限。
chmod u+rx file.txt

#文件 file.txt 的群组其他用户增加读的权限。
chmod g+r file.txt 

#文件 file.txt 的其他用户移除读的权限。
chmod o-r file.txt 

#文件 file.txt 的群组其他用户增加读的权限,其他用户移除读的权限。
chmod g+r o-r file.txt 

#文件 file.txt 的群组其他用户和其他用户均移除读的权限。
chmod go-r file.txt 

#文件 file.txt 的所有用户增加运行的权限。
chmod +x file.txt 

#文件 file.txt 的所有者分配读,写和执行的权限;
#群组其他用户分配读的权限,不能写或执行;
#其他用户没有任何权限。
chmod u=rwx,g=r,o=- file.txt

Afterword

At this point, I think everyone fully understands the meaning of chmod~ I hope you will find something after reading it. <^^>
ps: Today I know that I have talked about it in class, shame! So, students, listen to the class!

——————————————————————————————————————————————
Reference: Linux Command Line and Shell Script Programming Encyclopedia/15 Group Management and File Authority Management

Guess you like

Origin blog.csdn.net/rjszz1314/article/details/104399333