217

Why typing sudo cd whatever won't change the directory?

sri prasanna
  • 2,273

6 Answers6

216

Instead try using sudo -s to start a root shell and then simply cd into the directory.
When you're done as root, press CtrlD or type exit.


As Arjan hints at in his comment below, it is important to note that as root, one can easily do damage to essential system components. Use with care!

oKtosiTe
  • 9,776
136

cd is a shell builtin. sudo only works with executables. You could do sudo sh -c 'cd dirname' but as soon as the shell exits, you're returned to the directory you started from. If you say what it is you're trying to accomplish then I can help you find a way to do that.

18

You can simply su to become root and then cd all you want... I know an answer has already been accepted, but if one is not on the sudoers list then this is the only option.

Alex
  • 2,106
16

I had the same issue when I was attempting to navigate to the root directory in Kali Linux and:

sudo su

allowed me to execute:

cd /
hunt67241
  • 161
  • 1
  • 3
5

There are two ways that it "won't work", depending on your OS:

  1. If your OS follows POSIX, then running sudo cd will cause the external command "cd" (usually located at /usr/bin/cd) to execute in a forked process as the root user. That process changes directories successfully. Once that process is done, you will be returned to your regular shell, which is still in the directory where it started.

    So sudo cd runs without error, but does not change the current working directory of your current shell.

    (reference: this answer)

  2. Otherwise, running sudo cd will cause the following to happen. Your computer will look through your PATH, trying to find an executable named "cd". It will not find one. (only the shell built-in command "cd" exists, and that is not an executable file). Hence, you get an error. (On Ubuntu, I get the error message sudo: cd: command not found.)

    So sudo cd runs with error.

mareoraft
  • 280
0

you can typically switch to the root user bu typing "sudo sh" and cd into wherever