adduser と useradd の違いについて調べてみた

Linux系のOSでは、ユーザーを追加するためのコマンドとして、useraddadduserの2つが存在する。 コマンド名も似ていることから、どのような違いがあるのか、使い分けなどが必要なのかを調べてみた。

簡単なまとめ

  • どちらのコマンドも、ユーザを作成する機能自体は同じ。
  • adduser ではパスワードを対話形式で設定する必要がある。useradd では対話なしで、オプションで指定する必要がある。
  • Debian系のOSであるUbuntu では adduserが推奨されている。CentOS ではどちらのコマンドでも useradd が実行される。

環境

それぞれのコマンドは、以下の環境で実行して確認した。

adduser

対話形式でユーザを作成する。ユーザとグループ、ホームディレクトリの作成後に、パスワードとGECOSフィールド*1の入力を求められる。GECOSフィールドを設定しない場合は空欄のままEnterで進めることができる。

# adduser test
Adding user `test' ...
Adding new group `test' (1000) ...
Adding new user `test' (1000) with group `test' ...
Creating home directory `/home/test' ...
Copying files from `/etc/skel' ...
New password: 
Retype new password: 
passwd: password updated successfully
Changing the user information for test
Enter the new value, or press ENTER for the default
    Full Name []: 
    Room Number []: 
    Work Phone []: 
    Home Phone []: 
    Other []: 
Is the information correct? [Y/n] Y

デフォルトの設定は /etc/adduser.conf ファイルに記載されている。

# grep -E ^[^#] /etc/adduser.conf 
DSHELL=/bin/bash
DHOME=/home
GROUPHOMES=no
LETTERHOMES=no
SKEL=/etc/skel
FIRST_SYSTEM_UID=100
LAST_SYSTEM_UID=999
FIRST_SYSTEM_GID=100
LAST_SYSTEM_GID=999
FIRST_UID=1000
LAST_UID=59999
FIRST_GID=1000
LAST_GID=59999
USERGROUPS=yes
USERS_GID=100
DIR_MODE=0755
SETGID_HOME=no
QUOTAUSER=""
SKEL_IGNORE_REGEX="dpkg-(old|new|dist|save)"

useradd

対話なしでユーザを作成する。コマンド実行後に出力がされないが、ユーザは作成されている。

# useradd test
(出力なし)

adduser と異なり、デフォルトでは初期パスワードの設定がされないので、ユーザの作成後に passwd コマンドで設定する必要がある。ユーザ作成と同時にパスワードを設定したい場合は、-pオプションをつけて実行する。この場合もコマンド実行後に出力はされない。

# useradd test -p password
(出力なし)

デフォルトの設定は /etc/default/useradd ファイルに記載されている。

# grep -E ^[^#] /etc/default/useradd 
GROUP=100
HOME=/home
INACTIVE=-1
EXPIRE=
SHELL=/bin/bash
SKEL=/etc/skel
CREATE_MAIL_SPOOL=yes

useradd コマンドに--default オプションをつけて実行しても確認できる。

# useradd --default
GROUP=100
HOME=/home
INACTIVE=-1
EXPIRE=
SHELL=/bin/bash
SKEL=/etc/skel
CREATE_MAIL_SPOOL=yes

オプションと書式の比較

基本的な使い方は、上で記載したようにどちらのコマンドもほぼ変わりはないが、オプションまでは完全に一致しているわけではないので、それぞれのコマンドの書式に合わせて指定する必要がある。

adduser のオプションは以下。オプション名はほぼ読んでそのまま。

# adduser --help
adduser [--home DIR] [--shell SHELL] [--no-create-home] [--uid ID]
[--firstuid ID] [--lastuid ID] [--gecos GECOS] [--ingroup GROUP | --gid ID]
[--disabled-password] [--disabled-login] [--add_extra_groups]
[--encrypt-home] USER
...

useradd のオプションは以下。こちらには説明が併記されている。

# useradd --help
Usage: useradd [options] LOGIN
       useradd -D
       useradd -D [options]

