5

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
root@106.186.120.20'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
AGamePlayer
  • 7,605
  • It looks like you have specified the new shell incorrectly. Did you enter the full path to zsh when you changed it? – D_Bye Jan 04 '15 at 12:37
  • 1
    @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! – AGamePlayer Jan 04 '15 at 13:36
  • @AwQiruiGuo chsh allows you to change a user's shell. If you can't use it, or can't get permissions to, try to edit /etc/passwd directly. You may need sudo at some point ;) – John WH Smith Jan 04 '15 at 13:49
  • 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. – AGamePlayer Jan 04 '15 at 13:56
  • Use sh -s /bin/bash, then fix the problem. – ctrl-alt-delor Jan 17 '17 at 18:16
  • re sudo on some systems being in group sudo will allow you to use sudo with full privileges. I would recommend sudo over su. – ctrl-alt-delor Jan 17 '17 at 18:19
  • Let this be a lesson for all about glancing around for and respecting any statements like "actually, you should [some seemingly unimportant nit-picky thing like 'always use absolute paths when setting the shell']". – mtraceur Jul 30 '22 at 14:40

3 Answers3

10

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
#
llua
  • 6,900
  • 3
    And you won't ever have to use it again, because you are going to use absolute paths to the login shells right? :} – llua Jan 07 '15 at 16:07
8

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.

4
  • 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

jimmij
  • 47,140
  • 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 – AGamePlayer Jan 04 '15 at 14:01
  • 1
    @AwQiruiGuo what does ls -l /usr/bin/zsh show? – jimmij Jan 04 '15 at 14:03
  • result: lrwxrwxrwx 1 root root 28 Dec 26 06:53 /usr/bin/zsh -> /etc/alternatives/zsh-usrbin – AGamePlayer Jan 04 '15 at 14:04
  • 1
    @AwQiruiGuo and ls -l /etc/alternatives/zsh-usrbin? – jimmij Jan 04 '15 at 14:05
  • 1
    What's the difference between su -s /usr/bin/zsh and su -s /usr/bin/zsh - ? – PM 2Ring Jan 04 '15 at 14:06
  • 1
    @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. – jimmij Jan 04 '15 at 14:08
  • 1
    @jimmij So would leaving out the final - have caused the error mentioned in the 1st comment? – PM 2Ring Jan 04 '15 at 14:14
  • 1
    @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. – jimmij Jan 04 '15 at 14:22
  • @jimmij ls -l /etc/alternatives/zsh-usrbin got -> lrwxrwxrwx 1 root root 9 Dec 26 06:53 /etc/alternatives/zsh-usrbin -> /bin/zsh4 – AGamePlayer Jan 04 '15 at 14:28
  • @jimmij su -s /usr/bin/zsh and su -s /usr/bin/zsh - got the exact same result. – AGamePlayer Jan 04 '15 at 14:28
  • 1
    @AwQiruiGuo and what about ls -l /bin/zsh4? – jimmij Jan 04 '15 at 14:32
  • @jimmij result: -rwxr-xr-x 1 root root 696880 Feb 28 2012 /bin/zsh4 – AGamePlayer Jan 04 '15 at 14:41
  • 1
    @AwQiruiGuo Try to pass /bin/zsh4 to su directly. Also what happens when you just run zsh, is it ok? – jimmij Jan 04 '15 at 14:46
  • @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 – AGamePlayer Jan 04 '15 at 14:50
  • 1
    @AwQiruiGuo Yes, but with -s option. Let us continue this discussion in chat. – jimmij Jan 04 '15 at 14:51