LoginSignup
9
8

More than 5 years have passed since last update.

suできないユーザーに切り替える方法

Last updated at Posted at 2017-03-21

はじめに

apacheユーザー権限で動作するスクリプトの検証をしたい場合に、su - apacheを実行しても、nologin設定がされているため、ユーザー切り替えをすることが出来ません。

[root@localhost ~]# su - apache
Last login: Tue Mar 21 16:02:54 UTC 2017 on pts/0
This account is currently not available.

このようなユーザーにsuでユーザー切り替えする方法をまとめます。

現状

そもそもsuできない理由は、/sbin/nologinとログイン禁止シェルが設定されており、この場合はメッセージが表示されて終了となります。

[root@localhost ~]# cat /etc/passwd | grep apache
apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin

vagrantのようなsuできるユーザーの場合ですと、/bin/bashとbashが指定されています。

[root@localhost ~]# cat /etc/passwd | grep vagrant
vagrant:x:1000:1000:vagrant:/home/vagrant:/bin/bash

手順

su には -s, --shell <shell> run shell if /etc/shells allows itというオプションがありシェルを指定することができます。現状で調べたとおりsuできないのはログインできないシェルが指定されているのが原因でしたので、bashを指定することでログインできるようになります。

[root@localhost ~]# su -s /bin/sh apache
sh-4.2$ whoami
apache
9
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
8