19

I mistakenly entered

chsh -s /usr/bin 

instead of

chsh -s /bin/bash 

and now I can't log into a root shell, how do I start a bash shell as root manually ?

  • in what context did you do this. In a current terminal, for the users default shell? – jgr208 Mar 28 '16 at 13:17
  • dont you have users in the sudo group? – Rui F Ribeiro Mar 28 '16 at 13:26
  • 3
    does sudo vipw work? – Rui F Ribeiro Mar 28 '16 at 13:30
  • 1
    @RuiFRibeiro yes it did thanks, I wasn't aware I could change the default root shell in that file – ChiseledAbs Mar 28 '16 at 13:32
  • 1
    Not necessary in this case, but when you have inadvertently create a catastrophe involving root, you can usually take the machine to single user mode, fix the problem, and then return to multi-user mode. – boardrider Mar 30 '16 at 8:23
29

While root does not have access, a user in the sudo group can still run privileged commands - it seems the error is not in sudo, but elsewhere in the sudo chshcommand (e.g. chsh error).

As such your sudo is apparently working.

The passwd file can be edited with:

sudo vipw

And the root shell changed manually.

(first line of /etc/passwd usually)

root:x:0:0:root:/root:/bin/bash

Fom man vipw

The vipw and vigr commands edits the files /etc/passwd and /etc/group, respectively. With the -s flag, they will edit the shadow versions of those files, /etc/shadow and /etc/gshadow, respectively. The programs will set the appropriate locks to prevent file corruption.

  • 3
    I never knew about vipw and vigr, thank you! I always used sudo vim /etc/passwd – cat Mar 28 '16 at 15:54
  • 3
    You might be interested in visudo sudo.ws/man/1.8.15/visudo.man.html too, @tac – Rui F Ribeiro Mar 28 '16 at 16:05
  • 3
    Or sudo -e, that performs this locking-editing feature for any edit. – kojiro Mar 28 '16 at 21:54
16

Another option, assuming you have access to another account, is to manually override the default shell by using su --shell=/bin/bash:

-s, --shell=SHELL
run SHELL if /etc/shells allows it

The primary advantage of this is that it only requires access to another account, not another privileged one.

8

For security reasons, logins whether textual or graphical and utilities like su and sudo that allow you to run commands as a different user all run those command through the target user's shell. If the target user's shell is non-functional, well, you've seen the results :-(

Unless you happen to have a still-functional root shell running somewhere or something like a setuid-root binary or something that allows you to bypass this, rebooting and correcting the problem using a rescue system or live image is probably your best bet.

Boot the rescue system, mount your real system's root filesystem somewhere, say, /mnt/foo, and edit /mnt/foo/etc/passwd to fix the shell. Save, unmount, and you're done.

  • curious, what happens if you do this and the /etc/passwd file is in a hash instead of plain text? – jgr208 Mar 28 '16 at 13:18
  • 1
    /etc/passwd is not plaintext? I haven't seen that in at least, like, a decade. Is that still a thing on some systems? Anyway, I guess chroot /mnt/foo chsh -s /bin/bash might do the trick in that case? – Celada Mar 28 '16 at 13:21
  • I have never had the problem described, however I work with locked down systems that need to have /etc/passwd not in plain text as to avoid hackers and the such compromising the system even more if they gain access. Hmm that might work, lets just say I hope i never have to try and see if it works actually. – jgr208 Mar 28 '16 at 13:24
  • I was able to edit /etc/passwd with sudo from a user account, I wasn't aware that the shell was defined in this file, thanks – ChiseledAbs Mar 28 '16 at 13:34
  • @ChiseledAbs oh, you were? Good for you, glad you solved your problem. I didn't think it would let you. – Celada Mar 28 '16 at 13:36
6

Add init=/bin/bash to your kernel command line ( if booting with grub, press e to edit the boot entry ), and you will have a bash shell running as root without even having to supply a password. Your root filesystem likely will still be mounted read only though, so you will need to remount it first, then you can change the shell back with chsh.

4

sudo usermod -s /bin/bash jdoe

will change the shell of jdoe to bash. You can then sudo egrep jdoe /etc/passwd to verify.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.