Options:
  -b, --base-dir BASE_DIR       base directory for the home directory of the
                                new account
  -c, --comment COMMENT         GECOS field of the new account
  -d, --home-dir HOME_DIR       home directory of the new account
  -D, --defaults                print or change default useradd configuration
  -e, --expiredate EXPIRE_DATE  expiration date of the new account
  -f, --inactive INACTIVE       password inactivity period of the new account
  -g, --gid GROUP               name or ID of the primary group of the new
                                account
  -G, --groups GROUPS           list of supplementary groups of the new
                                account
  -h, --help                    display this help message and exit
  -k, --skel SKEL_DIR           use this alternative skeleton directory
  -K, --key KEY=VALUE           override /etc/login.defs defaults
  -l, --no-log-init             do not add the user to the lastlog and
                                faillog databases
  -m, --create-home             create the user's home directory
  -M, --no-create-home          do not create the user's home directory
  -N, --no-user-group           do not create a group with the same name as
                                the user
  -o, --non-unique              allow to create users with duplicate
                                (non-unique) UID
  -p, --password PASSWORD       encrypted password of the new account
  -r, --system                  create a system account
  -R, --root CHROOT_DIR         directory to chroot into
  -P, --prefix PREFIX_DIR       prefix directory where are located the /etc/* files
  -s, --shell SHELL             login shell of the new account
  -u, --uid UID                 user ID of the new account
  -U, --user-group              create a group with the same name as the user
  -Z, --selinux-user SEUSER     use a specific SEUSER for the SELinux user mapping

なぜ2つのコマンドが存在するのか

昔のUnixにはどちらのコマンドも存在せず、ユーザを追加するには以下の手順を踏む必要があった。

  1. /etc/passwdを編集
  2. /etc/groupを編集
  3. ホームディレクトリを作成
  4. ドットファイル群(.bashrcなど、'.' から始まる設定ファイル)を、/etc/skelから作成したホームディレクトリにコピー
  5. 作成したディレクトリとファイルの所有権を変更する(chown/chgrp)

(これではさすがに面倒なので)後に専用コマンドが作成されたが、BSD系統では adduser、SystemV系統では useradd というコマンドが作成された。そのため2つのコマンドが存在しているが、現在では互換性を保つ目的などがあり、ほとんどのOSではどちらのコマンドでもユーザ作成が可能なようになっている模様。

Ubuntuではどちらのコマンドも実行可能になっているが、man useradduseradd のマニュアルを確認すると、Debian*2では adduser の利用が推奨されているとの記載がある。

# ls -l /usr/sbin/useradd
-rwxr-xr-x 1 root root 147160 May 28  2020 /usr/sbin/useradd
# ls -l /usr/sbin/adduser 
-rwxr-xr-x 1 root root 37785 Apr 16  2020 /usr/sbin/adduser


# man useradd
...
DESCRIPTION
       useradd is a low level utility for adding users. On Debian, administrators should usually use
       adduser(8) instead.
...

CentOSでは、adduser にはシンボリックリンクが設定されており、どちらのコマンドでも useradd が実行されるようになっている。

# ls -l /usr/sbin/useradd
-rwxr-xr-x. 1 root root 143424 Aug 13  2020 /usr/sbin/useradd
# ls -l /usr/sbin/adduser 
lrwxrwxrwx. 1 root root 7 Aug 13  2020 /usr/sbin/adduser -> useradd

まとめ(再掲)

先頭でも記載したが、adduseruseradd では以下のような違いがあった。

  • どちらのコマンドも、ユーザを作成する機能自体は同じ。
  • adduser ではパスワードを対話形式で設定する必要がある。useradd では対話なしで、オプションで指定する必要がある。
  • Debian系のOSであるUbuntu ではadduserが推奨されている。CentOS ではどちらのコマンドでも useradd が実行される。

基本的にどちらのコマンドを利用しても良いものではあるが、よく使うOSのディストリビューションがあるのであればそちらのコマンドを覚えておくと良さそう。

*1:GECOSは、General Electric社が開発していたOSである、General Electric Comprehensive Operating Systemの略。当時はこのフィールドにログイン情報を記録していたが、現在は単なるコメントとして記録される

*2:UbuntuDebian系のOSである