1. Group management
1. Introduction to the Linux group
In Linux, each user must belong to a group and cannot be independent from outside the group. In Linux, each file has the concept of owner, all groups, and other groups
①Owner
②The group you belong to
③Other groups
④Change the group the user belongs to
2. File/directory owner
The user who created the file is automatically the owner of the file.
View file owner
①Command: ls -ahl (all human list)
②Application: Create a group police, then create a user tom, put tom in the police group, and then use tom to create a file ok.txt
Create group police groupadd police
Create user tom and put it in the police group useradd -g police tom
Set tom’s password passwd tom
Log in as user tom
Create file ok.txt touch ok.txt
3. Modify the file owner Change Owner - the group where the file is located has not changed
①Command: chown user name file name
②Application: Use root to create a file apple.txt, and then change its owner to tom
Create file: touch ok.txt
Change the owner of the file: chown tom apple.txt
View file owner: ls -ahl
4.Creation of groups
①Instruction
groupadd group name
②Application
Create a group monster and create a user fox and put it in the monster group
groupadd monster
useradd -g monster fox
5. Modify the group in which the file is located - without changing the owner
①Instructions:
chgrp group name file name
②Application
Use the root user to create the file orange.txt, check which group the file currently belongs to, and then modify the file to the police group.
Create file: touch orange.txt
Modify to the police group: chgrp police orange.txt
6. Modify the group to which the user belongs
①Instructions:
usermod -g group name username
②Case:
Create a bandit group (bandit) and change the user tom from the original police group to the bandit (bandit) group
Create a group: groupadd bandit
Modify group: usermod -g bandit tom
2. Permission management
1. Basic introduction to permissions
The meaning of command ll information
①File type:
- Ordinary file d directory 1 soft link c character device b block file
②Permissions in groups of 3
rreadable _
w is writable and can be modified. Files may not necessarily be deleted. To delete files, you must have write permissions for the directory.
xexecutable _
2. Modify permissions
instruction
chmod command, modify file or directory permissions
Method 1: +, -, = change permissions
uOwnergAllGroupsoOthersaEveryone _ _ _ _ _ _ _
①chmod u=rwx,g=rx,o=x file directory name
②chmod o+w file directory name
③chmod ax file directory name
Case:
1) Give the owner of the abc file read, write and execute permissions, give the group read and execute permissions, and give other groups read and execute permissions.
chmod u=rwx,g=rx,o=rx abc
2) Remove execution permissions for the owner of the abc file and add group write permissions
chmod u-x,g+w abc
3) Add read permissions to all users of the abc file
chmod a+r abc
Method 2: Change permissions through numbers
r=4
w=2
x=1
rwx=7
rx=5
Case:
Change the permissions of the /home/abc.txt file to rwx r-x r-x, using the method of giving numbers:
rwx=4+2+1=7
r-x=4+1=5
chmod 755 /home/abc.txt
3. Modify the file owner chown
instruction
chown newowner file changes the owner of the file
chown newowner:newgroup file changes the user's owner and all groups
-R recursively applies to all directories
Case
1) Please change the owner of the /home/abc .txt file to tom
chown tom /home/abc.txt
2) Please change the owner of all files and directories in the /home/kkk directory to tom
chown -R tom /home/kkk
4. Modify the group where the file is located
instruction
chgrp new group name file
Case
1) Please change the group of the /home/abc.txt file to bandit (bandit)
chgrp bandit /home/abc.txt
2) Please change the group of all files and directories in the /home/kkk directory to bandit (bandit)
chgrp -R bandit /home/kkk
5. Practice
Create group:police,bandit
Users who created the police group: jack, jerry
Users who create bandit group:xh,xq
①Create a group
groupadd police
groupadd bandit
②Create user
police group
useradd -g police jack
useradd -g police jerry
bandit group
useradd -g bandit xh
useradd -g bandit xq
③Jack creates a file jack01.txt, which he can read and write. People in this group can read it, but other groups have no permissions.
Create a file
touch jack01.txt
Modify permissions
chmod 640 jack01.txt
④Jack modifies the file so that other groups can read it and people in this group can read and write.
chmod g=rw,o=r jack01.txt
⑤xh Go to the police and see if you can read and write
root modification group
usermod -g police xh
jack gives read and write permissions to /home/jack’s group
chmod g=rx /home/jack/
After xh logs out, you can read the jack01.txt file.
cat /home/jack/jack01.txt