Why doesn’t `sudo cd /var/foo` work?

So, you want to cd into /var/foo but it gives you a permission denied error and when you want to use sudo to do this you are not permitted.

What is the technical reason for this and is it possible to do this some other way?

The reason you can’t do this is simple, 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 so it’s not possible to use sudo cd.

But luckily there is a workaround for that:

You can use sudo -i to elevate yourself to super user. Like that:

istechnolgy@localhost:~$ sudo -i

istechnolgy@localhost:~$ cd /var/foo

But be careful, this can be dangerous,  now you are  logged on as root  and as root you can  use whatever commands you wish without any restriction. It is very very important that after you finish whatever you had to do in that folder to logout as root, this is also simple, just type exit and you are back to being logged on as a normal user.

Be the first to comment

Leave a Reply