Ubuntu:新增和删除用户

Ubuntu:新增和删除用户,修改用户组信息。

参考:https://www.digitalocean.com/community/tutorials/how-to-add-and-delete-users-on-ubuntu-16-04#how-to-delete-a-user。

Linux上root用户是权力最大的用户,但是也非常危险,处于安全考虑,增加个人用户是必要的方法,下文讲了讲在Ubuntu上如何新增和删除用户。

创建用户

实例: root用户新增用户chenming

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
root@iZhp3fz3iqsadyes2s8ay8Z:~# adduser chenming
Adding user `chenming' ...
Adding new group `chenming' (1000) ...
Adding new user `chenming' (1000) with group `chenming' ...
Creating home directory `/home/chenming' ...
Copying files from `/etc/skel' ...
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully
Changing the user information for chenming
Enter the new value, or press ENTER for the default
Full Name []:
Room Number []:
Work Phone []:
Home Phone []:
Other []:
Is the information correct? [Y/n] y


首先创建了一个新的用户组chenming
在这个组内新建了用户chenming
要求你输入密码。
要求输入一些其他信息,可以按回车略过。
最后按下y对以上信息进行确认。

实例:root用户新增用户

1
$sudo adduser chenming

给用户授权

实例:chenming加到sudo组里面

1
2
3
root@iZhp3fz3iqsadyes2s8ay8Z:~# groups chenming
chenming : chenming

可以看到,chenming只在chenming的组里面(前面是用户名,冒号后面是组名)。在这个组里面,可能很多命令你都不能执行。

1
root@iZhp3fz3iqsadyes2s8ay8Z:~# usermod -aG sudo chenming

再来看一下:

1
2
root@iZhp3fz3iqsadyes2s8ay8Z:~# groups chenming
chenming : chenming sudo

look,进入了sudo组了,这下你可以臭屁了。

还有一种方法可以加入sodu组。
如果是root用户。

1
root@iZhp3fz3iqsadyes2s8ay8Z:~# visudo

这个时候会打开一个文本编辑器,去编辑/etc/sudoer这个文件,可能是vim,也可能是nano
找到:

1
2
# User privilege specification
root ALL=(ALL:ALL) ALL

在下面加入:

1
chenming ALL=(ALL:ALL) ALL

保存(vim下是:xnano下是ctrl+x),退出,这样chenming这个用户就加入了sudo组。

删除用户

仅仅删除用户:

1
2
3
4
root@iZhp3fz3iqsadyes2s8ay8Z:~# deluser chenming
Removing user `chenming' ...
Warning: group `chenming' has no more members.
Done.

将用户的目录也删除:

1
root@iZhp3fz3iqsadyes2s8ay8Z:~# deluser --remove-home chenming

但这个时候,这个已经被删除的用户还是在sudo组里面。
参照上面的过程,使用visudo命令,删掉增加的那一行即可。