1

So I think many people have gone through this issue where you SSH into a machine, cd to whatever path you need to be at, and then realize you actually want to be root and not your own user. However, when you sudo su -, you sign in as root and get taken to root's home directory.

Is there a way to copy the PWD before I run sudo su - and then cd to it after without typing it again? Something like cd - that is available across users.

  • 1
    Shouldn't sudo su (without the trailing dash) log you in as su without changing the directory? – onse Sep 13 '13 at 14:23
3

A few things.

First you should almost never sudo su. There's no reason for it, but lets pretend your in one of the very few circumstances where you want to sudo su.

Second, sudo su (no -) will keep you in your current directory and bring over your current shell setup (i.e. vars and such). So your real answer is to use sudo su without a -. Now lets pretend you want to use it together with - for some reason (note: the usages for this are damn rare). The result is that you will log in as root and not just run things as root.

Third, assuming you want to get back to your directory after sudo su -ing. Your best bet is to just copy and paste, or navigate back to the directory. However, if you insist:

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

I would like to stress however that you are almost certainly using sudo and su incorrectly. The whole point of sudo (and not having a root account) is so that you run things as your self with elevated permissions. Your circumventing that.

I suggest reading:

2

Use sudo -s, it will open a new shell as root, but stay in the same directory. Note that various other environment variables from your regular user will be taken along as well.

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.