I am a new Ubuntu user and find some people tell me to use sudo -i to get root and others tell me to use sudo -s. What is the difference? Which one do I use and when?
Asked
Active
Viewed 4,270 times
11
Zanna
- 72,471
user2156473
- 111
2 Answers
13
The major difference between sudo -i and sudo -s is:
sudo -igives you the root environment, i.e. your~/.bashrcis ignored.sudo -sgives you the user's environment, so your~/.bashrcis respected.
Here is an example, you can see that I have an application lsl in my ~/.bin/ directory which is accessible via sudo -s but not accessible with sudo -i. Note also that the Bash prompt changes as will with sudo -i but not with sudo -s:
dotancohen@melancholy:~$ ls .bin
lsl
dotancohen@melancholy:~$ which lsl
/home/dotancohen/.bin/lsl
dotancohen@melancholy:~$ sudo -i
root@melancholy:~# which lsl
root@melancholy:~# exit
logout
dotancohen@melancholy:~$ sudo -s
Sourced .bashrc
dotancohen@melancholy:~$ which lsl
/home/dotancohen/.bin/lsl
dotancohen@melancholy:~$ exit
exit
Though sudo -s is convenient for giving you the environment that you are familiar with, I recommend the use of sudo -i for two reasons:
- The visual reminder that you are in a 'root' session.
- The root environment is far less likely to be poisoned with malware, such as a rogue line in
.bashrc.
dotancohen
- 2,864
4
sudo -i
-i [command]
The -i (simulate initial login) option runs the shell speci‐
fied by the password database entry of the target user as a
login shell. This means that login-specific resource files
such as .profile or .login will be read by the shell. If a
command is specified, it is passed to the shell for execution
via the shell's -c option. If no command is specified, an
interactive shell is executed. sudo attempts to change to
that user's home directory before running the shell. The
security policy shall initialize the environment to a minimal
set of variables, similar to what is present when a user logs
in. The Command Environment section in the sudoers(5) manual
documents how the -i option affects the environment in which
a command is run when the sudoers policy is in use.
sudo -s
-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 the password database. If a command is specified, it is
passed to the shell for execution via the shell's -c option.
If no command is specified, an interactive shell is executed.
Avinash Raj
- 80,616
Pitel
- 262
-
1Additional info: you can also check the
manpageman sudofor more information. – v2r Feb 08 '14 at 14:32 -
If I can understand what the man page is saying, I won't find this page... Seriously, I think the man page of Linux also need involve more people to contribute. Probably we can centralize the repo of all the man pages so people can contribute to it more easily. – Jing He Nov 21 '24 at 08:27
sudo -sunless you have placed something setting$PS1in an unusual way in one of the initialization scripts. Tested on GNU bash, version 4.4.19(1) (Ubuntu 18.04 LTS) and Sudo version 1.8.21p2 – Weijun Zhou Dec 09 '18 at 17:12