How could I reach root after switching from bash to zsh?
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 (r
ead and ex
ecute). -
If path was not correct then check where your
zsh
executable is placed withtype 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
Related videos on Youtube
Tommy
Something for nothing. Bite me if you can score 9+ in a CPS Test.
Updated on September 18, 2022Comments
-
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:
- If I enter the correct password, it will tell me that "Cannot execute zsh: No such file or directory"
- 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
isroot:x:0:0:root:/root:zsh
-
Jazz over 9 yearsIt looks like you have specified the new shell incorrectly. Did you enter the full path to zsh when you changed it?
-
Tommy over 9 years@jimmij It's
root:x:0:0:root:/root:zsh
so it should beroot:x:0:0:root:/root:/bin/zsh
instead, right? How could I fix this? Thanks! -
Tommy over 9 yearsJust entered
sudo vim /etc/passwd
to edit it, before I was asked to enter a password, I gotsudo: 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 over 7 yearsUse
sh -s /bin/bash
, then fix the problem. -
ctrl-alt-delor over 7 yearsre
sudo
on some systems being in groupsudo
will allow you to use sudo with full privileges. I would recommendsudo
oversu
.
-
Tommy over 9 yearsI checked the file passwd and noticed it's
root:x:0:0:root:/root:zsh
and after I checked withtype zsh
I gotzsh is /usr/bin/zsh
. So I usesu -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 aCannot execute zsh: No such file or directory
error -
jimmij over 9 years@AwQiruiGuo what does
ls -l /usr/bin/zsh
show? -
Tommy over 9 yearsresult:
lrwxrwxrwx 1 root root 28 Dec 26 06:53 /usr/bin/zsh -> /etc/alternatives/zsh-usrbin
-
jimmij over 9 years@AwQiruiGuo and
ls -l /etc/alternatives/zsh-usrbin
? -
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 exampleecho $PATH
with and without-
argument. -
PM 2Ring over 9 years@jimmij So would leaving out the final
-
have caused the error mentioned in the 1st comment? -
jimmij over 9 years@PM2Ring Very unlikely.
su
without-
should work fine, it just leaves (do not set) some variables, perhaps better example isexport LC_ALL=C
and then comparesu
orsu -
to another user. OP on the other hand seems to have some problem with (broken?) link or file permission. -
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 over 9 years@jimmij
su -s /usr/bin/zsh
andsu -s /usr/bin/zsh -
got the exact same result. -
jimmij over 9 years@AwQiruiGuo and what about
ls -l /bin/zsh4
? -
Tommy over 9 years@jimmij result:
-rwxr-xr-x 1 root root 696880 Feb 28 2012 /bin/zsh4
-
Tommy over 9 years@jimmij did you mean
su /bin/zsh4
? It gotNo passwd entry for user '/bin/zsh4'
and I wasn't promoted to enter any password -
llua over 9 yearsAnd you won't ever have to use it again, because you are going to use absolute paths to the login shells right? :}