Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

How to I change into a directory owned by root?

If I do: sudo cd RootOwedDir

I get: sudo cd: command not found

If I su root

I get: authentication failure, probably because I never really set up an actual root account.

What am I doing wrong here?

share|improve this question
To summarize: both cYrus's "sudo su" and "sudo sh -sc "cd dir_path" (note the additions 's' parm) solved the problem. Thanks to everyone for their help. – unknown google user Dec 12 '10 at 0:17
up vote 8 down vote accepted

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.

It is important to note that as root, one can easily do damage to essential system components. Use with care!

share|improve this answer

That's because cd isn't a command but a built-in shell function; and su expects the root password. Simply use:

sudo su

to run a shell with root privileges. Type exit or press Ctrl + D to return to the previous shell when you're done.

share|improve this answer
sudo su is unnecessarily complicated. sudo -s makes sudo start a shell; or you can run sudo bash or sudo zsh if you don't want to have to remember the -s option. – Gilles Dec 11 '10 at 23:06
sudo sh -c "cd dir_path"
share|improve this answer
This is not working for me either. It might be because the directory I want to enter has embedded spaces in the name. I have tried variations on sudo sh -c "cd my\ path" with no luck. No error message it just returns as if it did something but I remain in the same directory. sh is installed on my system though I normally use bash. – unknown google user Dec 11 '10 at 19:30
@unknown - I've always figured you'd have to spawn a subshell to run shell builtins as root, whether in bash or plain old sh. I'm determined to figure this out. – Wesley Rice Dec 11 '10 at 19:39
Adding the -s option along -c did the trick. Thanks for the effort. – unknown google user Dec 12 '10 at 0:19

You can also do a sudo vim on the folder, ie sudo vim RootOwedDir and it will show the vim view for folders, which will show all the files/folders that are in that folder. then you can just run what you need to from outside the folder, and sudo it just like normal.

share|improve this answer

su - then type in your password. You now can just cd into the directory. Remember tho you are now root so ctrl-d before you do anything you don't want to be root for.

share|improve this answer
That produces an authentication error on Ubuntu. – Wesley Rice Dec 11 '10 at 19:02
Really? I have never had an issue for me. – MrStatic Dec 11 '10 at 19:06
For OP it would, certainly, as he doesn't doesn't have a proper root account. – Wesley Rice Dec 11 '10 at 19:15
Interesting, I am on a fresh install of Ubuntu, I never 'setup' a root user yet I can simply sudo - and bam I am god of the system. – MrStatic Dec 11 '10 at 19:18
Yeah, I just get an authentication error. – unknown google user Dec 11 '10 at 19:23

For me a combination of sudo and screen worked out:

sudo -iu vagrant screen -mS npm_install bash -c 'cd /vagrant && npm install'

This command first switches to the vagrant user. Then as vagrant changes the directory to /vagrant and executes npm install.

share|improve this answer

The easiest way to do this is to enable the root account and use su to become root. Ubuntu is very protective of the root account due to the potential for misuse, but at times, it is much easier to use the root account rather than sudo on every line. Just be aware of what account you are using at any given time.

Taken from here:

Open the terminal and type: sudo passwd root. When you see the prompt that says “Enter new UNIX password”, enter the password you want for the root user and confirm it.

At this point, you will be able to change to root using su and cd to the directory.

share|improve this answer
-1. This is full of wrong. What does GDM have to do with anything? OP wants to 'cd' to a dir readable only by root. No one should ever log in as root via GDM. – Wesley Rice Dec 11 '10 at 21:11
Good point. I didn't fully read what I was pasting there. – zcid Dec 11 '10 at 21:14
Much easier than this is enabling a root shell with sudo -s as others have said, and much better than this is learning about sudo, a much more flexible and granular tool than su, if well used. – Adriano Varoli Piazza Nov 3 '11 at 21:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .