$cat /etc/passwd |grep -i root
root:x:0:0:root:/root:/bin/bash
$sudo cat /etc/shadow |grep -i root
root:!:17179:0:99999:7:::
In the second field of shadow file, !
means root user cannot login but why can I login to root user by sudo su
?
Why can't I login to root user by su root
or su -
?
An
!
in theshadow
entry's encrypted password field means that no password can authenticate against it. Fromman shadow
:As the manual says, this does not mean that you can't login as root. It just means that you can't login as root using a password for the root account. (You can login as root via SSH using SSH keys, for example, if you had configured it earlier, even if the account is locked.)
sudo
normally authenticates with your password, not root's. This can be changed by setting one oftargetpw
,rootpw
orrunaspw
insudoers
. If you set one these options, and try to use a password when the password is locked, that will fail.Now let's look at the commands accordingly:
sudo su
:sudo
runs the commandsu
(substitute user) with root privileges so even if the/etc/shadow
says or hasroot:!:17179:0:99999:7:::
it will still run commands with root privileges.su -
orsu root
:/etc/shadow
file can not log in so using these commands will not work. If you want them to work then the root account must be unlocked by giving it a password.Summary:
su -
0rsu root
switches to user root, does not exist so it can't happen, butsudo su
runs switch command withroot
privileges, so in this case it will go if you are in thesudo
group. You're not actually logging in as root in this case, just acting as root so it will go.Source: What is the difference between 'su -' and 'su root'?