How to Fix – Reading A File: Permission Denied on Linux?
Below are the solutions to resolve the “Reading A File: Permission Denied” problem in the Linux Operating System.
Solution 1: Changing File Permission
Step 1: To fix “Reading A File: Permission Denied” on Linux, you can change the file’s permissions using the chmod command. First, check the current permissions with ls -l, where the output shows permissions for the owner, group, and others in the format rwxrwxrwx. Use chmod to modify permissions, such as chmod +r filename to add read permission for the file. If you don’t have permission to change permissions, use sudo before the chmod command.
Syntax:
ls -l or ls -l filename
Example: This will list the current permissions granted to the file.
ls -l
Output:
Step 2: Now change the file permission so that the user can read the file. For that, we have to chmod command specifying whom you want to give permission to owner/user (u), group (g), others (o), or all (a) followed by the ‘+’ symbol which means to give permission and then that followed by the ‘r’ which means ‘read‘ and after that filename.
Syntax:
chmod [recipient]+r filename
Example: This will grant read permission to the user, others, and the group.
chmod og+r demo.txt #read permission to other and group
or
chmod u+r demo.txt #read permission to user
Output:
Solution 2: Using sudo Command
You can use the sudo command to run commands as a superuser, allowing you to access files and perform actions that require elevated privileges. For example, if you encounter “Permission Denied” while reading a file, you can use sudo cat filename to read the file with superuser permissions. This grants you the necessary access to bypass permission restrictions and read the file content.
Syntax:
sudo cat <filename>
Example: Using sudo with cat command to read demo.txt file
sudo cat demo.txt
Output:
Solution 3: Change file Ownership
You can change the ownership of the file using the chown command. For instance, if the file is owned by another user and you have permission to change ownership, you can use sudo chown your_username filename to change ownership to your user account. This allows you to read the file without encountering “Permission Denied.”
Syntax:
sudo chown <your_username> <filename>
Example: This command will change the ownership of the file and grant read permissions to the brahmbeyond user.
sudo chown brahmbeyond test.txt
Output:
To verify and read the file after granting permission, execute the below command in the terminal to read the contents of the file.
Command:
cat test.txt
Output:
How to Fix – Reading A File: Permission Denied on Linux
In this article, we will see how to fix when a permission error occurs while reading any file in Linux. We’ll see how to fix that and also why that error occurs, and its common causes so that in future you will be able to solve those kinds of errors yourself. We’ll learn various methods to solve this error allowing you to choose the approach that best suits your preferences.
Contact Us