I already read it from manual but I can't see difference..
su
- change user ID or become superuser
sudo -s [command]
The -s
(shell) option runs the shell specified by the SHELL environment variable if it is set or the shell as specified in passwd(5). If a
command is specified, it is passed to the shell for execution. Otherwise, an interactive shell is executed.
sudo -i
disappear description in manual
The main difference between these commands is in the way they restrict access to their functions.
su
(which means "substitute user" or "switch user") - does exactly that, it starts another shell instance with privileges of the target user. To ensure you have the rights to do that, it asks you for the password of the target user. So, to become root, you need to know root password. If there are several users on your machine who need to run commands as root, they all need to know root password - note that it'll be the same password. If you need to revoke admin permissions from one of the users, you need to change root password and tell it only to those people who need to keep access - messy.sudo
(hmm... what's the mnemonic? Super-User-DO?) is completely different. It uses a config file (/etc/sudoers) which lists which users have rights to specific actions (run commands as root, etc.) When invoked, it asks for the password of the user who started it - to ensure the person at the terminal is really the same "joe" who's listed in/etc/sudoers
. To revoke admin privileges from a person, you just need to edit the config file (or remove the user from a group which is listed in that config). This results in much cleaner management of privileges.As a result of this, in many Debian-based systems
root
user has no password set - i.e. it's not possible to login as root directly.Also,
/etc/sudoers
allows to specify some additional options - i.e. user X is only able to run program Y etc.The often-used
sudo su
combination works as follows: firstsudo
asks you for your password, and, if you're allowed to do so, invokes the next command (su
) as a super-user. Becausesu
is invoked byroot
, it does not require you to enter the target user's password. So,sudo su
allows you to open a shell as another user (including root), if you're allowed super-user access by the/etc/sudoers
file.sudo
lets you run commands in your own user account with root privileges.su
lets you switch user so that you're actually logged in as root.sudo -s
runs a shell with root privileges.sudo -i
also acquires the root user's environment.To see the difference between
su
andsudo -s
, docd ~
and thenpwd
after each of them. In the first case, you'll be in root's home directory, because you're root. In the second case, you'll be in your own home directory, because you're yourself with root privileges.There's more discussion of this exact question here.
This answer is a dupe of my answer on a dupe of this question, put here on the canonical answer so that people can find it!
The major difference between
sudo -i
andsudo -s
is:sudo -i
gives you the root environment, i.e. your~/.bashrc
is ignored.sudo -s
gives you the user's environment, so your~/.bashrc
is respected.Here is an example, you can see that I have an application
lsl
in my~/.bin/
directory which is accessible viasudo -s
but not accessible withsudo -i
. Note also that the Bash prompt changes as will withsudo -i
but not withsudo -s
:Though
sudo -s
is convenient for giving you the environment that you are familiar with, I recommend the use ofsudo -i
for two reasons:.bashrc
.su
Asks root password, becomes root, opens an interactive non-login shell.su -
Asks root password, becomes root, opens an interactive login shell.sudo -s
Asks your passwords, becomes root, opens a interactive non login shell.sudo -i
Asks your passwords, becomes root, opens a interactive login shell.Best practice is to use the above two commands.
sudo su
Asks your password, becomes root momentarily to runsu
as root.sudo su -
Asks your password, becomes root momentarily to runsu -
as root.So in this case you are running
su
usingsudo
and you don't have to know root's actual password. The results are same assu
andsu -
.su
asks for the password of the user "root".sudo
asks for your own password (and also checks if you're allowed to run commands as root, which is configured through/etc/sudoers
-- by default all user accounts that belong to the "admin" or "sudo" groups are allowed to use sudo).sudo -s
launches a shell as root, but doesn't change your working directory.sudo -i
simulates a login into the root account: your working directory will be/root
, and root's.profile
etc. will be sourced as if on login.In Ubuntu or a related system, I don't find much use for
su
in the traditional, super-user sense.sudo
handles that case much better. However,su
is great for becoming another user in one-off situations where configuring sudoers would be silly.For example, if I'm repairing my system from a live CD/USB, I'll often mount my hard drive and other necessary stuff and
chroot
into the system. In such a case, my first command is generally:That way, I'm operating not as root, but as my normal user, and I then use
sudo
as appropriate.