27

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?

1
  • 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. Dec 12, 2010 at 0:17

7 Answers 7

26

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!

6
sudo sh -c "cd dir_path"
3
  • 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. Dec 11, 2010 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. Dec 11, 2010 at 19:39
  • Adding the -s option along -c did the trick. Thanks for the effort. Dec 12, 2010 at 0:19
5

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.

2
  • 3
    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. Dec 11, 2010 at 23:06
  • To add to what @Gilles'SO-stopbeingevil' already said: you're using two programs (sudo and su) to do something that a single one could do. On some machines you might only have su available, on other you might have only sudo. It's better to learn to use both of these tools effectively than to memorize sudo su, even if it's a widely used practice.
    – silviot
    Aug 19, 2021 at 9:28
1

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.

0

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.

8
  • That produces an authentication error on Ubuntu. Dec 11, 2010 at 19:02
  • Really? I have never had an issue for me. Dec 11, 2010 at 19:06
  • 2
    For OP it would, certainly, as he doesn't doesn't have a proper root account. Dec 11, 2010 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. Dec 11, 2010 at 19:18
  • Yeah, I just get an authentication error. Dec 11, 2010 at 19:23
0

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.

-3

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.

3
  • -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. Dec 11, 2010 at 21:11
  • Good point. I didn't fully read what I was pasting there.
    – zcid
    Dec 11, 2010 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. Nov 3, 2011 at 21:52

You must log in to answer this question.

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