When working on the command line, I often change to sudo using sudo -i. However, my working directory changes automatically to /root. I never want to go there; I want to stay where I was! How can I achieve this?


You could use sudo -s instead, it would not change your current directory to /root, though some of your environment variables would not be those of root.

This page from the Ubuntu Forums has a nice summary:

Summary of the differences found   
                                               corrupted by user's 
                HOME=/root      uses root's PATH     env vars
sudo -i         Y               Y[2]                 N
sudo -s         N               Y[2]                 Y
sudo bash       N               Y[2]                 Y
sudo su         Y               N[1]                 Y

This page from from Ubuntu's documentation has much more background information on sudo.

  • 1
    Beautiful! Thanks a bunch, simple but great little overview!
    – User402841
    Jun 13 '12 at 23:41
  • @user1162541 Happy to be able to help.
    – Levon
    Jun 13 '12 at 23:42
  • sudo -iu works for me ubuntuforums.org/…
    – rofrol
    Mar 4 '15 at 13:56
  • During my test, env vars of sudo -i is still corrupted by user's env vars. The only way to avoid to this is to use su -l.
    – Simba
    Nov 14 '19 at 8:07

If you want to use su, there is a way to stay in the same directory.

su - user -c "cd `pwd`; bash"

What’s going on here:

  • su - user = login as user
  • -c which means "run a command in the new user’s shell"
  • -c "cd `pwd`" the command we give is to switch to the current directory (`pwd`) – but because we use the backticks in double quotes, the pwd command is evaluated before we run the su command, so that we actually switch to the directory we’re in NOW as the old user.

    • By contrast, -c 'cd `pwd`' would execute the pwd command in the new shell, so this would evaluate to cd /root, which, of course, won’t accomplish anything.

    The only problem here is that the new shell exits right after running the command, so then we add:

  • -c "cd `pwd`; bash" which means "run bash (new shell) after running the cd command. The bash shell doesn’t exit until we log out of it.

Note that you can replace `pwd` with $(pwd).  They’re functionally the same, but the abundance of quote-like characters can become hard to read.


I've faced the same issue and I'm not allowed to run anything other than sudo su - devuser on dev server, so this is what I came up with:

  1. In devuser's .profile switch back to previous user home if found:
if [ -n $prev_user_home ] ; then
        cd $prev_user_home
  1. A script to determine a previous user. The script is placed in devuser's bin directory:
#brings you back home after sudo su

function get_owner {
  echo $(ps ouid -p $pid h | tr -d ' ')

my_uid=$(get_owner $pid)
while [[ $uid == $my_uid && $i -lt 20 ]] ; do
    pid=$(ps -o ppid= $pid)
    uid=$(get_owner $pid)

user_home=$(getent passwd $uid | cut -d ':' -f '6')
if [[ -d $user_home && $uid != 0 ]] ; then
    echo $user_home

It goes up a process tree and checks if process owning user changed.


K.I.S.S = Keep It Simple Student

The easiest way without complication (if you are executing a predefined script) is just to temporarily set the HOME environment variable after sudo -i. But don't forget to change it back when done. There's probably a way in Linux to call a script when you exit sudo, but I'm not sure what that is. You will need to chown whatever files the myuser needs to access when done since everything will be owned by root.

sudo -i
export HOME=/home/myuser

... do stuff

export HOME=/root

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.