If you’re getting an error like “cd: Permission denied” when trying to change directories in Bash, don’t worry, you’re not alone.
This is a common issue that can be fixed fairly easily. In this blog post, we will discuss two ways to fix the cd: Permission denied error.
Hopefully one of these methods will work for you!
Table of Contents
What does cd: Permission denied mean?
The cd: Permission denied error indicates that you don’t have the necessary permissions to access the directory.
Boost Your Website Speed!
If you want your website to run as fast as ours, consider trying Cloudways. Their powerful cloud infrastructure and optimized stack deliver exceptional performance. Free migration!This can happen for a few reasons, such as incorrect ownership or insufficient privileges.
Understanding file permissions in Linux
Before we dive into the two ways to fix this error, let’s quickly review what file permissions are in Linux. File permissions in Linux determine who can access, modify, and delete files or folders.
Every file or directory has three levels of ownership:
- User owner (u).
- Group owner (g).
- Others (o).
Each level of ownership can be assigned the following permissions:
- Read (r).
- Write (w).
- Execute (x).
Note that the execute permission for a file allows you to execute that file. The execute permission for a directory allows you to access the contents of the directory, but not execute it.
So if you don’t have the execute permission of that directory, you will get a permission denied error when you try to access it.
To learn more about file permissions, I recommend the following article.
What does r w x mean for a directory in Linux?
A directory is a file, and “read” permission means you can read it. But you really cannot do very much without x permission as well.
With directories, you usually have both read and execute permission or neither. On a directory, that x is officially called “search permission”.
You need x to use a directory in a pathname. So if you try “cat /etc/passwd”, you will need x on / and /etc. You also need x to cd into a directory.
Suppose you have read but not search (x) permission on a directory. What can you do? Not much.
You can use “ls” to view the file names. Even “ls -l” will not work. Read access without search permission is not very useful.
How to check file permission in Linux?
Now that we understand file permission. Let’s see how to check the file permissions of the directory you’re trying to access.
To do this, use the ls -l command in your terminal (replace ‘your_directory’ with the name of your directory):
ls -l your_directory
The first letter of the output will be either a ‘d’ or a ‘-’ which tells you if it’s a directory (d) or file (-). The next nine characters are permissions in three sets: user, group, and others. The r stands for read, the w stands for write and the x stands for execute.
For example, if you see a ‘rwx’ for user, that means the user has all permissions (read, write and execute). If you see ‘r-x’ then the user only has read and execute permissions.
In the following example, there is no x permission for all the users, groups or others. In this case, you won’t be able to change into the directory.
% ls -l testdirectory
drw-r–r– 1 howtouselinux staff 0 3 3 21:04 testdirectory
To learn more about how to check file permissions, I high recommend the following article.
How to fix cd: Permission denied?
So how do we fix this? Here are two methods:
modify directory permission
If the permissions are incorrect, you can use the command “chmod” to change them.
In our case, it is related to the execution permission. We can use the following command to add the x (execute) permission.
- chmod u+x your_directory – add execute permission for the owner of this directory
- chmod g+x your_directory – add execute permission for all the group users
- chmod o+x your_directory – add execute permission for all the other users
- chmod a+x your_directory – add execute permission for all the users
Note: You might switch to the root user to change the permission or contact the system administrator to change this.
modify user privileges
You can also add your user to the right groups to fix this problem if the directory permission can not be changed.
If you are not sure which group this directory belongs to, you can use ls -l command to find out.
To do this, you’ll need to use the command “usermod -a -G groupname username”.
The -a switch in the command above stands for append and is important. If you add a user to a group without using the -a flag, the user will be removed from any other groups that they may already be part of.
The -G flag signifies that you want to add the user to the group name following the switch.
Check this post to get more info about how to add users to group in Linux.
After this, you can check the user’s group by using the command “groups”. Now you should have permission to access the directory.