Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to "su" WITHOUT changing the current directory?

890 views
Skip to first unread message

Peter Hanke

unread,
Jul 18, 2010, 8:11:13 AM7/18/10
to
Assume I am currently logged in as user "karl" in a terminal with current path

/tmp/

Now I do a "su" and enter the root password.

This causes the terminal to switch to user "root" and - unfortunately - to a different directory
e.g. /home/root

Ok, I can do now a

cd /tmp

to get to the other directory.

but I could imagine that there is a way to tell Unix to stay automatically (!) in the
directory where the original "su" command was issued.

How can I achieve this?

Peter

Jean-David Beyer

unread,
Jul 18, 2010, 8:16:09 AM7/18/10
to
Peter Hanke wrote:
> Assume I am currently logged in as user "karl" in a terminal with current path
>
> /tmp/
>
> Now I do a "su" and enter the root password.
>
> This causes the terminal to switch to user "root" and - unfortunately - to a different directory
> e.g. /home/root

That should happen _only if_ you do

su -

of

su - root


>
> Ok, I can do now a
>
> cd /tmp
>
> to get to the other directory.
>
> but I could imagine that there is a way to tell Unix to stay automatically (!) in the
> directory where the original "su" command was issued.
>
> How can I achieve this?

You should not have to do anything to achieve what you want. It is
default behavior. You may have put something in your .bashrc file or the
global one.
>
> Peter
>


--
.~. Jean-David Beyer Registered Linux User 85642.
/V\ PGP-Key: 9A2FC99A Registered Machine 241939.
/( )\ Shrewsbury, New Jersey http://counter.li.org
^^-^^ 08:10:01 up 10 days, 16:56, 3 users, load average: 4.61, 4.73, 4.84

Chris F.A. Johnson

unread,
Jul 18, 2010, 9:06:30 AM7/18/10
to

'su' does not change directories; it will leave you in the same
directory.

'su -' will behave as a login and will change directories.

--
Chris F.A. Johnson, <http://cfajohnson.com>
Author:
Pro Bash Programming: Scripting the GNU/Linux Shell (2009, Apress)
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)

DenverD

unread,
Jul 18, 2010, 9:10:16 AM7/18/10
to

you don't say what (distro/version) you are using...but some Linux
distros (like mine, below) are born with 'su' doing what you wish
yours would do...

that is, change to the root user but retain the original user's
environment (current location in (for example) /tmp, PATH etc etc etc)

to jump to /root/home (and get _all_ of root's environment) i have to
'su -' or 'su - root'..

suggest you look in man su for your distro/version to determine how to
pick up root powers with the user's environment..

oh, and you might check your alias list to see if someone made 'su' an
alias for 'su -' etc...

--
see caveat: http://tinyurl.com/6aagco
DenverD (Linux Counter 282315) via Thunderbird 2.0.0.23 (20090817),
KDE 3.5.7 "release 72-11", openSUSE Linux 10.3, 2.6.22.19-0.4-default
#1 SMP i686 athlon

Sam

unread,
Jul 18, 2010, 9:16:58 AM7/18/10
to
Peter Hanke writes:

Works for me.

[mrsam@octopus ~]$ pwd
/home/mrsam
[mrsam@octopus ~]$ su root
Password:
[root@octopus mrsam]# pwd
/home/mrsam

unruh

unread,
Jul 18, 2010, 11:46:34 AM7/18/10
to
On 2010-07-18, Peter Hanke <pete...@andres.net> wrote:
> Assume I am currently logged in as user "karl" in a terminal with current path
>
> /tmp/
>
> Now I do a "su" and enter the root password.
>
> This causes the terminal to switch to user "root" and - unfortunately - to a different directory
> e.g. /home/root

Nope. It does not. It leaves the directory alone. Now if you do
su -
then it will change directory.

Aragorn

unread,
Jul 18, 2010, 12:46:27 PM7/18/10
to
On Sunday 18 July 2010 14:11 in comp.os.linux.misc, somebody identifying
as Peter Hanke wrote...

> Assume I am currently logged in as user "karl" in a terminal with
> current path
>
> /tmp/
>
> Now I do a "su" and enter the root password.
>
> This causes the terminal to switch to user "root" and - unfortunately
> - to a different directory e.g. /home/root

Two remarks here:

[1] The use of "su" - as opposed to "su -", "su - root" or
"su -l root" should *not* take you out of the directory
you're in. It should normally give you superuser
privileges without changing your environment. The other
commands I've just listed do source root's environment
and would indeed normally take you to the root user's
home directory.

[2] Having the root user's home directory as "/home/root" is
non-standard on GNU/Linux and unless "/home/root" is on the
root filesystem, it will give you headaches when you're
attempting to boot up in single-user maintenance mode, i.e.
runlevel 1. When dropping to runlevel 1 from within any
other runlevel, you'd be okay, but when booting up to
runlevel 1, no other filesystems will be mounted than the
root filesystem, and with the root user's home directory
living on another filesystem, you would have no access to
the root user's environment variables.

> Ok, I can do now a
>
> cd /tmp
>
> to get to the other directory.
>
> but I could imagine that there is a way to tell Unix to stay
> automatically (!) in the directory where the original "su" command was
> issued.

Normally, issuing "su" without any parameters should do just that. If
it doesn't on your system, then perhaps you should check your aliases
to see if "su" was aliased to "su -" or "su - root".

--
*Aragorn*
(registered GNU/Linux user #223157)

Greg Russell

unread,
Jul 19, 2010, 3:33:06 PM7/19/10
to
In news:4c42ef61$0$6993$9b4e...@newsspool4.arcor-online.net,
Peter Hanke <pete...@andres.net> typed:

> Assume I am currently logged in as user "karl" in a terminal with
> current path /tmp/
>
> Now I do a "su" and enter the root password.
>
> This causes the terminal to switch to user "root" and - unfortunately
> - to a different directory e.g. /home/root
...

> but I could imagine that there is a way to tell Unix to stay
> automatically (!) in the directory where the original "su" command
> was issued.
>
> How can I achieve this?

By learning how to use the "man su" command. The -f option will be right in
front of you, as the "man su" page is quite short.


Golden California Girls

unread,
Jul 19, 2010, 10:27:33 PM7/19/10
to

Really?
-f If the invoked shell is csh(1), this option prevents it from
reading the ``.cshrc'' file.

will....@gmail.com

unread,
Jan 16, 2014, 3:01:27 AM1/16/14
to
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 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.

Richard Kettlewell

unread,
Jan 16, 2014, 4:02:03 AM1/16/14
to
Please fix your line lengths.

will....@gmail.com writes:
> If you want to use su, there is a way to stay in the same directory.

Yes. Don’t use the “-” option.

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

This doesn’t work if the cwd path contains spaces (among other
possibilities).

--
http://www.greenend.org.uk/rjk/
0 new messages