Trusted answers to developer questions

How to resolve the "Permission Denied" error in Linux

Get Started With Data Science

Learn the fundamentals of Data Science with this free course. Future-proof your career by adding Data Science skills to your toolkit — or prepare to land a job in AI, Machine Learning, or Data Analysis.

widget

While using Linux, you may encounter the error, “permission denied”. This error occurs when the user does not have the privileges to make edits to a file. Root has access to all files and folders and can make any edits. Other users, however, may not be allowed to make such edits.

Remember that only root or users with Sudo privileges can change permissions for files and folders.

The permissions can be changed using the chmod keyword. The syntax for the command is:

chmod flags permissions filename

  • flags are the additional options users can set.
  • permissions define if the user can read, write, or execute the file. They can be represented using symbolic or octal numbers.
  • filename is the name of the file whose permissions are changed.

Here is an example where users can read, write, and execute a file; whereas, group and others can only read it.

chmod u=rwx,g=r,o=r file

Here, each letter has a meaning:

  • r gives read permissions

  • w gives write permissions

  • x gives execute permissions

The same command can be run using octal notation:

chmod 744 file

Here, each digit represents the sum of the permissions allowed:

  • 4 gives read permissions

  • 2 gives write permissions

  • 1 gives execute permissions

  • 0 gives no permissions

The sum of these permissions is used to represent each type of author.

Flags

The following flags can be set:

Flag--help--version-R, --recursive--reference=file--preserve-root--no-preserve-root-v, --verbose-f, --silent, --quiet-c, --changesDescriptionopen the help menuoutput the version numberchange permisssions recursivelyset permissions similar to filedo not open recursively on '/'open recursively on '/', defaultoutput a message for every filedo not output error messagesgives verbose output when a change is made

Live demonstration

Now, let's resolve this issue in the below terminal. click the following terminal and change the file mode by passing the chmod +x example_script.sh and then again execute the file by using ./example_script.sh .

Note: We have used a file example_script.sh that only have read permission. So we can read it but we can't excute the file but we can read its content. So we need to update its permission to get access.

Terminal 1
Terminal
Loading...

RELATED TAGS

linux
error
chmod
Copyright ©2024 Educative, Inc. All rights reserved
Did you find this helpful?