0

Is there any difference between the two command?

sudo su - root

and

sudo bash -c 'su - root'

It seems a silly question but I believe there is some benefit on security by only allowing the second one. I just do not know what is the benefit for that. So could you give some advice on this?

1

sudo su lauches su directly with super user privileges, while sudo bash lauches the shell first and then executes the command with bash -c.

The main difference would be that your .bashrc script will be run before executing the su - root command. The outcome depends on that.

Use cases:

  • Lauch bash with elevated privilges, using your own password:

    sudo bash
    
  • Lauch shell as user root, with root's password:

    su - root
    
  • Lauch shell as user root, with your own password:

    sudo su - root
    
  • Everything already covered, no really need for:

    sudo bash -c 'su - root'
    
1

Sudo won’t allow you to natively do command redirection; for example, the following won’t work — permission is still denied on write attempt:

sudo echo -e “\x23\x21/usr/bin/sudo /bin/bash\napt-get update\napt-get -y dist-upgrade” > /usr/bin/distupgrade && sudo chmod a+x /usr/bin/distupgrade

Wrapping everything in sudo bash -c solves this problem:

sudo bash -c “echo -e \”\x23\x21/usr/bin/sudo /bin/bash\napt-get update\napt-get -y dist-upgrade\” > /usr/bin/distupgrade && chmod a+x /usr/bin/distupgrade”
0

You should, instead, use

sudo -i

su and sudo are two very different ways to escalate privileges, but in the end both allow you to run commands as your target user - in your case root.

sudo uses your username and your groups to decide wether or not you can execute a command, while su expects you to know the password of your target user - except for root, who can become anyone without password.

When you run sudo su - root you are authenticating twice, in practice to create a shell as root, which you can do by running sudo -i

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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