Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I want to cd into /var/named but it gives me a permission denied error, and when I want to use sudo to do this I am not permitted. What is the technical reason for this, and is it possible to do this some other way?

share|improve this question

3 Answers 3

up vote 29 down vote accepted

The reason you can't to this is simple and two fold

1: cd is not a program but an in-built command and sudo only applies to programs.

sudo foo means run the program foo as root

sudo cd /path returns

sudo: cd: command not found

because cd is not a program.

2: If it were possible to use sudo to cd to a protected directory then having run the command sudo cd /var/named you would be in that directory as a normal user but normal users are not allowed to be in that directory.

This is not possible.

Workaround:

You can use sudo -i to elevate your self to super user. For example

sudo -i
cd /var/named 

You are now logged on as root and can use whatever commands you wish. When finished type exit and you are back to being logged on as a normal user.

share|improve this answer
1  
Technically cd is both a builtin and an external command. unix.stackexchange.com/questions/50058/… –  kojiro May 7 '13 at 6:55
1  
@kojiro Ubuntu does not; at least in a clean install; have /usr/bin/cd only the built in –  Warren Hill May 7 '13 at 7:23
    
Regarding point 2: If you sudo -i (or sudo su) then cd /var/named and then su normaluser... can you do that? –  Alvin Wong May 7 '13 at 8:31
1  
@AlvinWong Why not try it and find out? Just tested if you sudo -i then cd to a directory you should not be in the su normal_user then pwd shows you are in the protected directory but ls does not work ls: cannot open directory.: permission denied –  Warren Hill May 7 '13 at 9:03

It's also worth remembering that, cd's status as a shell builtin or external binary notwithstanding, sudo works by spawning a new process to run the command specified.

Why is this important? Because the basic execution flow of sudo becomes something very similar to this:

  1. The shell spawns off a subprocess to run sudo with the given parameters
  2. sudo authenticates the user and confirms their right to execute the specified command
  3. sudo spawns off a subprocess to execute the specified command
  4. sudo waits for the subprocess spawned off in step 3 to exit
  5. sudo exits, returning to the shell
  6. The subprocess spawed in step 1 exits, returning the user to the shell prompt

(This may be technically slightly incorrect; there is a system call which actually replaces the running process with a new one (that's the C library's execve()). However, for the purposes of this explanation, the two are equivalent.)

This becomes important when you consider that the current working directory is a property of each process and is inherited but not promoted. So if process A spawns off a new process B, then process B starts with the same working directory that process A was in. (This is why something as mundane as ls ./ does what you'd expect.) But if process B changes its working directory, then unless process A goes out of its way looking for that, A is completely unaware of that change.

So even if sudo cd /somewhere did exactly what it says on the tin, by the time sudo exits, you are brought right back where you started. Hence effectively from the point of view of the user, it becomes a no-op. The fact that cd called the chdir() system library function doesn't help.

As @Warren Hill pointed out, the proper solution (I actually wouldn't call it a workaround) is to use sudo -i which drops you to a root shell where you can navigate around the filesystem freely and execute whatever commands you feel like. Do note however that when you exit this shell, you are still brought right back where you started in the directory hierarchy for exactly the same reason as I described above.

share|improve this answer

That's because cd is not an executable, it's a shell function to change directory.

If you run:

type cd

your will get:

cd is a shell function

You can use sudo -s to open an interactive shell and then cd to to your desired directory:

sudo -s
cd /var/named

To return back to your normal shell simply hit Ctrl+D.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.