16

I recently switched to Ubuntu and I have observed one thing when I use the following command to switch to root user

sudo su - root

It takes me directly to root's home (/root), so I lose my place in the filesystem (the directory where I executed this command). Is there any way to switch to root without losing the current directory?

2 Answers 2

20

First of all, don't use sudo su. It's not wrong, or dangerous or anything, it's just inelegant and pointless. You are running two separate programs to do a job easily handled by one. If you want to start a shell as root, sudo can do it for you.

If you want to start a login shell (that's what sudo su -) does, use sudo -i. That, however, since it starts a login shell, will take you to root's home directory by default.

To start a regular, non-login shell, you can use sudo -s. That will start a root shell for you and keep you in the directory you ran it from:

terdon@tpad ~ $ pwd
/home/terdon
terdon@tpad ~ $ sudo -i  ## changes directory
[root@tpad ~]# pwd
/root
[root@tpad ~]# logout
terdon@tpad ~ $ sudo -s  ## doesn't change directory
[root@tpad terdon]# pwd
/home/terdon

So, the way to start a root shell and stay in the same directory is sudo -s.

7
  • 1
    The problem may be that sudo -s doesn't run the target user's profile because it's not a login shell. So perhaps the real question is whether there's a way to get a login shell with sudo, but without changing directory. Probably not.
    – Barmar
    Mar 4, 2017 at 22:45
  • @Barmar no, but sudo -i does. I used -s since the OP didn't want a login shell (didn't want to move to $HOME). If you want a login shell, use sudo -i. I've posted a comparison of the environments of sudo -i, su -, sudo -s and su in my question here if you want to have a look.
    – terdon
    Mar 4, 2017 at 23:41
  • Just now I had a very bad experience with this command, sudo -s. Even it does not change the directory but it did not gave me full root access. Some of my files were present in a directory but I was unable to see them as normal user and as root user in this way (sudo -s). Then I used the old way (sudo su - root) and went to the directory and my files now were visible to me. Why sudo -s have given me restricted access? Apr 27, 2017 at 9:48
  • @HammadHassan there is no way what you describe could have happened. There must be something else going on. What do you mean "unable to see" the files? Do you maybe mean "unable to read the files"? Are you maybe using encryption?
    – terdon
    Apr 27, 2017 at 9:51
  • "unable to see" means simple command "ls". It did not showed me my files. No encryption at all. My files are simple. And this is really happened. Apr 27, 2017 at 10:10
8

This is very simple. Just type

sudo su

instead of

sudo su - root

This will keep you in same folder as root.

4
  • 9
    This isn't wrong, I mean it does work, but using sudo su doesn't make much sense. That's what sudo -s is for. I've never understood why so many Ubuntu tutorials suggest sudo su.
    – terdon
    Mar 4, 2017 at 14:42
  • @terdon I've also never understood it, but it seems to be widespread among all Linux users. Maybe the alliteration makes it more memorable.
    – Barmar
    Mar 4, 2017 at 22:43
  • @Barmar personally, I've only seen it in the Ubuntu world. Since most Linux systems have a normal root account by default, when needing a shell you'd do su (alone, no sudo). Because, however, Ubuntu disables the root account by default, the sudo su has come quite naturally. it is also, apparently, the case in some corners of the UNIX world as well, but in Linux, I've only seen it in Ubuntu.
    – terdon
    Mar 4, 2017 at 23:40
  • @terdon I've seen it for decades, I'm not even sure what distributions we were using. We use Debian in my current company, and my coworkers use it.
    – Barmar
    Mar 5, 2017 at 0:44

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.