Table of contents
3. The group of the file/directory
1. Modify the group where the file/directory belongs
2. Modify the group to which the user belongs
4. Basic introduction to permissions
5. Detailed explanation of rwx permissions
1. Basic introduction
In Linux, a user belongs to a group; when we create a user, a directory with the same name as the user name will be created in /home/xx by default, which is the user's home directory; when a user logs in, it will switch to /home/xx directory; a user cannot enter other user directories by default; user permissions are reflected in the operation of files.
2. Owner of file/directory
The creator of a file is the owner of the file.
1. View the owner of a file
ls -ahl file/directory //View the owner of the file
The output results are displayed in order: file type and permissions, number of links, file owner, file group, file size, creation or latest modification time, and file name. Detailed introduction follows.
2. Modify file owner
chown [options] [end user] [file or directory] //Change the owner of the file or directory
chown [options] [end-user:end-group] [file or directory] //Change the owner and all groups of a file or directory
Option description
Options | Function |
-R | Recursive operation |
for example:
(1) Change the owner of the a.txt file to Billie
chown Billie a.txt
(2) Recursively change file owners and all groups
chown -R Billie:grp_2 hello/
3. The group of the file/directory
1. Modify the group where the file/directory belongs
charp [end-user group] [file or directory] // Change the group to which a file or directory belongs
for example:
Modify the group to which the a.txt file belongs to grp_2
chgrp grp_2 a.txt
2. Modify the group to which the user belongs
usermod [options] User group username
Option description
Options | Function |
-g | Modify the user's initial login group. The given group must exist. The default group id is 1 |
4. Basic introduction to permissions
The following is displayed in ls -l :
-rwxrw-r-- 1 root grp_2 1213 feb 2 08:43 a.txt
Parameter Description
Number of digits | illustrate |
No. 0 | File type (-: ordinary file; d: directory; l: connection file; c: character device file; b: block device file) |
No. 1-3 | The file owner has permissions on the file |
No. 4-6 | The group to which the file belongs has permissions on the file |
No. 7-9 | Other users have permissions to the file |
1 | If it is a file, it indicates the number of hard links. If it is a folder, it indicates the number of subdirectories. |
root |
File owner |
grp_2 | The group to which the file belongs |
1213 | The file size, if it is a directory, is unified to 4096 |
feb 2 08:43 | Indicates the time when the file was created or last modified |
a.txt | file name |
5. Detailed explanation of rwx permissions
1.rwx applies to files
(1) r stands for readable: can be read and viewed.
(2) w means writable: the file can be modified, but the file cannot be deleted. The prerequisite for deleting a file is that you have write permission for the file.
(3) x represents executable: can be executed.
2.rwx acts on the directory
(1) r stands for readable: can be read and viewed.
(2) w stands for writable: it can be modified, created, deleted, and renamed within the directory.
(3) x represents executable: you can enter the directory.
6. Modify permissions
Through the chmod command, you can modify the permissions of files or directories.
(1) The first method: +-= change permissions
chmod [{ugoa}{+-=}{rwx}] file or directory
u: Owner g: All groups o: Others a: Everyone
(2) The second method: digital change permissions
chmod [mode=421] [file or directory]
r=4 w=2 x=1 rwx=4+2+1=7 rw=4+2=6 rx=4+1=5 wx=2+1=3
for example:
(1) chmod u=rwxg=rx,o=x file or directory // means giving rwx permissions to the owner, rx permissions to the user in the group, and x permissions to others.
(2) chmod 751 file or directory is equivalent to chmod u=rwxg=rx,o=x file or directory