How could I reach root after switching from bash to zsh?

17,616

Solution 1

try: cd /usr/bin; su. Since you didn't give an absolute path to zsh. su is checking the PWD. changing to the directory zsh exists in will work on some systems. an example:

% su
Password:
su: zsh: No such file or directory
% cd /usr/local/bin
% su
Password:
# print $OSTYPE
freebsd10.0
#

Solution 2

Your entry in /etc/passwd is

root:x:0:0:root:/root:zsh

This is an invalid entry: the shell must be a full path to an executable, the login program does not perform $PATH lookup.

You won't be able to log into the root account by normal means. You can use sudo to invoke a command, e.g. sudo vipw, if your account has sudo permissions. This is the only common method to bypass the login shell setting in the target account.

If you aren't a sudoer, you've probably locked yourself out of the root account. You'll need console access to repair the system. Boot in single user mode (see How do I run a command as the system administrator (root)) and edit /etc/passwd to contain

root:x:0:0:root:/root:/bin/zsh

(with the correct path for zsh on your system).

Some administrators set up a toor account with UID 0 but a different shell (typically a statically linked binary such as sash, to allow root to log in even in case of a misconfiguration such as this one (the most common misconfiguration is a broken shared library in the normal shell).

To avoid such issues in the future, use the chsh command to change a user's shell, rather than editing /etc/passwd directly. And if you must edit /etc/passwd or some other file that is involved in gaining root access, keep a root shell open in a terminal and don't close it until you've verified that you can still log in as root.

Solution 3

  • Check your current shell with

    grep '^root:' /etc/passwd
    

    you should see at the end of the line full path to shell used by root user, like /bin/zsh. Then check if the path is not misspelled, file exists and has proper permissions set (read and execute).

  • If path was not correct then check where your zsh executable is placed with

    type zsh
    
  • After that su to root using the correct shell path, e.g. in case of /bin/zsh:

    su -s /bin/zsh -
    
  • Lastly run chsh to change default shell to /bin/zsh

Share:
17,616

Related videos on Youtube

Tommy
Author by

Tommy

Something for nothing. Bite me if you can score 9+ in a CPS Test.

Updated on September 18, 2022

Comments

  • Tommy
    Tommy over 1 year

    It's very weird that after switching to zsh from bash, I can't access root.

    I normally use 'su' to login as root after I login as a normal user (username is normalusername) with less privileges. And it was always nice. But after switching root shell from bash to zsh, when I try to login via su, I got:

    normalusername@(none):~$ su
    Password: (enter the correct password)
    Cannot execute zsh: No such file or directory
    

    When I access root directly via ssh from my Mac, I got:

    localhost:~ myname$ ssh -lroot 106.186.120.20
    [email protected]'s password: (enter the correct/incorrect password)
    Permission denied, please try again.
    

    No matter whether I entered the correct password, it comes again and again.

    So I intentionally entered a wrong password with "su" from a normal user, I got:

    normalusername@(none):~$ su
    Password: (entered a wrong password and pressed enter)
    (pressed enter)
    su: Authentication failure
    

    After entering the incorrect password I didn't see anything, I typed enter twice and then got the su: Authentication failure result.

    I tried for many times and the conclusion is:

    1. If I enter the correct password, it will tell me that "Cannot execute zsh: No such file or directory"
    2. If I enter the incorrect password, it will not showing up anything until I hit enter for 2-6 times.

    It seems to be an indication that I didn't just forget the "correct password". But how can I access root anyway?

    The entry in /etc/passwd is

    root:x:0:0:root:/root:zsh
    
    • Jazz
      Jazz over 9 years
      It looks like you have specified the new shell incorrectly. Did you enter the full path to zsh when you changed it?
    • Tommy
      Tommy over 9 years
      @jimmij It's root:x:0:0:root:/root:zsh so it should be root:x:0:0:root:/root:/bin/zsh instead, right? How could I fix this? Thanks!
    • Tommy
      Tommy over 9 years
      Just entered sudo vim /etc/passwd to edit it, before I was asked to enter a password, I got sudo: unable to resolve host (none) then I was prompted to enter the password for my current user. I entered and then I got this: normalusername is not in the sudoers file. This incident will be reported.
    • ctrl-alt-delor
      ctrl-alt-delor over 7 years
      Use sh -s /bin/bash, then fix the problem.
    • ctrl-alt-delor
      ctrl-alt-delor over 7 years
      re sudo on some systems being in group sudo will allow you to use sudo with full privileges. I would recommend sudo over su.
  • Tommy
    Tommy over 9 years
    I checked the file passwd and noticed it's root:x:0:0:root:/root:zsh and after I checked with type zsh I got zsh is /usr/bin/zsh. So I use su -s /usr/bin/zsh but the same trouble happened. I was asked to enter the password and If I enter the correct one, I got a Cannot execute zsh: No such file or directory error
  • jimmij
    jimmij over 9 years
    @AwQiruiGuo what does ls -l /usr/bin/zsh show?
  • Tommy
    Tommy over 9 years
    result: lrwxrwxrwx 1 root root 28 Dec 26 06:53 /usr/bin/zsh -> /etc/alternatives/zsh-usrbin
  • jimmij
    jimmij over 9 years
    @AwQiruiGuo and ls -l /etc/alternatives/zsh-usrbin?
  • jimmij
    jimmij over 9 years
    @PM2Ring basically it sets all environment variables for login user, from manual: argument - may be used to provide an environment similar to what the user would expect had the user logged in directly. Compare for example echo $PATH with and without - argument.
  • PM 2Ring
    PM 2Ring over 9 years
    @jimmij So would leaving out the final - have caused the error mentioned in the 1st comment?
  • jimmij
    jimmij over 9 years
    @PM2Ring Very unlikely. su without - should work fine, it just leaves (do not set) some variables, perhaps better example is export LC_ALL=C and then compare su or su - to another user. OP on the other hand seems to have some problem with (broken?) link or file permission.
  • Tommy
    Tommy over 9 years
    @jimmij ls -l /etc/alternatives/zsh-usrbin got -> lrwxrwxrwx 1 root root 9 Dec 26 06:53 /etc/alternatives/zsh-usrbin -> /bin/zsh4
  • Tommy
    Tommy over 9 years
    @jimmij su -s /usr/bin/zsh and su -s /usr/bin/zsh - got the exact same result.
  • jimmij
    jimmij over 9 years
    @AwQiruiGuo and what about ls -l /bin/zsh4?
  • Tommy
    Tommy over 9 years
    @jimmij result: -rwxr-xr-x 1 root root 696880 Feb 28 2012 /bin/zsh4
  • Tommy
    Tommy over 9 years
    @jimmij did you mean su /bin/zsh4? It got No passwd entry for user '/bin/zsh4' and I wasn't promoted to enter any password
  • llua
    llua over 9 years
    And you won't ever have to use it again, because you are going to use absolute paths to the login shells right? :}