Linux: Switch to nologin User

Objective: Switch and execute commands as a particular user with a nologin shell on Linux.

A nologin user entry might look something like this in the /etc/passwd file.

$ cat /etc/passwd | grep www-data
www-data:x:33:33:www-data:/var/www:/usr/sbin/nologin

The above example is for the www-data user. Notice the /usr/sbin/nologin shell assigned to this user.

If you try to su to this user from root, you will most probably get the following error message.

# su - www-data
This account is currently not available.

To switch to the nologin user account using su as root, you can use the following syntax.

# su -s /bin/bash www-data

$ whoami
www-data

To switch from a non-root user, append sudo to the above command.

$ whoami
ibrahim

$ sudo su -s /bin/bash www-data

$ whoami
www-data

To run a script as a nologin user, use the following command syntax.

# su -s /bin/bash -c /path/to/script www-data 

If you are want to use sudo and want to open a bash shell for that nologin user, use the following command syntax.

$ sudo -u www-data /bin/bash

To run a script using sudo, use the following command syntax.

$ sudo -u www-data /path/to/script

ibrahim = { interested_in(unix, linux, android, open_source, reverse_engineering); coding(c, shell, php, python, java, javascript, nodejs, react); plays_on(xbox, ps4); linux_desktop_user(true); }

« Previous Article