Root shell mistakenly changed. How can I change it back to a valid shell?

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.


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.


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.

Tags:

Shell

Bash