0

Why sudo -i command is not setting the TERM, PATH, HOME, SHELL, LOGNAME, USER and USERNAME on my fresh Ubuntu 12.04.1 LTS as decribed in the manual?

# sudo -u johnny -i echo $HOME && echo $USER
/root
root

Using -H is not setting $HOME either. And my user does exist with a home :

# cat /etc/passwd
[..]
johnny:x:1000:1000::/home/johnny:/bin/bash

Update : Why am I having this issue? Because I am trying to create an ubuntu upstart job for multiple unicorn applications & I am using user installation of RVM + Bundle : without $HOME being properly evaluated, RVM do not find ~/.rvm.

1
  • I'd guess that sudo -E -u johnny echo $HOME && sudo -E -u johnny echo $USER would've worked for you.
    – krowe
    Sep 26, 2013 at 17:04

2 Answers 2

1

Not really an answer, but have you tried this:

$ cat /home/foo/test.sh
#!/bin/sh
echo $USER
echo $HOME

$ chmod +x /home/foo/test.sh
$ sudo -u johnny -i "/home/foo/test.sh"
johnny
/home/johnny

While running this:

$ sudo -u johnny -i echo $USER && echo $HOME
root
/root

Looks like your $USER is being evaluated in the command line, while the command is executed as the initial user.

1
  • Thanks for the hints, $USER is indeed being evaluated from the first command call and this is where my idea completely fail. I found a way to hack around this using new shell & escaping the variable from the first command call like this : sudo -u johnny -i /bin/sh -c "echo \$HOME".
    – jpdoyle
    Sep 23, 2012 at 2:36
1

In your example the variables ($HOME, $USER, ...) are interpreted before the sudo command is executed.

This, in contrast, should work as you'd expect it:

sudo -u johnny -i env | grep HOME

You must log in to answer this question.

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