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.
1 2 |
$ 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.
1 2 |
# 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.
1 2 3 4 |
# su -s /bin/bash www-data $ whoami www-data |
To switch from a non-root user, append sudo
to the above command.
1 2 3 4 5 6 7 |
$ 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.
1 |
# 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.
1 |
$ sudo -u www-data /bin/bash |
To run a script using sudo
, use the following command syntax.
1 |
$ sudo -u www-data /path/to/script |