Linux file basic properties


Linux system is a typical multi-user system. Different users are in different positions and have different permissions. In order to protect the security of the system, the Linux system has different regulations on the permissions of different users to access the same file (including directory files).

In Linux, we can use the ll or ls -l command to display the attributes of a file and the user and group 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 example, the first one of the bin file Properties are represented by "d". "d" in Linux means that the file is a directory file.

In Linux, the first character indicates whether the file is a directory, file, link file, etc.

  • When it is [d], it is the directory

  • When it is [-], it is the directory File;

  • If it is [l], it is represented as a link file (link file);

  • If it is [b] is represented as a storable interface device (random access device) in the installation file;

  • if it is [c], it is represented as Serial port devices in the device file, such as keyboard and mouse (one-time read devices).

The following characters are grouped into groups of three, and they are all combinations of the three parameters of "rwx". Among them, [ r ] represents readable (read), [ w ] represents writable (write), and [ x ] represents executable (execute). It should be noted that the positions of these three permissions will not change. If there is no permission, a minus sign [-] will appear.

The attributes of each file are determined by the 10 characters in the first part on the left (as shown below).

1039.png

Represented by numbers 0-9 from left to right.

The 0th bit determines the file type, and the 1st-3rd bits determine the owner (the owner of the file) has the permissions of the file.

Bits 4-6 determine that the group (users in the same group of the owner) has the permissions for the file, and bits 7-9 determine that other users have permissions for the file.


Among them, the 1st, 4th and 7th bits indicate read permission. If it is expressed by the "r" character, it has read permission. If it is expressed by the "-" character, it does not have read permission. ;

The 2nd, 5th and 8th bits indicate write permission. If the "w" character is used, it means there is write permission. If the "-" character is used, it means there is no write permission; the 3rd, 6th and 9th bits indicate Executable permission. If it is represented by the "x" character, it has execution permission. If it is represented by the "-" character, it does not have the execution permission.


Linux file owner and 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 has ownership of the file.

At the same time, in the Linux system, users are classified by groups, and a user belongs to one or more groups.

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

Therefore, the Linux system stipulates different file access permissions according to the file owner, users in the same group of file owners, and other users.

In the above example, the bin file is a directory file. The owner and group are both root. The owner has readable, writable, and executable permissions; other users in the same group as the owner have readable and executable permissions. Executable permissions; other users also have read and executable permissions.

Change file attributes

1. chgrp: change file group

Syntax:

chgrp [-R] 属组名文件名

Parameter options

  • -R: Recursively change the file group, that is, when changing the group of a directory file, if you add the -R parameter, the group of all files in the directory will be changed.

2. chown: Change the file owner, you can also change the file group at the same time

Syntax:

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

Enter the /root directory (~) Change the owner of install.log to the bin 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 the owner and group of install.log back 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 9 files Properties

Linux file properties have two setting methods, one is numbers and the other is symbols.

There are nine basic permissions for Linux files. The three identities of owner/group/others each have their own read/write/execute permissions.

Let’s first review the data mentioned above: the permission characters of the file are: "-rwxrwxrwx", these nine permissions are in groups of three! Among them, we can use numbers to represent each permission. The score comparison table for each permission is as follows:

  • r:4

  • w:2

  • x:1

The three permissions (r/w/x) scores of each identity (owner/group/others) are It needs to be accumulated. For example, when the permission is: [-rwxrwx---], the score is:

  • owner = rwx = 4+2+1 = 7

  • group = rwx = 4+2+1 = 7

  • others= --- = 0+0+0 = 0

So when we set the permission changes, the permission number of the file will be 770! The syntax of the chmod command to change permissions is as follows:

chmod [-R] xyz 文件或目录

Options and parameters:

  • xyz: It is the numeric type permission attribute just mentioned, which is the rwx attribute Addition of values.

  • -R: Perform recursive and continuous changes, that is, all files in the sub-directory will be changed.

For example , if you want to set and enable all permissions of 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 change the permissions to-rwxr-xr--? Then the permission score becomes [4+2+1][4+0+1][4+0+0]=754.

Symbol type changes file permissions

There is another way to change permissions! From the previous introduction, we can find that there are basically nine permissions, namely (1) user (2) group (3) others! Then we can use u, g, o to represent the permissions of the three identities!

In addition, a represents all, that is, all identities! Then the read and write permissions can be written as r, w, x! That is to say, you can use the following method to look at it:


##+(Add) r File or directory
chmod u
g
o
a
-(Remove)
=(Setting)
w
x
If we need to set file permissions For -rwxr-xr--, you can use

chmod u=rwx,g=rx,o=r file nameto 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 if you want to remove the permissions without What about changing other existing permissions? For example, if you want to remove the executable permissions of everyone, then:

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