Run "cd" command as superuser in Linux

Is there a way to run the cd command with superuser privileges to gain access to directories that are owned by root? When I run sudo cd <path>, I get sudo: cd: command not found.


Solution 1:

The other answer isn't wrong.

Possibly a better answer is:

The sudo tool is intended to take actions as a superuser, and you're describing something that is more of a state change that would precede actions such as 'ls' or 'vi' or others to make them simpler.

I suggest, e.g. if you wanted to edit a file in /root/private/:

sudo ls /root
sudo ls /root/private
sudoedit /root/private/<name from previous ls command>

This is definitely more typing, and a little harder than just changing directories. However, it is far more audit-able, and much more in-line with the principles behind sudo than running some variant of 'sudo bash.'

If you are working in a secure environment, your IA team will thank you. If you find yourself wondering: "What change did I make the other day?," then you will thank you, because you won't have to wonder what file or files you edited.

All of this said, enabling and executing some form of 'sudo bash' is definitely easier. If you were looking for easier, why are you using 'sudo' in the first place instead of just logging in as root?

Solution 2:

As you noted, cd is a shell built-in command, and there's a reason for that: the "current directory" is a per-process parameter which can be only changed by the process itself.

Your shell's working directory cannot be changed by any child process – so even if you manage to run cd in a privileged subshell, it'll only change the working directory of that temporary subshell, and it does not matter what method of raising privileges you use.

So for sudo cd to work, sudo itself would have to be a shell built-in, and it would need some way to raise privileges of an already-running process. Currently no such mechanism exists on Linux (nor most other operating systems).


One way to achieve what you want is to run an interactive shell with root privileges (any method works), and just use the regular cd in it:

[user@host /]$ sudo bash
[root@host /]# cd /root/secret

If you want to do it all in one command, it would have to look like this – first change the working directory, then start an interactive shell:

sudo bash -c "cd /root/secret && bash"
su -c "cd /root/secret && zsh"

Note: The outer command doesn't have to be a shell, it just needs to be something that changes its working directory and executes a new command. Recent Linux systems have one or two helpers which could be used:

sudo nsenter --wd="/root/secret" bash       # util-linux v2.23 (debian jessie)
sudo env --chdir="/root/secret" bash        # coreutils v8.28 (debian buster)

The advantage of this method is that it doesn't require nested quoting; you can run a multi-word command without having troubles with whitespace or special characters.

Finally, some programs have a built-in option to change their working directory:

sudo make -C /etc
sudo git -C /root/secret log