How to switch to root user but stay in the same directory

42,105

Solution 1

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.

Solution 2

This is very simple. Just type

sudo su

instead of

sudo su - root

This will keep you in same folder as root.

Share:
42,105

Related videos on Youtube

Hammad Hassan
Author by

Hammad Hassan

I am working in Machine Learning from 2015. I worked on traditional ML algorithms as well as on Deep Neural Networks. The most enthusiastic part of my expertise includes using the latest transformer-based models, launched by Google and Facebook. A short summary of my technical skill set is as: • 6+ years experience in ML / NLP / Deep Learning • 4+ years hands-on experience with Python in ML • 3+ years hands-on experience using AWS for building and deploying ML models • 2 years hands-on experience with Java in ML (Stanford CoreNLP) • scikit-learn • pytorch • transformer • numpy / pandas • Spacy / NLTK • SQL • Docker • MatPlotLib I am a self motivated person and learning following stuff now a days: • Big Data with Apache Spark and Python • PowerBI

Updated on September 18, 2022

Comments

  • Hammad Hassan
    Hammad Hassan over 1 year

    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?

  • terdon
    terdon about 7 years
    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.
  • Barmar
    Barmar about 7 years
    @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
    Barmar about 7 years
    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.
  • terdon
    terdon about 7 years
    @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
    terdon about 7 years
    @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.
  • Barmar
    Barmar about 7 years
    @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.
  • Hammad Hassan
    Hammad Hassan about 7 years
    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?
  • terdon
    terdon about 7 years
    @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?
  • Hammad Hassan
    Hammad Hassan about 7 years
    "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.
  • terdon
    terdon about 7 years
    @HammadHassan that makes no sense. If ls shows no files, then there are no files in the current directory. Your user or the permissions of the files can't change that. Come into the main chat room, if you like, and we can try and figure this out.
  • Fadi
    Fadi over 2 years
    @HammadHassan you can't see hidden files without passing any flags to ls