Ubuntu(Debian系)でユーザを作る際はuseraddではなくadduser


ubuntuでユーザを作成した際に、useraddを用いて作成したユーザでログインしたところホームディレクトリがないと怒られた

# useradd new_user
# su - new_user
No directory, logging in with HOME=/

どうもデフォルトではディレクトリを作成してくれないようである。man useradd をみるに、-m オプションを明示的に指定するか、CREATE_HOME=yes でなければ、ホームディレクトリは作成されないようだ

       -m, --create-home
           Create the user's home directory if it does not exist. The files and directories contained
           in the skeleton directory (which can be defined with the -k option) will be copied to the
           home directory.

           By default, if this option is not specified and CREATE_HOME is not enabled, no home
           directories are created.

そして、Debian系はデフォルトでは、CREATE_HOMEに関する設定がない

# useradd -D
GROUP=100
HOME=/home
INACTIVE=-1
EXPIRE=
SHELL=/bin/sh
SKEL=/etc/skel
CREATE_MAIL_SPOOL=no

また、useraddのDescriptionには次のように書いてある

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

調べてみると、adduser という似たような名前のコマンドがあり、こちらでユーザを作成するのが、Debianユーザの常識だそうである。たしかに、こちらでユーザを作成すると、ちゃんとホームディレクトリを作ってくれた。知らなかった、、

man adduserには次のように書いてある

DESCRIPTION
       adduser and addgroup add users and groups to the system according to command line  options  and
       configuration  information  in  /etc/adduser.conf.   They  are friendlier front ends to the low
       level tools like useradd, groupadd and usermod programs, by default choosing Debian policy con‐
       formant  UID  and  GID values, creating a home directory with skeletal configuration, running a
       custom script, and other features.  adduser and addgroup can be run in one of five modes:

みてみると、このコマンドはperlスクリプトであり、内部でuseraddを呼び出すフロントエンドとなっている。

# which adduser
/usr/sbin/adduser
# file /usr/sbin/adduser
/usr/sbin/adduser: a /usr/bin/perl script, ASCII text executable

以上、ユーザを作成するときはadduserを使いましょう!