I ssh into my machine and then, in order to run some commands, I need to sudo bash first.

In an interactive terminal session, if I do this:

[vagrant@33 ~]$ sudo bash
[root@33 vagrant]# which bundle

things work. But if I need to do this in one line:

[vagrant@33 ~]$ sudo bash -c "whoami; which bundle"
which: no bundle in (/sbin:/bin:/usr/sbin:/usr/bin)

things stop working. Why is this happening and what else do I need to do to the second scenario in order to be able to run it like the first one?

  • "which: no bundle in (/sbin:/bin:/usr/sbin:/usr/bin)" <--- hint!
    – Braiam
    Nov 17 '14 at 12:59

When you call sudo bash you run new instance of bash with user root environment. But if you call sudo bash -c "whoami; which bundle" the environment is remain as for your user. If you havn't intention to do so you can modify last call by sudo options -i

-i, --login

Run the shell specified by the target user's password database entry 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 command is run with an environment similar to the one a user would receive at log 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.


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.