19

When I do su - to get to root, my current directory is set to root's home. Is there anyway to keep the current directory that I was in, much like sudo -s. Or is the answer to use sudo?

Amandasaurus
  • 30,211
  • 62
  • 184
  • 246
  • 5
    Please note that I don't want to sound rude, but why this problem is not solved via "man su" ? – Myrrdyn Jun 17 '09 at 17:13
  • 1
    @Rory please read the Myrrdyn comment – c4f4t0r Jan 16 '14 at 15:46
  • I like using `sudo su -c "zsh"`, in my case, keeps me in current dir and load [zsh](https://en.wikipedia.org/wiki/Z_shell) as shell with root user. `ctrl+d` after and you get back to current user. For directory thing, works directly on centos for me, but maybe adding ` && cd $(pwd)` inside passed command could do the trick. – GabLeRoux Apr 24 '15 at 17:08
  • @Myrrdyn Documentations are great when you know what you are looking for. When you don't, asking experts some advises is faster, and more didactic. – Romain Vincent Aug 18 '18 at 16:19

8 Answers8

19

It's always better to use sudo, if possible, because then you don't need to know (or give someone) root's password. Set the root password to something long and horrible and then lock it in a safe.

If you want to deny someone access later, you just remove their access to sudo, rather than having to teach everyone else a new root password.

However - you don't need to use the '-' parameter if you don't want to. You will get a shell as root, it will just not be a login shell (so it will not run root's .profile.)

crb
  • 7,928
  • 37
  • 53
  • 1
    In my case I needed alias to do something like `asuser git reset --hard`. But `su - -m -c "git reset --hard"` and `sudo -u git reset --hard` would save my environment and git would try to read **current** user's git config. So I just wanted to state that sometimes user205705's [solution](https://serverfault.com/a/567631/307481) using su - -c "cd `pwd`; bash" is better. – ReFruity Aug 29 '15 at 06:44
18

I agree that sudo is almost always a better answer but to answer the other part of the question...

The '-' in 'su -' indicates that you want to emulate a superuser login, rather than just run with superuser priviledges.

If you use plain 'su' rather than 'su -' you will stay in the same directory; however you will also be running in the same environment so may need to modify your path to access some admin commands.

Russell Heilling
  • 2,527
  • 19
  • 21
12

If you use su without the -, it should keep you in your current directory. -, -l or --login tell su to:

Provide an environment similar to what the user would expect had the user logged in directly.

Or just use sudo, it's got a lot of other advantages. Or ssh keys.

freiheit
  • 14,334
  • 1
  • 46
  • 69
7

If you really 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
  • -c which means "run a command in the new 's shell
  • -c "cd `pwd`" the command we give is to switch to the current direcotory (pwd) - but because we use the backticks, 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. 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 and the bash shell doesn't exit until we log out of it.
will.barley
  • 71
  • 1
  • 4
  • 2
    This is actually the only answer to the question. Good job! – alx Feb 09 '20 at 12:04
  • Actually, `su - [user] -c "cd $(pwd); bash"` might be slightly better (change backticks to `$()`). – alx Feb 09 '20 at 12:12
  • Any idea why I'm getting `bash: cannot set terminal process group (20): Inappropriate ioctl for device` and `bash: no job control in this shell` warnings when using this approach? – joe Oct 31 '21 at 09:55
6

You should definitely use sudo.

su -m
-m (-p): do not reset environment variables (generally not recommended)

That will keep you in the folder when you change to root.

Benefits of using sudo

Jindrich
  • 4,958
  • 8
  • 29
  • 42
  • 1
    I can't see how this is any different from just "su" on it's own, other than possibly not resetting the PATH environment, which is a possible security problem. – David Pashley Jun 17 '09 at 14:53
  • @DavidPashley for instance, it executes the `.bashrc` from the outer user, not the root one. – phil294 May 26 '17 at 03:32
3

Use sudo :). Seriously, you don't need su. 'sudo' is better as you only use it for privileged commands and can help avoid mistakes. You also get accountability.

chmeee
  • 7,270
  • 3
  • 29
  • 43
2

If you use "su" it does an interactive shell. This is the same as "sudo -s". "su -" creates a login shell, which will override the environment. "sudo -i" is the equivilent with sudo. If you are trying to get an interactive shell, you should always use the sudo -i (or su -) form, or it's possible to end up with weird file permissions in a user's home directory.

David Pashley
  • 23,151
  • 2
  • 41
  • 71
0

you must modify the source of su to have this job done. there is a chdir call in a procedure of initialize envoironment varibles. comment out those statesments included that call. and now, you can do "su -" but keeps the current directory.