Linux基础命令[25]-groupadd

发布于:2024-04-26 ⋅ 阅读:(15) ⋅ 点赞:(0)

1. groupadd 命令说明

groupadd:用于创建用户组,基本信息如下:

Usage: groupadd [options] GROUP

Options:
  -f, --force                   exit successfully if the group already exists,
                                and cancel -g if the GID is already used
  -g, --gid GID                 use GID for the new group
  -h, --help                    display this help message and exit
  -K, --key KEY=VALUE           override /etc/login.defs defaults
  -o, --non-unique              allow to create groups with duplicate
                                (non-unique) GID
  -p, --password PASSWORD       use this encrypted password for the new group
  -r, --system                  create a system account
  -R, --root CHROOT_DIR         directory to chroot into
  -P, --prefix PREFIX_DIR       directory prefix

基本参数如下:

选项 作用
-f, --force 强制创建新用户组
-g, --gid GID 指定新用户组的组ID
-h 显示帮助信息并退出
-K, --key KEY=VALUE 覆盖/etc/login.defs 中的默认值
-o, --non-unique 允许创建有重复 GID 的用户组
-p, --password PASSWORD 为新用户组使用加密过的密码
-r, --system 创建一个系统账户
-R, --root CHROOT_DIR chroot 到的目录
-P, --prefix PREFIX_DIR 指定新创建的用户组的父用户组

2. groupadd 命令语法

groupadd [options] GROUP

3. groupadd 命令示例

3.1 不加参数

不加参数时,直接创建用户组,组ID默认新增1,若用户组已存在,会有报错。

/etc/group 保存有组信息。

[root@localhost ~]# tail -5 /etc/group
tuser3:x:1007:
tuser13:x:981:
demo1:x:1008:
demo2:x:1010:
tuser15:x:1011:
[root@localhost ~]# groupadd tuser16
[root@localhost ~]# tail -5 /etc/group
tuser13:x:981:
demo1:x:1008:
demo2:x:1010:
tuser15:x:1011:
tuser16:x:1012:
[root@localhost ~]# groupadd tuser16
groupadd: group 'tuser16' already exists
[root@localhost ~]# 

在这里插入图片描述

3.2 -f(强制创建)

-f 可以强制创建用户组,一般用于脚本。

[root@localhost ~]# groupadd -f tuser16
[root@localhost ~]# tail -5 /etc/group
tuser13:x:981:
demo1:x:1008:
demo2:x:1010:
tuser15:x:1011:
tuser16:x:1012:
[root@localhost ~]# 

在这里插入图片描述

3.3 -g(指定组ID)

组ID必须是一个正整数,且唯一。默认是上个组ID自增1,-g 可以指定新用户组的组ID。

[root@localhost ~]# groupadd -g 11020 tuser17
[root@localhost ~]# tail -5 /etc/group
demo1:x:1008:
demo2:x:1010:
tuser15:x:1011:
tuser16:x:1012:
tuser17:x:11020:
[root@localhost ~]# 

在这里插入图片描述

3.4 -r(系统用户组)

系统用户组的组ID通常是小于1000的值,并且不会显示在登录界面的用户列表中。这些组通常用于系统服务和进程。

[root@localhost ~]# groupadd -r sysgrp
[root@localhost ~]# tail -5 /etc/group
tuser15:x:1011:
tuser16:x:1012:
tuser17:x:11020:
test16:x:11021:
sysgrp:x:980:
[root@localhost ~]# 

在这里插入图片描述

4. 总结

groupadd 用于创建自定义用户组,并可以改变其部分属性。