August 21, 2010

Use Of "sudo cd" Command

There are certain privileged directories on the Linux system, that a normal user can't access. You need to know the root password or at least be a sudoer to access those directories. Some people try sudo cd to cd into such directories, but end up with an error. I am describing a few here along with the explanation.

1. sudo cd does not work.

varsha@varsha-laptop:~$ sudo cd /etc

[sudo] password for varsha:
sudo: cd: command not found


Explanation: cd is a shell command, and not a program. So, when you try sudo cd, there is no program that can be launched, and so you get the error sudo: cd: command not found.

Also, there is a list of commands that can be run by certain users and not ALL commands can be run. This is maintained through a sudoers file. See:

varsha@varsha-laptop:~$ man sudoers

2. Becoming root first, and then the cd command works.

varsha@varsha-laptop:~$ su
Password:
root@varsha-laptop:/home/varsha# cd /directory-path
root@varsha-laptop:/directory-path#


Explanation: Anyone can use sudo to enter a directory and manipulate files, so this calls for the use of su instead of sudo.

Thanks to Sharad Birmiwal for some of the information. Please refer to http://groups.google.com/group/iitdlug/browse_thread/thread/c1523585d9fc7987 for the discussion.