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”组本身。

删除用户的主要组 (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] GROUPOptions:-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 userroot@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 Value Description
0 success
2 invalid command syntax
6 the group doesn’t exist
8 can’t delete user primary group
10 can’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)

  • groupdel man pagegroupdel手册页

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

groupdel 删除组

groupdel 删除组_如何在Linux中删除组– groupdel命令相关推荐

  1. 创建组groupadd_如何在Linux中创建组– groupadd命令

    创建组groupadd In this tutorial, we will learn how to create a Group in Linux using groupadd command. 在 ...

  2. linux 怎么删除大文件,如何在Linux中删除超大的(100-200GB)文件

    平日,要在 Linux 终端删除一个文件,我们应用 rm 敕令(删除文件).shred 敕令(安然删除文件).wipe 敕令(安然擦除文件)或者 secure-deletion 对象包(一个安然文件删 ...

  3. linux中设置环境变量_如何在Linux中设置环境变量

    linux中设置环境变量 Wondering how to set environment variables in Linux? This is exactly what we'll be doin ...

  4. python怎么去掉换行符_如何在Python中删除尾部换行符?

    如何在Python中删除尾部换行符? 什么是Perl的chomp函数的Python等价物,如果它是换行符,它会删除字符串的最后一个字符? 26个解决方案 1473 votes 尝试方法lstrip() ...

  5. linux grep 排除_如何在Linux中排除Grep?

    linux grep 排除 grep is very useful tool used by a lot of tech guys. grep provides different functions ...

  6. shell中竖线的作用_如何在 Linux 中安装、配置和使用 Fish Shell?

    每个 Linux 管理员都可能听到过 shell 这个词.你知道什么是 shell 吗? 你知道 shell 在 Linux 中的作用是什么吗? Linux 中有多少个 shell 可用? -- Ma ...

  7. linux 命令行删除分区,如何在 Linux 中删除分区

    管理分区是一件严肃的事情,尤其是当你不得不删除它们时.我发现自己经常这样做,特别是在使用 U 盘作为实时磁盘和 Linux 安装程序之后,因为它们创建了几个我以后不需要的分区. 在本教程中,我将告诉你 ...

  8. linux列出组_如何列出Linux中的所有组?

    linux列出组 Linux groups are a collection of users. They are meant to easily provide privileges to a gr ...

  9. linux 更改ssh端口_如何在Linux中更改SSH端口-简易指南

    linux 更改ssh端口 The default port on SSH is 22. But for security reasons, it's a good idea to change SS ...

最新文章

  1. python-虎扑爬虫
  2. 怎么把向上滑动取消_美瞳滑片是什么意思?为什么会滑片?怎么解决?
  3. metal分析是什么意思_metal分析.pptx
  4. 搜索引擎反作弊之内容作弊
  5. CSS基础(part3)--伪类及伪元素
  6. [019] C#基础:理解装箱与拆箱
  7. mysql加索引快很多
  8. 文件系统管理 之 文件和目录访问权限设置
  9. websocket + node.js聊天系统
  10. python从数据库取数据 显示字段名_如何在python中将SQL数据库中的字段名放入列表中...
  11. SpringBoot—数据库初始化脚本配置
  12. JS控制CSS样式语法对照
  13. 快速格式化代码(HTML、CSS)
  14. golang fmt.printf()
  15. Hadoop 2.7.1 使用minicluster进行单元测试
  16. 【Android 开发入门】我为什么要在Android找工作越来越难的时候开始学习它
  17. 债券指数基金以及债券ETF
  18. 关于百度云订阅无法正常查看订阅者分享的解决办法
  19. vscode eslint beautify 格式化 html
  20. centos8安装失败 Linux dd,在RHEL 8/CentOS 8上安装Telegraf的方法

热门文章

  1. 移动滚动条显示或隐藏元素事件
  2. 在一台机器设置两个listener(Oracle)
  3. 人生感悟:生活磨练有时也是一种财富
  4. 语音信号处理基础(二)
  5. [转载] python全局变量的使用
  6. [转载] Numpy 使用教程--Numpy 数学函数及代数运算
  7. [转载] numpy.arctan, math.atan, math.atan2的区别
  8. [转载] 详解 Numpy.ndarray
  9. [转载] python 一个简单的天气预报程序
  10. JAVA基础-类型转换