Linux file permissions and managers
There are three types of linux permissions, r (read) can be read, w (write) can be written, and x (eXecute) can be executable.
r For a file, it has the permission to read the content of the file; for a directory, it has the permission to browse the directory.
w For files, it has the authority to add, modify, and delete file content; for directories, it has the authority to create, delete, modify, and move files in the directory.
x For files, it has the authority to execute files; for directories, the user has the authority to enter the directory.
There are three types of linux file managers: owner, group, and other groups.
- The owner is generally the creator of the file. The creator naturally becomes the owner of the file and can change the owner.
- Group. When a user creates a file, the group where the user belongs becomes the group where the file belongs by default, and the group where the user belongs can be changed.
- Other groups, users other than the owner and users in the group are other groups
[root@localhost yuanwanli]# useradd zhangsan # 创建用户zhangsan
[root@localhost yuanwanli]# su zhangsan
[zhangsan@localhost ~]$ touch test.txt # 使用zhangsan用户创建文件test.txt
[zhangsan@localhost ~]$ ls -l
total 0
-rw-rw-r--. 1 zhangsan zhangsan 0 Aug 23 20:14 test.txt # 所有者 ,所在组分别为zhangsan, zhangsan .
ls -l content explanation
- Identify the file type,-ordinary file, d directory, l soft link, c character device (keyboard, mouse), b block file
- 9 consecutive characters, each of which represents the owner, the group, and the permissions of other groups. For example, rw- rw- r-means that the owner has rw permission but no x permission, members of the group have rw permission but no x permission, and other groups only have r permission.
- Number, if it is a directory, it means the number of subdirectories, if it is a file, it means the number of soft connections
- File owner
- File group
- Number, indicating the file size, if it is a directory, it is 4096
- File creation date
- File name and suffix
Modify permissions chmod command
Method 1: Pass +-= change permissions
"=" Means to assign a value to the permission
[zhangsan@localhost ~]$ chmod u=rwx,g=rwx,o=rw test.txt
[zhangsan@localhost ~]$ ls -l
total 12
-rw-r--r--. 1 root root 9792 Aug 23 22:23 ok.txt
-rwxrwxrw-. 1 zhangsan zhangsan 0 Aug 23 20:14 test.txt
"+ -" means to increase or decrease the permissions, for example, cancel the execution permission for the owner and group of the test.txt file, and increase the execution permission for other groups.
[zhangsan@localhost ~]$ chmod u-x,g-x,o+x test.txt
[zhangsan@localhost ~]$ ls -l
total 12
-rw-r--r--. 1 root root 10251 Aug 23 22:26 ok.txt
-rw-rw-rwx. 1 zhangsan zhangsan 0 Aug 23 20:14 test.txt
Method 2: Change permissions through numbers
The number rules are r=4, w=2, x=1, rwx = 7, rw = 6, r= 4
For example, set the owner authority of the test.txt file to rwx, the group where it belongs to rw, and the other groups to r.
[zhangsan@localhost ~]$ chmod 764 test.txt
[zhangsan@localhost ~]$ ls -l
total 28
-rw-r--r--. 1 root root 24786 Aug 24 00:01 ok.txt
-rwxrw-r--. 1 zhangsan zhangsan 0 Aug 23 20:14 test.txt
Modify file owner and group
Use chown and chgrp commands respectively. Usage is as follows
[root@localhost zhangsan]# chown root test.txt # test.txt的所有者修改为zhangsan
[root@localhost zhangsan]# ll
total 28
-rw-r--r--. 1 root root 26316 Aug 24 00:11 ok.txt
-rwxrw-r--. 1 root zhangsan 0 Aug 23 20:14 test.txt
[root@localhost zhangsan]# chown zhangsan:root test.txt # 同时修改所有者与所在组
[root@localhost zhangsan]# ll
total 28
-rw-r--r--. 1 root root 26316 Aug 24 00:11 ok.txt
-rwxrw-r--. 1 zhangsan root 0 Aug 23 20:14 test.txt
[root@localhost zhangsan]# chgrp zhangsan test.txt
[root@localhost zhangsan]# ll
total 28
-rw-r--r--. 1 root root 26622 Aug 24 00:13 ok.txt
-rwxrw-r--. 1 zhangsan zhangsan 0 Aug 23 20:14 test.txt
-R Process this directory and all files in this directory.
[root@localhost home]# chown -R root:root /home
[root@localhost home]# ll
-rwxr-xr-x. 1 root root 227024 Aug 16 08:42 vmware-uninstall-tools.pl
lrwxrwxrwx. 1 root root 48 Aug 16 08:46 vmware-user -> /lib/vmware-tools
drwx------. 4 root root 4096 Aug 20 05:11 xingdian
drwx------. 30 root root 4096 Aug 23 20:10 yuanwanli
drwx------. 4 root root 4096 Aug 24 00:11 zhangsan
File color
The file type represented by the file color in Linux. Familiar with the color can help us understand the file faster
colour | Types of |
---|---|
green | Executable file |
red | Compressed file or package file |
blue | table of Contents |
white | General files, such as text files, configuration files, source code files, etc. |
Light blue | Link files, mainly files created using the ln command |
Flashing red | Indicates that there is a problem with the linked file |
yellow | Device file |
gray | Represents other files |