Fixing the ‘Permission Denied’ Error on Linux

If you receive the Permission Denied error on your Linux system, it usually means that your user account does not have the proper permissions on the file or directory you are trying to interact with. All files and directories in the Linux file system have user and group permissions attached to them that delegate access to read, write, or execute the file. These permissions work independently of each other, so just because you are able to open a file, does not mean you can edit it.

In this tutorial, we will show how to fix the Permission Denied error on Linux. These methods involve basic permission changes on files, or elevating to the root account for protected documents. Follow along with us below to start editing file permissions and fixing the error.

In this tutorial you will learn:

  • How to view file permissions and owner
  • How to change read, write, or execute permissions on a file
  • How to change the owner or group of a file
  • How to use the root account to bypass permission restrictions
Fixing the 'Permission Denied' Error on Linux
Fixing the ‘Permission Denied’ Error on Linux
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any Linux distro
Software N/A
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

Fixing the ‘Permission Denied’ Error on Linux – step by step instructions




The Permission Denied error can occur on files and directories alike. Here is what it looks like when we try to execute a script that our user account does not have executable permissions for:

Permission Denied error occurring on Linux when trying to execute a file
Permission Denied error occurring on Linux when trying to execute a file

This just means that our current user account does not have the properly configured permissions to interact with the file in the way that we desire. We can utilize the chown or chmod commands to remedy the issue and grant ourselves the permissions required to use the file.

Follow the step by step instructions below to remedy the Permission Denied error:

  1. Let’s start by checking what permissions are on the file by using the ls -l command. This will show the owner, group, and permission information for any file or directory. Specify the path to the file or directory as an argument:
    $ ls -l example.sh
    -rw-rw-r-- 1 linuxconfig linuxconfig 32 Jan 28 17:15 example.sh
    

    The output shows us that the owner and group have read, write permissions, and other users have read permissions. As you can see, we do not have execute permissions on the file, which is why we get the error when trying to execute it.

  2. The simplest and fastest way to fix the issue is by adding the proper permissions that we need by using the chmod command:

    Add read permissions:

    $ chmod +r example.sh
    

    Add write permissions:

    $ chmod +w example.sh
    

    Add execute permissions:

    $ chmod +x example.sh
    

    Add all permissions:

    $ chmod +rwx example.sh
    
    WARNING
    It is recommended that you only add the necessary permissions to each file. It is tempting to simply assign rwx permissions to any file that you plan to use, but file permissions are a security feature and should only be handed out to users or groups that really need it.



  3. Alternatively, you can take ownership of the file when appropriate. This usually affords you more permissions than other users, but you can always choose to edit the permissions where appropriate as well. This command will change the owner of the file to user linuxconfig with the chown command.
    $ chown linuxconfig example.sh
    
  4. If you are trying to read, edit, or execute a system file (not an ordinary document), then it is recommended that you use the root account instead of changing file permissions. Changing the file permissions of a system file or application configuration file can have undesirable side effects. If, for example, you needed to edit a system file with nano, just preface the command with sudo:
    $ sudo nano /path/to/config
    

    Alternatively, elevate to the root account and open, edit, or execute the file that you needed permissions on with the following Linux command:

    $ su -
    # nano /path/to/config
    
  5. Keep in mind that you can also change the permissions on directories. If you find that you can’t create any new files in a certain path, it is probably because you do not have write permissions on the directory. This can be fixed with chmod:
    $ chmod +w /path/to/dir
    

    Or edit the permissions on all files recursively by adding the -R option to your command:

    $ chmod -R +w /path/to/dir
    
  6. It is also possible to use absolute mode (permissions represented by numbers) instead of symbolic mode (permissions represented by rwx). Some examples:

    Give full permissions (read, write, execute) for the owner of the file, and read permissions to all other users:

    $ chmod 744 file-name
    

    Give full permissions (read, write, execute) to every user:

    $ chmod 777 file-name
    

    Check out our tutorial on Intro to Linux File Permissions for more examples on how to use absolute mode and symbolic mode to modify file permissions.



Closing Thoughts

In this tutorial, we saw how to fix the Permission Denied error on a Linux system. As discussed, this error is the result of missing the necessary permissions in order to either read, write, or execute a file or a directory’s contents. The ways covered to remedy this issue are by changing file permissions with chmod command, taking ownership with chown command, or using administrator privileges with the sudo command. The root user account always has full permissions on any file, regardless of what has been configured.



Comments and Discussions
Linux Forum