Linuxにおいて表題の通り。Dockerfileを読んでいて、groupadd/useraddのこのオプションは何だろう?と気になったところから、いくつか試したメモ。
■ 環境
- CentOS 6.4
- AmazonLinux
■ groupadd -r
groupaddコマンドに”-r“オプションがあるということを知った。”-g“オプションしか使用したことがなかったので、manで調べてみると、下記のように記載されていた。
-r, --system
Create a system group.
The numeric identifiers of new system groups are choosen in the SYS_GID_MIN-SYS_GID_MAX range, defined in login.defs, instead of GID_MIN-GID_MAX.
“system group“にどこかピンとこないのだが、『システムを稼動させる為に存在するグループ』という認識をすることにした。ここで言う『システム』は例えばMongoDBであったりZooKeeperであったり。サービス(デーモン)として稼動させるプログラムを動かす為だけに存在させる、という認識を持った(あくまで自身がそう思っただけである)。
試しに作成してみたところ下記のように作成された。
$ sudo groupadd -r withsin
作成されたグループのGIDは498となっていた。通常のグループを作成した時は500番以降が使われるはずだが、499から降順に作成されていくようである。
■ useradd -r
useraddコマンドにも”-r“オプションが存在する。こちらもmanには下記のように記載されていた。
-r, --system
Create a system account.
System users will be created with no aging information in /etc/shadow, and their numeric identifiers are choosen in the SYS_UID_MIN-SYS_UID_MAX range, defined in /etc/login.defs, instead of UID_MIN-UID_MAX (and their GID counterparts for the creation of groups).
Note that useradd will not create a home directory for such an user, regardless of the default setting in /etc/login.defs (CREATE_HOME). You have to specify the -m options if you want a home directory for a system account to be created.
やはり”system account“にはピンと来ないが、groupaddの時と同様に『システムを稼動させる為に存在するユーザ』という認識をした。つまりこのユーザにはログインさせたくない。よって作成時には下記のオプションも同時に使用することにする。
$ sudo useradd -g withsin -s /sbin/nologin -r withsin
“-r“オプションを付けたユーザは、デフォルト設定ではホームディレクトリが作成されないようである。”-M”オプションを指定しても良いが、”system account“にはデフォルトで作成されないとの事なので、二重に指定しなくても良いであろう。上記のコマンドから下記のようなアカウントが作成された。
$ sudo id withsin
uid=495(withsin) gid=497(withsin) groups=497(withsin)
$
$ grep withsin /etc/passwd
withsin:x:495:497::/home/withsin:/sbin/nologin
$
$ grep withsin /etc/group
withsin:x:497:
$
ちなみにログインさせたくない時のshellに何を指定するか、では自身は普段は”/sbin/nologin“を使用していたが、”/bin/false“という手段もあるらしい。
知らないと危険な false と nologin の挙動の違い #Linux #BSD
http://blog.archinwaterworks.com/2014/06/false_or_nologin/
上記に読む限り、やはり”/sbin/nologin“を使用する方が良さそうである。
以上。