32

I am using ubuntu 10.04.

I notice that after I run in terminal:

sudo -s

The prompt changed from:

my_user@my_hostname

to:

root@my_hostname

Seems it changed to root privilege.

But when I check the documentation of sudo command here, it explains another story of sudo -s, can anyone explain to me what is sudo -s doing exactly?

galoget
  • 393
  • 2
  • 10
Leem
  • 493
  • 3
  • 6
  • 9

5 Answers5

11

The two aren't really inconsistent - the sudo command always changes user, either to root, or to the user you specify with the -u switch. All the -s does is provide a shortcut for starting a shell as that user. It is really equivalent to:

sudo $SHELL

except that it will likely fallback to /bin/sh or something if SHELL is not set.

TomH
  • 3,094
  • 17
  • 16
8

sudo -s runs the shell specified in your $SHELL environment variable as the superuser/root. You can specify another user using -u.

The $SHELL environment variable contains the path to the user's default login shell. The actual setting for the default shell program is usually in etc/passwd. Depending on what you've done in your current session, the $SHELL variable may not contain the shell program you're currently using. If you login automatically with zsh for instance, but then invoke bash, $SHELL won't change from /bin/zsh.

Show the current user and shell program:

echo $(whoami) is logged in and shell is $0
  • whoami prints out the username the user is working under.
  • $0 contains the name/path of the currently running program (shell program in this case).
galoget
  • 393
  • 2
  • 10
RKelley
  • 181
  • 1
  • 3
3

From the manual:

sudo allows a permitted user to execute a command as the superuser or another user, as specified in the sudoers file.

-s Shell, runs the shell specified by the SHELL environment variable if it is set or the shell as specified in passwd(5).

More seriously, the sudo -s run a shell environment variable. Since you didn't add any variable it run as specified in passwd, and so connect you as root.

galoget
  • 393
  • 2
  • 10
  • 1
    It is not obvious from the manual that if you don't provide a user, it defaults to root. You would have to read the description for `-u` option to learn that. But for someone who is not familiar with `sudo`, they wouldn't know to look at `-u`. – wisbucky Mar 18 '15 at 23:20
1

Have a look at this post from superuser:

What's the difference between the commands "su -s" and "sudo -s"?

By the way, your post should be moved to superuser (or askubuntu as said in comments)!

jopasserat
  • 150
  • 1
  • 6
0

It sounds like it is creating another instance of the shell on top of the current shell, but with root privileges. I'll bet that after you do sudo -s if you type exit, you will go back to the original shell.

Noah Duncan
  • 101
  • 1