8

Maybe I'm not using the right terms so allow me to explain myself using an example. Connecting over SSH as user "centos" and executing cat /etc/passwd in my Centos7 machine I get:

centos:x:1000:1000:Cloud User:/home/centos:/bin/bash
www:x:1001:1001::/home/www:/sbin/nologin

Now, if try sudo su www - I get: This account is currently not available.

That according to my limited knowledge is due to the nologin part. What I want to do is to be able to switch to the www user in the SSH session without (if possible) giving the www user the possibility to access directly the server over SSH, like when the root user tries to SSH and get an error telling that you are supposed to log in as centos and then change to root if needed.

What will be the steps to achieve the desired behavior?

Shadur
  • 1,337
  • 1
  • 11
  • 20
DomingoSL
  • 365
  • 1
  • 4
  • 13
  • Related: https://unix.stackexchange.com/q/358740/147970 – cg909 Dec 09 '21 at 23:31
  • 3
    "shell access" kind of implies that user being able to log in directly. Sure, you could block them from SSH specifically, but still allow password logins on a console terminal, serial port, or other place where you could run a getty daemon. So that answers your question title, rather than what you're actually asking (about starting a shell as another user that can't login directly.) – Peter Cordes Dec 10 '21 at 14:05
  • I'll grant deman this, the title of the question is poorly worded compared to what the asker is actually trying to achieve per the text. Fixing. – Shadur Dec 11 '21 at 09:14

4 Answers4

36

Just run a shell under the user.

sudo -u www bash
Gerald Schneider
  • 23,274
  • 8
  • 57
  • 89
17

The su command takes a shell option (-s), so you can specify which shell to use, allowing you to become the user.

sudo su www -s /bin/bash
yoozer8
  • 322
  • 2
  • 12
3

The command syntax you're looking for is sudo -u www -s

This will drop you into a shell as the www user even if they don't have a shell normally.

Shadur
  • 1,337
  • 1
  • 11
  • 20
-5

Yes, you need to alter /etc/passwd - set a valid shell for this user. In order to run commands as this user, sudo -u www might help.