Sudo su vs su linux

24

2

If I enter in the password to su when prompted, it doesnt work while sudo su does. Also, when a command is executed using sudo (command), does sudo automatically execute it by root by default (since the man page says sudo, sudoedit - execute a command as another user

michaelxu@michaelxu-server:~$ su
Password:
su: Authentication failure
michaelxu@michaelxu-server:~$ sudo su
[sudo] password for michaelxu:
root@michaelxu-server:/home/michaelxu# exit

agz

Posted 2013-04-25T02:07:19.303

Reputation: 6 820

Answers

41

That's because su asks for password of the user you're changing into (which by default is root) while sudo asks for your own user account's password and checks if you are allowed to run the command.

When you run sudo su you are asking sudo to run the command su as root, which gives you the root shell. If you are using only su you will have to know the target user's password to have access.

Using sudo without parameters implies that you want to use root. If you want to execute the command as another user, try sudo -u <username> <command>

Renan

Posted 2013-04-25T02:07:19.303

Reputation: 551

7One other important bit of information to understand the related command sudo su otheruser is that su allows the root to switch into any users without entering their password, while non-root users that runs su have to input the target user's password. sudo su therefore runs su as root, which allows you to get a shell for another user without knowing their password (as long as it's allowed by sudoers). – Lie Ryan – 2013-04-25T09:06:57.090

So what would my root password be for the "su" command? How come it isnt the password I set in the beginning of the installation? – agz – 2013-04-25T23:23:07.287

Because your distribution probably didn't set it. You can set a root password running sudo passwd (first provide your user password, then choose a new root password). – Renan – 2013-04-26T00:41:54.093

So at the moment there is no password?...so basically root is just an account that sudoers can use to make changes to root files? – agz – 2013-04-26T03:18:54.680

not just that, there are many other services that have to run as root (check ps aux). – Renan – 2013-04-26T06:15:47.867

17

su stands for switch (or substitute) user. With no user name given, it defaults to switching to root. It prompts for the password of the user you're switching to.

sudo is used to run a command as root (i.e., with root permissions), or as other users. It prompts for your password, and checks that you're allowed to use sudo.

When you type su, you're asking to switch to root, and the password that's requested is the root password. When you type sudo su, you're asking to execute the command to switch to root as root, so the password that's asked for is yours.

ForeverWintr

Posted 2013-04-25T02:07:19.303

Reputation: 859

1

...and to fix the problem of not being able to use 'su' alone, sudo to a root shell, and set a password for root, with 'chpass'.

Nevin Williams

Posted 2013-04-25T02:07:19.303

Reputation: 3 725