Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

The basic properties of the Linux file


May 22, 2021 Linux


Table of contents


The basic properties of the Linux file

Linux system is a typical multi-user system, different users in different positions, with different permissions. T o protect the security of the system, the Linux system provides different permissions for different users to access the same file, including directory files.

In Linux, we can use the ll or ls-l command to display the properties of a file and the users and groups to which the file belongs, such as:

[root@www /]# ls -l
total 64
dr-xr-xr-x   2 root root 4096 Dec 14  2012 bin
dr-xr-xr-x   4 root root 4096 Apr 19  2012 boot
……

In the instance, the first property of the bin file is represented by a "d". "d" in Linux represents the file as a directory file.

The first character in Linux represents that the file is a directory, file, link file, and so on.

  • When it is a directory, it is a directory
  • When it is -
  • In the event of a link document (link file);
  • In the event of a device, the device is a storage-ready interface device (random access device);
  • A serial port device, such as a keyboard, mouse (one-time reading device), is represented by a serial port device in the device file.

The next characters are grouped with three and are a combination of three parameters of rwx. A mong them, the "r" stands for read, the "w" stands for writeable (write), and the "x" stands for executable (execute). Note that the location of these three permissions does not change, and if there are no permissions, a minus sign will appear.

The properties of each file are determined by 10 characters from the first part on the left (see figure below).

The basic properties of the Linux file

0-9 numbers are used from left to right.

The 0th determines the file type, and the 1rd-3rd determines that the owner (the owner of the file) has permissions for the file.

The 4th-6th determines that the group (the same group of users of the owner) has permissions to the file, and the 7th-9th determines that other users have permission to own the file.

Among them, the 1st, 4th and 7th bits represent read permissions, if the "r" character is used, then there is read permission, if the "-" character is used, there is no read permission;

The 2nd, 5th, and 8th bits represent write permissions, if they are represented by the "w" character, if they are represented by the "-" character, if they are not written, and the 3rd, 6th, and 9th bits represent executable permissions, if they are represented by the "x" character, there are execution permissions, and if they are represented by the "-" character, there is no execution permission.


Linux files belong to the master and belong to the group

[root@www /]# ls -l
total 64
dr-xr-xr-x   2 root root 4096 Dec 14  2012 bin
dr-xr-xr-x   4 root root 4096 Apr 19  2012 boot
……

For a file, it has a specific owner, that is, the user who owns the file.

At the same time, in Linux systems, users are grouped, and one user belongs to one or more groups.

Users other than the file owner can be divided into the same group of users and other users of the file owner.

As a result, the Linux system provides different file access rights by file owner, file owner, group of users, and other users.

In the above example, the bin file is a directory file that belongs to both the master and the group as root, which has readable, writeable, executable permissions, readable and executable permissions to other users who belong to the main group, and other users have readable and executable permissions.

In general, the permissions of a file have no effect on root users.

Change the file properties

1, chgrp: Change the file belongs to the group

Grammar:

chgrp [-R] 属组名文件名

Argument options

  • -R: Recursive change file belongs to a group, that is, when you change the genus of a directory file, if you add the -R parameter, then all files in that directory belong to the group will change.

2, chown: change the file belongs to the master, you can also change the file belong to the group at the same time

Grammar:

chown [–R] 属主名 文件名
chown [-R] 属主名:属组名 文件名

Go to the /root directory (-) to change the owner .log install to bin this account:

[root@www ~] cd ~
[root@www ~]# chown bin install.log
[root@www ~]# ls -l
-rw-r--r--  1 bin  users 68495 Jun 25 08:53 install.log

Change .log owner and group of installs to root:

[root@www ~]# chown root:root install.log
[root@www ~]# ls -l
-rw-r--r--  1 root root 68495 Jun 25 08:53 install.log

3, chmod: Change the file 9 properties

Linux file properties have two ways to set them, one is a number and the other is a symbol.

Linux files have nine basic permissions, each with its own read/write/execute permissions.

Review the data just mentioned above: the permission character of the file is: '-rwxrwxrwx', these nine permissions are three three groups! Among them, we can use numbers to represent each permission, and the score comparison table for each permission is as follows:

  • r:4
  • w:2
  • x:1

Each identity (owner/group/others) has its own three permission (r/w/x) scores that need to be added up, for example, when the permissions are: -rwxrwx--- the score is:

  • owner = rwx = 4+2+1 = 7
  • group = rwx = 4+2+1 = 7
  • others= --- = 0+0+0 = 0

So wait for us to set the permission change, the permission number of this file is 770! T he syntax of the instruction chmod to change permissions is this:

 chmod [-R] xyz 文件或目录

Options and parameters:

  • xyz : is the permission property of the number type just mentioned, which adds up to the value of the rwx property.
  • -R : Make ongoing changes to recursive, i.e. all files in this directory will change

For example, if you want to enable all permissions for the .bashrc file, the command is as follows:

[root@www ~]# ls -al .bashrc
-rw-r--r--  1 root root 395 Jul  4 11:45 .bashrc
[root@www ~]# chmod 777 .bashrc
[root@www ~]# ls -al .bashrc
-rwxrwxrwx  1 root root 395 Jul  4 11:45 .bashrc

What if you want to turn permissions into -rwxr-xr? Then the score of the permission becomes the score of the 4 plus 2 plus 1 .

The symbol type changes the file permissions

There's another way to change permissions! F rom the previous introduction, we can see that basically the nine permissions are (1) user (2) group (3) others three identities! Then we can use u, g, o to represent the permissions of the three identities!

In addition, a represents all, i.e. all identity! T hen read and write permissions can be written as r, w, x! That is, you can look at it in the following way:

chmod u
G
O
A
(Join)
- (remove)
(Settings)
R
W
The file or directory

If we need to set the file permissions to -rwxr-xr-- , we can use chmod u-rwx, g-rx, o-r file name to set:

[root@www ~]# ls -al .bashrc
-rwxr-xr-x  1 root root 395 Jul  4 11:45 .bashrc
[root@www ~]# chmod  a+w  .bashrc
[root@www ~]# ls -al .bashrc
-rwxrwxrwx  1 root root 395 Jul  4 11:45 .bashrc

And what if you want to remove permissions without changing other existing permissions? For example, to take away all people's executable permissions:

[root@www ~]# chmod  a-x  .bashrc
[root@www ~]# ls -al .bashrc
-rw-rw-rw-  1 root root 395 Jul  4 11:45 .bashrc