🐧

【Linux】ユーザ作成

2025/03/10に公開

ユーザ作成

useradd testuser

useraddのデフォルト値

オプションを明示的に指定しない場合、以下のデフォルト値が設定される。

[root@localhost ~]# useradd -D
GROUP=100
HOME=/home
INACTIVE=-1
EXPIRE=
SHELL=/bin/bash
SKEL=/etc/skel
CREATE_MAIL_SPOOL=yes
[root@localhost ~]#

UID、GIDを同じ値で作成

useradd testuser -u 3000 -U

追加されたユーザを確認

[root@localhost ~]# tail -n 1 /etc/passwd
testuser:x:3000:3000::/home/testuser:/bin/bash
[root@localhost ~]#
[root@localhost ~]# tail -n 1 /etc/group
testuser:x:3000:
[root@localhost ~]#

ログインさせたくない場合

-sオプションでログインシェルを/sbin/nologinとして作成

useradd testuser2 -u 3001 -U -s /sbin/nologin
[root@localhost ~]# tail -n 1 /etc/passwd
testuser2:x:3001:3001::/home/testuser2:/sbin/nologin
[root@localhost ~]# tail -n 1 /etc/group
testuser2:x:3001:
[root@localhost ~]#

ログイン不可ユーザでログインした時の挙動

/etc/nologin.txtの内容を出力してログインを拒否する。
nologin.txtはデフォルトでないため作成する。

[root@localhost ~]# cat /etc/nologin.txt
This user can not log in.
[root@localhost ~]#
[root@localhost ~]# su testuser2
This user can not log in.
[root@localhost ~]#

/etc/nologin、/etc/nologin.txt

/etc/nologin
デフォルトで作成されず、touchなどで作成すると一般ユーザでのログインが不可となるファイル。ただしrootログインおよびrootログイン後の一般ユーザへのスイッチは可能。

/etc/nologin.txt
デフォルトで作成されず、ログイン不可ユーザでのログイン時に出力するメッセージを記載するファイル。

Discussion