groupdel 删除组_如何在Linux中删除组– groupdel命令

groupdel 删除组

Linux groupdel command is used to delete a group. This is a very powerful command, so use it carefully. It’s a common Linux command and you can use it in all the Linux distributions such as Ubuntu, CentOS, Debian, Fedora, etc.

Linux groupdel命令用于删除组。 这是一个非常强大的命令,因此请谨慎使用。 这是一个常见的Linux命令,您可以在所有Linux发行版中使用它,例如Ubuntu,CentOS,Debian,Fedora等。

Let’s look at some examples of deleting a group in Linux using groupdel command.

让我们看一些使用groupdel命令在Linux中删除组的示例。

没有用户的Linux删除组 (Linux Delete Group with No Users)

For the example, I have already created few groups and a test user. Let’s look at the simple example to delete a group which has no users.

对于该示例,我已经创建了几个组和一个测试用户。 让我们看一个简单的示例,删除没有用户的组。


root@localhost:~# getent group test_users1
test_users1:x:1005:
root@localhost:~#
root@localhost:~# groupdel test_users1
root@localhost:~# getent group test_users1
root@localhost:~# 

与用户删除组 (Deleting a Group with Users)

I have created a group test_users and added journaldev user to it. Let’s confirm this using the getent and id commands.

我创建了一个test_users组,并向它添加了journaldev用户。 让我们使用getentid命令确认这一点。


root@localhost:~# getent group test_users
test_users:x:1004:journaldev
root@localhost:~# 

root@localhost:~# id journaldev
uid=1002(journaldev) gid=1003(journaldev) groups=1003(journaldev),27(sudo),1004(test_users),1007(test_users_pwd)
root@localhost:~#

Let’s see what happens when we delete this group.

让我们看看删除该组时会发生什么。


root@localhost:~# groupdel test_users
root@localhost:~# id journaldev
uid=1002(journaldev) gid=1003(journaldev) groups=1003(journaldev),27(sudo),1007(test_users_pwd)
root@localhost:~# getent group test_users
root@localhost:~# 

The group is deleted and removed from the user groups list.

该组将被删除,并从用户组列表中删除。

我们可以使用groupdel命令删除系统组吗? (Can we Delete System Group using groupdel command?)

Can we remove a system level group using groupdel command?

我们可以使用groupdel命令删除系统级别组吗?

Let’s try to remove sudo group using the groupdel command and see what happens.

让我们尝试使用groupdel命令删除sudo组,看看会发生什么。


root@localhost:~# groupdel sudo
root@localhost:~#  id journaldev
uid=1002(journaldev) gid=1003(journaldev) groups=1003(journaldev),1007(test_users_pwd)
root@localhost:~# getent group sudo
root@localhost:~# 

Looks like we were able to remove the “sudo” user group. Let’s see what happens to the superuser privileges of the user.

看来我们能够删除“ sudo”用户组。 让我们看看用户的超级用户特权发生了什么。


root@localhost:~# su - journaldev
journaldev@localhost:~$ 
journaldev@localhost:~$ ls /root
ls: cannot open directory '/root': Permission denied
journaldev@localhost:~$ sudo ls /root
[sudo] password for journaldev: 
journaldev is not in the sudoers file.  This incident will be reported.
journaldev@localhost:~$ 

Looks like sudo privileges are removed too, which is obvious because we removed the “sudo” group itself.

看起来sudo特权也被删除了,这很明显,因为我们删除了“ sudo”组本身。

Linux Delete Group Groupdel Command

删除用户的主要组 (Deleting Primary Group of a User)

When we create a new user, a new group with the same name is also created and assigned to it. This is called the primary group of the user.

当我们创建一个新用户时,还将创建一个具有相同名称的新组并将其分配给它。 这称为用户的主要组。

Let’s see if we can remove the primary group of the user?

让我们看看是否可以删除用户的主要组?


root@localhost:~# groupdel journaldev
groupdel: cannot remove the primary group of user 'journaldev'
root@localhost:~#

Let’s look at the help option to see if there is any way to overcome this error message?

让我们看一下帮助选项,看看是否有任何方法可以克服此错误消息?


root@localhost:~# groupdel -h
Usage: groupdel [options] GROUP

Options:
  -h, --help                    display this help message and exit
  -R, --root CHROOT_DIR         directory to chroot into
  -f, --force                   delete group even if it is the primary group of a user

root@localhost:~# 

The output clearly states that we can use the -f option to delete the primary group of the user.

输出清楚地表明,我们可以使用-f选项删除用户的主要组。


root@localhost:~# groupdel -f journaldev
root@localhost:~# id journaldev
uid=1002(journaldev) gid=1003 groups=1003
root@localhost:~# 

We were able to remove the primary group of the user. But, the “gid=1003” is still showing in the user information. So, I don’t understand what is the use case for this feature.

我们能够删除用户的主要组。 但是,“ gid = 1003”仍显示在用户信息中。 因此,我不知道此功能的用例是什么。

Linux groupdel命令退出值 (Linux groupdel command exit values)

If you are using groupdel command in a shell script, it’s better to know the exit values to check whether command executed fine or got some error.

如果您在Shell脚本中使用groupdel命令,最好知道退出值,以检查命令是否执行正常或出现一些错误。

Exit ValueDescription
0success
2invalid command syntax
6the group doesn’t exist
8can’t delete user primary group
10can’t update group information files
退出值 描述
0 成功
2 无效的命令语法
6 该组不存在
8 无法删除用户主要组
10 无法更新群组信息文件

结论 (Conclusion)

Linux groupdel command is very powerful and it can remove system groups too. If you are using this command to remove a group, please take extra care because it’s irreversible and it can create major issues if the group had many users.

Linux groupdel命令功能非常强大,它也可以删除系统组。 如果使用此命令删除组,请格外小心,因为它是不可逆的,并且如果组中有很多用户,它可能会引起严重问题。

参考 (Reference)

翻译自: https://www.journaldev.com/39787/linux-delete-group-groupdel-command

groupdel 删除组

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值