晴耕雨読

working in the fields on fine days and reading books on rainy days

ログインシェルを /sbin/nologin にしたユーザにログインする

「usermod -s /sbin/nologin ユーザ名」を実行すると対象ユーザへの ssh や su によるログインができなくなります。

~]# cat /etc/passwd | grep postgres
postgres:x:26:26:PostgreSQL Server:/var/lib/pgsql:/bin/bash

~]# usermod -s /sbin/nologin postgres

~]# cat /etc/passwd | grep postgres
postgres:x:26:26:PostgreSQL Server:/var/lib/pgsql:/sbin/nologin

~]# su - postgres
This account is currently not available

ただし、場合によってはログインを禁止したユーザでコマンドを実行したい場合もあります。 そのときは su コマンドにオプション –shell=/bin/bash を追加して実行することでログインすることができます。

~]# su - postgres --shell=/bin/bash
-bash-4.1$ id
uid=26(postgres) gid=26(postgres) groups=26(postgres)

参考文献