English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
The Linux system is a typical multi-user system where different users are in different positions and have different permissions.
To protect the security of the system, Linux systems have different specifications for the permissions of different users to access the same file (including directory files).
In Linux, we usually use the following two commands to modify the owner and permissions of a file or directory:
chown (change ownerp): Modify the owner and group.
chmod (change mode): Modify the user's permissions.
In the figure below, the user is authorized through chown, and the user's permissions to open the door are set through chmod.
In Linux, we can use the ll or ls –l command to display a file's attributes as well as the user and group to which the file belongs, for example:
[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,bin The first attribute of the file is represented by d. d signifies a directory file in Linux.
In Linux, the first character represents whether this file is a directory, file, or link file, etc.
When it is d, it is a directory
When it is - is a file;
If it is l, it indicates a link document (link file);
If it is b, it indicates a storage interface device (random access device) inside the device file;
If it is c, it indicates a serial port device inside the device file, such as a keyboard, mouse (one-time read device).
The next characters are grouped in threes, and all are combinations of the three parameters 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 do not change; if there is no permission, a minus sign will appear - and so on.
The attributes of each file are determined by the first part on the left 10 Characters to determine (as shown in the figure below).
From left to right, use 0-9 These numbers are used to represent.
The 0 Bit determines the file type, the 1-3 Bit determines the permissions of the owner (the owner of the file) for the file.
The4-6Bit determines the permissions of the group (users in the same group as the owner) for the file, the7-9Bit determines the permissions of other users for the file.
Among them, the 1,4,7 Bit representation of read permission, if represented by the r character, then there is read permission, if represented by - Character representation means no read permission;
The 2,5,8 Bit representation of write permission, if represented by the w character, then there is write permission, if represented by - Character representation means no write permission; the 3,6,9 Bit representation of executable permission, if represented by the x character, then there is execution permission, if represented by - Character representation means no execution permission.
[root@www /]# ls -l total 64 drwxr-xr-x 2 root root 4096 Feb 15 14:46 cron drwxr-xr-x 3 mysql mysql 4096 Apr 21 2014 mysql ...
For a file, it has a specific owner, which is the user who has ownership of the file.
At the same time, in the Linux system, users are categorized by groups, and a user belongs to one or more groups.
Users other than the file owner can be further divided into users in the same group as the file owner and other users.
Therefore, the Linux system specifies different file access permissions for the file owner, users in the same group as the file owner, and other users.
In the above examples, the mysql file is a directory file, owned by mysql, with read, write, and execute permissions; other users in the same group as the owner have read and execute permissions; other users also have read and execute permissions.
For the root user, in general, file permissions do not take effect on them.
Syntax:
chgrp [-R] Group name File name
parameter options
-R: Recursive change file group, that is, when changing the group of a directory file, if you add-The R parameter, then the group of all files in the directory will be changed.
Syntax:
chown [–R] owner name file name chown [-R] owner name: group name file name
Enter /The root directory (~) changes the owner of install.log to the account bin:
[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
Linux file attributes have two setting methods, one is numeric and the other is symbolic.
The basic permissions of Linux files are nine, namely owner/group/others (owner/group/other) There are three identities each with their own read/write/execute Permission.
Let's review the data mentioned above: the permission characters of the file are: -rwxrwxrwx, these nine permissions are grouped in threes! Among them, we can use numbers to represent each permission, and the corresponding score table of each permission is as follows:
r:4
w:2
x:1
Each identity (owner/group/others) Each has its three permissions (r/w/The score needs to be accumulated, for example, when the permission is: -rwxrwx--- The score is as follows:
owner = rwx = 4+2+1 = 7
group = rwx = 4+2+1 = 7
others = --- = 0+0+0 = 0
So when we set the permission change, the permission number of the file will be 770The syntax of the permission change command chmod is as follows:
chmod [-R] xyz file or directory
Options and parameters:
xyz: This is the just mentioned numerical type of permission attribute, which is the sum of the rwx attribute values.
-R: Perform recursive (recursive) continuous changes, which means that all files in the subdirectory will also be changed
For example, if you want to set all permissions of the .bashrc file to be enabled, 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
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
There is another way to change permissions, from the previous introduction, we can find that there are basically nine permissions, respectively:
user: user
group: group
others: others
Then we can use u, g, o to represent the permissions of the three identities.
In addition, a then it represents allwhich means all identities. Read and write permissions can be written as r, w, x, that is, the following table can be used to view them:
chmod | u g o a | +(add) -(remove) =(set) | r w x | File or Directory |
If we need to set the file permissions to -rwxr-xr-- You can use chmod u=rwx,g=rx,o=r filename to set:
# touch test1 // Create test1 File # ls -al test1 // View test1 Default Permissions -rw-r--r-- 1 root root 0 Nov 15 10:32 test1 # chmod u=rwx,g=rx,o=r test1 // Modify test1 Permissions # ls -al test1 -rwxr-xr-- 1 root root 0 Nov 15 10:32 test1
And if you want to remove permissions without changing the existing ones, for example, to remove executable permissions for all users, then:
# chmod a-x test1 # ls -al test1 -rw-r--r-- 1 root root 0 Nov 15 10:32 test1