How to switch to root user but stay in the same directory
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?
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
.
This is very simple. Just type
sudo su
instead of
sudo su - root
This will keep you in same folder as root.