centOS 7 添加删除用户和用户组

2023-08-01,,

1、添加新用户

由于日常使用时root用户权限过大,所以添加一个用户供日常使用,或者供他人使用。

1 新增用户

adduser [用户名]


[root@centos ~]# adduser dex

2 设置密码

passwd [用户名]

[root@centos ~]# passwd dex
Changing password for user dex.
New password:
Retype new password:
sswd: all authentication tokens updated successfully.

3 授权

新创建的用户并不能使用sudo命令,需要给他添加授权。

个人用户的权限只可以在本home下有完整权限,其他目录要看别人授权。而经常需要root用户的权限,这时候sudo可以化身为root来操作。我记得我曾经sudo创建了文件,然后发现自己并没有读写权限,因为查看权限是root创建的。

新创建的用户并不能使用sudo命令,需要给他添加授权。

sudo命令的授权管理是在sudoers文件里的。可以看看sudoers:

[root@centos ~]# sudoers
bash: sudoers: 未找到命令...
[root@centos ~]# whereis sudoers
sudoers: /etc/sudoers /etc/sudoers.d /usr/libexec/sudoers.so /usr/share/man/man5/sudoers.5.gz

找到这个文件位置之后再查看权限:


[root@centos ~]# ls -l /etc/sudoers
-r--r----- 1 root root 4251 9月 25 15:08 /etc/sudoers

是的,只有只读的权限,如果想要修改的话,需要先添加w权限:

chmod -v u+w /etc/sudoers #添加sudoers文件可写权限


[root@centos ~]# chmod -v u+w /etc/sudoers
mode of "/etc/sudoers" changed from 0440 (r--r-----) to 0640 (rw-r-----)

然后就可以添加内容了,在下面的一行下追加新增的用户:

[root@centos ~]# vim /etc/sudoers

## Allow root to run any commands anywher
root ALL=(ALL) ALL
linuxidc ALL=(ALL) ALL #这个是新增的用户

wq保存退出,这时候要记得将写权限收回:

chmod -v u-w /etc/sudoers # 收回写权限

[root@centos ~]# chmod -v u-w /etc/sudoers
mode of "/etc/sudoers" changed from 0640 (rw-r-----) to 0440 (r--r-----)

这时候使用新用户登录,使用sudo:


[root@centos ~]$ sudo cat /etc/passwd
[sudo] password for linuxidc: We trust you have received the usual lecture from the local System
Administrator. It usually boils down to these three things: #1) Respect the privacy of others.
#2) Think before you type.
#3) With great power comes great responsibility.

第一次使用会提示你,你已经化身超人,身负责任。而且需要输入密码才可以下一步。如果不想需要输入密码怎么办,将最后一个ALL修改成NOPASSWD: ALL。

2、增加用户组

1 新建test工作组


[root@centos ~]# groupadd testgroup

2 新建dex用户并增加到testgroup工作组


[root@centos ~]# useradd -g testgroup testuser

注::-g 所属组 -d 家目录 -s 所用的SHELL

3 给已有的用户增加工作组

usermod -G groupname username


[root@centos ~]# usermod -G testgroup dex

3、删除用户

1 临时关闭

关闭用户账号:

[root@centos ~]$ passwd dex –l

重新释放:

[root@centos ~]$ passwd dex –u

2 永久性删除用户账号

[root@centos ~]$ userdel dex
groupdel testgroup
usermod –G testgroup testuser //(强制删除该用户的主目录和主目录下的所有文件和子目录)

3 删除用户组

[root@centos ~]$ groupdel testgroup
# 或者 (强制删除该用户的主目录和主目录下的所有文件和子目录)
[root@centos ~]$ usermod –G testgroup dex

centOS 7 添加删除用户和用户组的相关教程结束。

《centOS 7 添加删除用户和用户组.doc》

下载本文的Word格式文档,以方便收藏与打印。