TL;DR
/etc/groupのuser_listはあるユーザのSupplementary groupが変更されるときに更新される。
useraddやusermodだと-G, --groupsオプションを指定しない限り更新されない。
気になった経緯
「group01をPrimary groupにもつユーザを複数作る」という要件に対してAnsibleで作成したときと手動で作成したときで/etc/groupの内容が違う、という問い合わせが発端。
手動で作成した時のコマンド群は以下の通り。
groupadd -g 2000 group01
useradd -u 2000 -g 2000 user01
useradd -u 2001 -g 2000 user02
useradd -u 2002 -g 2000 user03
(varsの取り方などがやや特殊な使い方をしているため細部は異なるが)実行したplaybookの要点を整理すると以下の通り。
- hosts: all
tasks:
- name: "Add group01"
group:
name: group01
gid : 2000
- name: "Add users in group01"
user:
name: "{{ item.name }}"
uid : "{{ item.uid }}"
group : group01
groups: group01
with_items:
- { name: user01, uid: 2000 }
- { name: user02, uid: 2001 }
- { name: user03, uid: 2002 }
上記をそれぞれ実行したとき/etc/groupの内容が異なってくる。
group01:x:2000:
group01:x:2000:user01,user02,user03
結論としては手動で-G, --groupsを指定していないのにAnsibleでgroupsパラメータを有効にしているのが原因。
idコマンド実行時の結果は変わらないので実害はないはずだが、個人的には/etc/groupだけ見れば「あるグループに所属しているユーザ一覧」を出せると思っていたのでPrimary groupとして所属しているユーザが/etc/groupに反映されていないのは少し気持ち悪い。
man groupでもuser_listについてPrimary/Supplementaryに関する言及は特になかったので厳密な取り扱いはしないのかな。
The /etc/group file is a text file that defines the groups on the system. There is one entry per line, with the following format:
group_name:password:GID:user_list
The fields are as follows:
group_name the name of the group.
password the (encrypted) group password. If this field is empty, no password is needed.
GID the numeric group ID.
user_list a list of the usernames that are members of this group, separated by commas.
結局、権限に関する処理を実行する際に「ユーザ→グループ」の参照ができれば「グループ→ユーザ」はあんまり需要がないのだろうな、と。
「あるグループに所属するユーザ全員に対する処理」はユーザ一覧総ナメして所属の有無確認すれば実現できるしなぁ。
蛇足
-
useradd.cでは/etc/groupを更新する処理に入るのは
--G, --groupsが指定されたときのみ。 -
user moduleでは
groupとgroupsでコマンドへの反映処理はきっちり分けている。