Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
up vote 12 down vote accepted

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.

share|improve this answer
Beautiful! Thanks a bunch, simple but great little overview! – user 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

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.

share|improve this answer

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.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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