为什么“useradd”不能将用户添加到主要组?

xcz*_*zhh 1 users

我使用的命令是:

sudo useradd -g users tom

它应该将 tom 添加到组users 中,但是在检查/etc/group时我在users 中找不到 tom 。

但是,如果我只是尝试:

sudo useradd -G users jim

我发现 jim 已成功添加到用户

有没有人遇到过这个问题?

ish*_*ish 5

注意: useradd是一个低级实用程序,强烈建议您在 Ubuntu/Debian 系统上使用adduser(或usermod) 代替

  • 您最好使用该id -Gn username命令来检查主要/次要成员资格,而不是依赖/etc/group
  • 设置新用户主要(或登录)组的-g选项useradd

    • 如果设置为现有组,则不会反映在/etc/group
    • 但是您可以在/etc/passwd或使用中看到现有的 GIDid -Gn username
    • 示例(组izx是 gid 1000):
      $ sudo useradd -g izx tom
      $ grep ^izx /etc/group
      izx:x:1000:
      $ grep ^tom /etc/passwd
      汤姆:x:1008:1000 ::/home/tom:/bin/sh
      $ id -Gn 汤姆
      伊兹
      
  • 设置新用户次要(或补充)组的-G选项useradd

    • 这些会员资格反映在/etc/group和使用id -Gn username
    • 不是/etc/passwd(与-g上文)
    • 示例(组izx是 gid 1000):
      $ sudo useradd -G izx 哈利
      $ grep ^izx /etc/group
      izx:x:1000:哈利
      $ grep ^harry /etc/passwd
      哈利:x: 1009:1009 ::/home/harry:/bin/sh
      $ id -Gn 哈利
      哈利·伊兹