Linux bash can be used to run and execute scripts, programs, applications, and commands in a fast and practical way. When a script, program, application, or command is tried to be executed there may be errors like “Permission Denied“. This error can be caused for different reasons. In this tutorial, we list some solutions to the permission denied error.
Bash Permission Denied Error
Linux requires different privileges and permissions in order to execute, read, write files, scripts, and commands, etc. If the required permissions are not set you may get the “Permission Denied” error.
Set Execution Permission
The first way to solve the permission denied error is settings proper permissions. In order to run an executable, program, application or script we should have the execution permission. The execution permission can be set with the chmod u+x command for the owner.
But this makes the file executable for the current owner of the executable, program, application, or script. Let’s list the owner information about the specified script.
-rwxrwxr-x 1 ismail ismail 16864 Nis 11 18:28 helloworld
We can see that the current owner of the helloworld command is the user “ismail”. We can see that the user ismail has the execution permission which is depicted in the rwxrwxr-x.
The current owner of the file can be changed with the chown command. But this operation requires the root privileges which can be provided with the sudo command. In the following example, we change the file owner user and owner group to the ahmet. The first ahmet is the owner user and the second ahmet is the owner group.
sudo chown ahmet:ahmet
Set Execution Permission For File System
If setting execution permission and changing the owner user of the file is not solved the problem there are other things to do. The commands are stored in a disk where they are mounted to the Linux system with file systems. File systems may prevent the execution of the command, script, program, or application it mounted as with noexec option. Check the file system if it is mounted with the noexec option by using the mount command with the -l option.
After specifying the file system with the noexec option we should remount this file system with the exec option. In the following examle we remount the partition “/dev/sda3” to the “/” with the exec option.
sudo mount -o remount,exec /dev/sda3 /
Set Target/Directory Path Permission
If the permission denied error is not solved yet the last step is setting the directory permission recursively where the binary, executable, script, application file resides. This can be a bit dangerous because it may lower the security barier and may give the owner users to execute other binaries. In the following example we made all files in the /mnt/d executable for their owners.
chmod u+x -r /mnt/d