The use of sudo command in Linux system (CentOS7)

Use of sudo command

Example: Create a user (ordinary user), so that the user has commands to add and delete other users.
1. Create a user
useradd test and
use the cat /etc/passwd command to check whether the user is created successfully.
Because the new user information is at the end of the /etc/passwd file, we use the tail /etc/passwd command here to view only the last ten lines of the file
Insert picture description here
2. Modify the user Password
passwd test
Insert picture description here
3. View the command path for adding users (useradd) and deleting users (userdel)
whereis useradd
whereis userdel
Insert picture description here
4. Edit the /etc/sudoers file under the root user to
open a new terminal (so that we can see the 3 Command path)
vim /etc/sudoers
Add
test ALL=(ALL) /usr/sbin/useradd
test ALL=(ALL) /usr/sbin/userdel on line 101 and line 102 of the file (set nu command to view the line number) respectively
Save and exit
Insert picture description here
5. Switch to the new user (test)
su test
sudo -l
Insert picture description here

6. Use the commands to add users and delete users under this user (using the sudo command)
sudo /usr/sbin/useradd tsu
sudo /usr/sbin/userdel -r tsu
You can use the ls /home/ command to see which users are there
Insert picture description here

Complete core code:

[root@localhost ~]# useradd test
[root@localhost ~]# cat /etc/passwd //查看用户是否创建成功(文件的末行)
[root@localhost ~]# passwd test
[root@localhost ~]# whereis useradd
[root@localhost ~]# whereis userdel
[root@localhost ~]# vim /etc/sudoers   //在该文件下添加内容
[root@localhost ~]# su test
[test@localhost root]$ sudo -l
[test@localhost root]$ ls /home/     //查看创建用户之前的所有用户
[test@localhost root]$ sudo /usr/sbin/useradd tsu
[test@localhost root]$ ls /home/    //查看创建用户之后的所有用户
[test@localhost root]$ sudo /usr/sbin/userdel -r tsu
[test@localhost root]$ ls /home/    //查看删除用户之后的所有用户

This article mainly introduces the use of the sudo command in the Linux system. If you don't understand, you can private message or comment, and the blogger will definitely reply. There may be shortcomings, I hope you can give me some advice.

Guess you like

Origin blog.csdn.net/m0_53521757/article/details/112560924