8

I have the following doubt. In a tutorial related to a software installation that I am following say that I have to execute the following commands (I am doing it into an ssh shell, so this list of steps end with the exit command):

sudo -s
apt-get update
apt-get install -y build-essential libtool libcurl4-openssl-dev libncurses5-dev libudev-dev autoconf automake screen
exit

My doubts are:

What exactly does the -s parameter do after sudo command?

Searching on the web I found that:

‑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

It seems to me that the sudo -s execute a command using the environment variable of the shell.

But this is not clear for me: in this case what is the command executed with the environment variable? (it only executes sudo -s and not sudo -s [command]).

7

The simple answer is it gives you a root shell. That's what it's being used for here. There's a good technical comparison between the su, sudo -i, sudo -s and sudo su methods on Unix.SE but that's really not relevant here. The code could have used any and would have worked.

The help nugget means a few things:

  • It's looking for a command in the $SHELL environment variable. If you run echo $SHELL you'll probably see /bin/bash. That means you'll get a root instance of Bash. I'm sure if you were in zsh it would mean you get a root instance of zsh.

  • If $SHELL is empty, it falls back to the default shell defined in /etc/passwd for that user.

  • If you provide a command (eg sudo -s whoami), it's actually running: sudo /bin/bash -c "whoami"

  • If you don't pass a command, it doesn't pass a -c argument over so you just get an interactive shell.

I've used the phrase "root" several times. By default sudo is all about running things as root but sudo (and su) can run things as other users (if you have permission to do so). I'm only stating this for the pedants who will cry out that sudo -s -u $USER doesn't give them a root shell as I promised -s would above.


And in my humblest of opinions, it's really silly to become root for just two commands, not to mention that it could leave a user accidentally running further commands as root. If you want or need to run something as root, just premend the command with sudo:

sudo apt-get update
sudo apt-get install -y build-essential libtool libcurl4-openssl-dev libncurses5-dev libudev-dev autoconf automake screen

I'd be dubious of any tutorial that suggested getting a root shell, except in the cases where you do have bucketloads of commands to run... And even then, there's scripting.

| improve this answer | |
  • Perfect explaination. Now it is all more clear. One last doubt. If I preappend sudo to all the previous commands I will obtain the same final result? is it true or am I missing something? – AndreaNobili Jun 3 '14 at 22:06
  • @Oli you are 100% correct about Zsh, "sudo -s" logs into a "root" shell as "root" not Super User. – RCF Jun 3 '14 at 22:57
  • @AndreaNobili Yeah, that's what sudo is generally for; running a command as root without having to manually log in as root. – Oli Jun 3 '14 at 23:26
  • FYI I posted an answer below yours and my take on sudo -s is drastically different because of my limited knowledge and happening across the situation by accident. Still you might want to read my "humble" answer and possibly refresh your 1/2 decade old answer. – WinEunuuchs2Unix Dec 4 '19 at 2:18
0

sudo -s will run a command in the current directory like a regular user would. For example:

rick@alien:~/askubuntu$ suspendtime
Nov 12 14:06:27 to Nov 12 17:22:21 lasting 11,754 seconds
Nov 12 22:23:37 to Nov 13 04:24:37 lasting 21,660 seconds
(... SNIP ...)
Dec 02 21:58:49 to Dec 03 04:20:52 lasting 22,923 seconds
Dec 03 05:52:43 to Dec 03 16:37:44 lasting 38,701 seconds

Linux uptime 1,859,356 seconds (21 days, 12 hours, 29 minutes, 16 seconds)
45 Suspends 1,173,341 seconds (13 days, 13 hours, 55 minutes, 41 seconds)
Real uptime 686,015 seconds (7 days, 22 hours, 33 minutes, 35 seconds)

rick@alien:~/askubuntu$ sudo suspendtime
sudo: suspendtime: command not found

rick@alien:~/askubuntu$ sudo -H suspendtime
sudo: suspendtime: command not found

rick@alien:~/askubuntu$ sudo -s suspendtime
Nov 12 14:06:27 to Nov 12 17:22:21 lasting 11,754 seconds
(...SNIP...)
Dec 03 05:52:43 to Dec 03 16:37:44 lasting 38,701 seconds

Linux uptime 1,859,431 seconds (21 days, 12 hours, 30 minutes, 31 seconds)
45 Suspends 1,173,341 seconds (13 days, 13 hours, 55 minutes, 41 seconds)
Real uptime 686,090 seconds (7 days, 22 hours, 34 minutes, 50 seconds)

Of course using -s instead of prefixing the command with ./ may not be that great of a time saving.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.