Edit /etc/passwd file from grub
Solution 1
You cannot edit files from the Grub prompt (Grub can only read files, it doesn't have any write support). You need to boot Linux¹.
If you can't log in normally, you can completely bypass the normal boot process and start just a kernel and a shell. At the Grub prompt, press e to edit the boot sequence, add init=/bin/bash
to the end of the linux=…
line, and press Ctrl+X to boot. You'll get a root shell and nothing else. Run
mount -t proc proc /proc
mount -o remount,rw /
Now run chsh -s /bin/zsh root
to repair your user database.
Next, reboot with Alt+SysRq+U followed by Alt+SysRq+B, or boot normally with
mount -o remount,ro /
umount /proc
exec /sbin/init
Tip: run chsh
without a user argument. That way, it validates the shell.
Tip: when you're changing some configuration, always keep a shell open and test the configuration. If you made a mistake, you'll still have a command line to repair it.
Tip: you can create a toor
account with UID 0 (so it's still root), with the same password as root, and where the shell remains /bin/sh
, so that you keep another way to log in if things break. Better yet, install a statically linked shell (busybox-static
or zsh-static
) and use that as toor's login shell.
¹ Or you can use guestfs if the host is Linux.
Solution 2
You should remove from the password file the which
word...
It should be something like :
root:x:0:0:root:/root:/bin/zsh
Where /bin/zsh is the path to the Z shell binary.
In the current config, the login process is trying to execute which zsh
and that command is failing.
Also, edit the file as root, with sudo vim /etc/passwd
for example because it is a file which is writable only by root.
If you can't sudo
or su
, then boot into recovery mode (choose Advanced option for Ubuntu in GRUB boot loader, then choose Ubuntu...(recovery mode), then choose root/Drop to root shell prompt). After this, remount the /
filesystem as read-write (mount -o remount,rw /
) and then you can edit the content of the /etc/passwd
file. Tested this on Ubuntu 16.04.
Related videos on Youtube
iyerrama29
Updated on September 18, 2022Comments
-
iyerrama29 over 1 year
I tried installing zsh on ubuntu ( 14.04). When I switched to zsh, I was unable to go into super user mode.
If I do a su it asks for passwd and on entering the correct password, it says:
Cannot execute which zsh: No such file or directory
But when I enter an incorrect password and press enter 4-5 times, it shows nothing.
On checking my
/etc/passwd
file, it showed:root:x:0:0:root:/root:which zsh
I tried to edit the file and save but it shows
Unable to save /etc/passwd
I have tried to change the runlevel to 1 via grub, but it results in an error:
APCI PCC probe failed
I am running Ubuntu on VirtualBox.
-
Sergiy Kolodyazhnyy almost 8 yearsUbuntu grub has option to go into recovery. Also, you can change an option to boot into bash directly. Can't recall which, though
-
iyerrama29 almost 8 yearseven during recovery; setting root: Drop out to root shell asks for passwd, which when entered correct gives: which zsh: No such file or directory
-
-
iyerrama29 almost 8 yearsHi there, I tried editing the file, but as I mentioned above, it shows I cannot edit the file(unless I am the root - and also I am unable to become the root because the su command doesn't work): Unable to save /etc/passwd
-
Sergiy Kolodyazhnyy almost 8 yearsFilesystem is mounted read only. Try remount as read write
-
iyerrama29 almost 8 yearsI tried going to /etc and doing mount -o remount rw and got mount: only root can do that
-
iyerrama29 almost 8 yearsAs I mentioned, I cannot do a sudo or a su, when I do sudo it shows: sudo: /usr/bin/sudo must be owned by uid 0 and have setuid bit set and on su it asks for passwd but when entered correct password it gives ** Cannot execute which zsh: No such file or directory**
-
terdon almost 8 years-1, I'm afraid everything you suggest is wrong: 1) The OP can't use
sudo
as explained in their question; 2) You mean/etc/shells
not/etc/shell
; 3)$SHELL
doesn't show the currently running shell, it shows the default for the user. In any case, how is that relevant? -
magor almost 8 yearsboot into recovery mode (choose Advanced option for Ubuntu in GRUB boot loader, then choose Ubuntu...(recovery mode), then choose root/Drop to root shell prompt). After this, remount the
/
filesystem as read-write (mount -o remount,rw /
) and then you can edit the content of the/etc/passwd
. Tested this on Ubuntu 16.04. -
iyerrama29 almost 8 yearsI booted into the recovery mode and chose: root: Drop out to root shell prompt, it again gives: Give the root password for maintainence or press Ctrl+D to continue, on giving the right password, it shows which zsh: No such file or Directory
-
magor almost 8 yearsin this case you should try booting an Ubuntu install disk and drop to shell (or use the Try Ubuntu option) and then try to mount the drive and change the file
-
AReddy almost 8 years
/etc/shell
will not give you the required output. -
mstr almost 6 yearsThanks a lot for the great trick with changing the init! It helped me in a situation where I had to edit fstab to allow normal booting :-)