1

What is the difference between sudo -s and sudo -i and why do they give me different shells? It appears -s keeps me within bash and -i gives me sh.

On MacOSX High Sierra 10.13, if I type sudo -s into a terminal and enter my password, I get root as my username, but with a dollar sign at the prompt:

user1@mymachine:~$ sudo -s
**root@mymachine:~$**

But if I type in sudo -i, I get:

user1@mymachine:~$ sudo -i
Password:<br/>
**mymachine:~ root#**

Why am I not presented with the hash sign # if I am root in the above example with sudo -s? Am I not truly root?
Are the environment variables different between the two?

1 Answer 1

0

The first thing you need to realize is that sudo -s is equivalent to sudo -s root and sudo -i is equivalent to sudo -i root. Whenever you omit a user from sudo, it assumes you are targeting the user root.

sudo -s runs a shell as the target user while sudo -i logs in as the target user.

Whenever you run sudo -s you are effectively changing your shell to the root user shell while whenever you run sudo -i you are effectively logging out of your current user and logging in as root. This is important to know because whenever you are changing your shell to the root user shell, you are still keeping your user export information. When you run sudo -i you are logging out of your user and logging in as root. That means that you adopt the shell configuration of the root user.

This is unrelated but you should avoid changing your user to root because you might accidentally break things, you should just sudo every command that needs escalation even if it gets annoying. If you really want to change to a root shell, you should run su which will allow you to log in as root.

3
  • I see. So with sudo -s keeping my export, which is export PS1="\[\033[m\]\u\[\033[m\]@\[\033[m\]\h:\[\033[31m\]\W\[\033[m\]\$ " how come I don't get a #? From manual: "\$ If you are not root, inserts a "$"; if you are root, you get a "#" (root uid = 0) "
    – DrKumar
    Mar 8, 2018 at 23:18
  • I can't explain that because I don't have any experience with macs, maybe its because your export just uses the $ instead of the #. That's my best guess and I could be wrong.
    – Desultory
    Mar 8, 2018 at 23:26
  • Figured it out--needed to add one more \ to escape it. Now it displays a # accordingly. Thanks!!
    – DrKumar
    Mar 8, 2018 at 23:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .