Permission denied linux скрипт

Unable to execute bash scripts even as root?

That can happen if you have mounted the file system with the «noexec» option. You should remove it.

Also, to know quickly if your filesystem has been mounted with the ‘noexec’ option, use: mount And to remove the ‘noexec’ option, simply delete it from the list of options against the filesystem in the following file: /etc/fstab . Or alternatively add the ‘exec’ option to the end of the options.

The user option can cause this issue, as well. Removing it allowed me to execute the binary in question.

Another possible reason in Ubuntu can be the default file manager behavior. Go to filemanager->edit->prefferences->behavior and check execute on double click

Script needs be executable. Use this:

This duplicates another answer from several years back. It’s probably a common beginner problem, but this particular instance of this answer shouldn’t deserve more upvotes than the original, especially since the question already mentions the original OP had already made sure the permissions were correct.

Although not directly pertinent to this particular thread; if a file has come form a Windows system there may be a CR/LF at the end of the line. This would affect all lines in the file, including the initial execution line, and would not be visible if you are viewing the file.

$ ./test.sh -bash: ./test.sh: /bin/bash^M: bad interpreter: No such file or directory 

To see this, you could cat -A the file: $ cat -A ./test.sh #!/bin/bash^M$ echo «me»^M$

to see the actual rights and ownership of the file. To see if the chmod command actually worked. You might want to change the ownership along with the mod of the file check : http://www.tuxfiles.org/linuxhelp/fileowner.html

Use chmod +x ./test.sh this should allow you to run it.

Also, check to see if the directory/filesystem containing the script is nfs-mounted. root won’t run scripts from nfs-mounted locations.

In macOS this can occur if a com.apple.quarantine flag exists. If you see a @ suffix on the permissions after running a ls -l on the script’s path, execute ls -l@ *script_path* to confirm. Then run a xattred -d com.apple.quarantine *script_path* to remove the quarantine flag.

you need use ./test.sh when you in the directory of that file,if you don’t,try PATH TO THE SCRIPT .or you can copy it to some directory of /data and chmod it for shell,then do the above steeps.if you still fail,it’s ok because i have a same problem,i just did it success for once time.

if you are root user and still have that problem,so your shell is broken.i know that because i couldn’t execute many commands of the sh shell(similar to bash) even i tried as root and it said permission denied like your,i couldn’t change the permission.then i tried to copy them to my directory in /data ,change permission and i could use commands again.but when i try to execute the script,it’s no such file or directory.

Источник

Читайте также:  Linux количество попыток входа

How do I fix shell script permission denied in Linux?

In Linux, you may experience a “permission denied” error while trying to list files or execute a shell script inside the directory which does not have sufficient permissions. As Linux operating system is very concerned about its security, the “root” has complete access to all directories and files for making changes. Therefore, other users may not be permitted to make such changes.

Permission denied error in shell script execution

In our system, we have a shell script named “samplescript.sh”. Now, as a normal user, we will try to execute this hell script.

The output will show you the “permission denied error” because you do not have the permission to execute this script.

Fixing permission denied error

To avoid this “permission denied error,” the only thing you have to do is to add “x” or “execution” permission to this “samplescript.sh” file and make it executable for a typical user.

Firstly, check out the file permission of the shell script.

Using chmod command

The chmod command lets a user change permission of a file using a reference file, numeric or symbolic mode.

Syntax of chmod command:

  • flags: user can set these additional options
  • permissions: this part of the chmod command is used to define file permissions which include: “r” for read, “w” for write, and “x” for making it executable.
  • filename: specify the filename whose permissions you want to change.

Whereas “u+x” will make the script executable for the current Linux user, though the group owner or other “users” already have access to execute it.

Execution of the above-given chmod command should change the “samplescript.sh” into an executable format. Now execute the “ls” command to confirm the changes we made into the permissions of this shell script.

Utilize the cat command to view the content of this “samplescript.sh” script file.

Finally! It’s time to execute the shell script.

The output declares that we have successfully fixed the permission denied error of this “samplescript.sh” shell script.

Conclusion

Every Linux user should know the quick fix for the “permission denied” error encountered while executing any shell script. “chmod” command resolves this issue by changing the script’s file permissions and allowing it to in an executable format for the current user. This article has provided you a step-by-step procedure for fixing the shell script “permission denied” execution error.

About the author

Talha Saif Malik

Talha is a contributor at Linux Hint with a vision to bring value and do useful things for the world. He loves to read, write and speak about Linux, Data, Computers and Technology.

Источник

Run ./script.sh vs bash script.sh — permission denied

When I try to run ./script.sh I got Permission denied but when I run bash script.sh everything is fine. What did I do wrong?

Can you edit the question to include the contents of the shebang line and also the output of getfacl script.sh ?

4 Answers 4

Incorrect POSIX permissions

It means you don’t have the execute permission bit set for script.sh . When running bash script.sh , you only need read permission for script.sh . See What is the difference between running “bash script.sh” and “./script.sh”? for more info.

You can verify this by running ls -l script.sh .

Читайте также:  Find java jre linux

You may not even need to start a new Bash process. In many cases, you can simply run source script.sh or . script.sh to run the script commands in your current interactive shell. You would probably want to start a new Bash process if the script changes current directory or otherwise modifies the environment of the current process.

Access Control Lists

If the POSIX permission bits are set correctly, the Access Control List (ACL) may have been configured to prevent you or your group from executing the file. E.g. the POSIX permissions would indicate that the test shell script is executable.

$ ls -l t.sh -rwxrwxrwx+ 1 root root 22 May 14 15:30 t.sh 

However, attempting to execute the file results in:

$ ./t.sh bash: ./t.sh: Permission denied 

The getfacl command shows the reason why:

$ getfacl t.sh # file: t.sh # owner: root # group: root user::rwx group::r-- group:domain\040users:rw- mask::rwx other::rwx 

In this case, my primary group is domain users which has had execute permissions revoked by restricting the ACL with sudo setfacl -m ‘g:domain\040users:rw-‘ t.sh . This restriction can be lifted by either of the following commands:

sudo setfacl -m 'g:domain\040users:rwx' t.sh sudo setfacl -b t.sh 

Filesystem mounted with noexec option

Finally, the reason in this specific case for not being able to run the script is that the filesystem the script resides on was mounted with the noexec option. This option overrides POSIX permissions to prevent any file on that filesystem from being executed.

This can be checked by running mount to list all mounted filesystems; the mount options are listed in parentheses in the entry corresponding to the filesystem, e.g.

/dev/sda3 on /tmp type ext3 (rw,noexec) 

You can either move the script to another mounted filesystem or remount the filesystem allowing execution:

sudo mount -o remount,exec /dev/sda3 /tmp 

Note: I’ve used /tmp as an example here since there are good security reasons for keeping /tmp mounted with the noexec,nodev,nosuid set of options.

I would like to add that it’s also wise to check if the folder containing the script is «executable» or not (so it has the X flag or not). If it doesn’t, then the script will be only executable by root user.

Try chmod +rx script.sh , this will give read and execute permissions to user, group and others.

On my win7 with admin running cmd; I have .sh files associated with cygwin64/bin/bash, but it was blocked by cmd. None of the above suggestions helped (chmod, setfacl, mount).

The solution below worked, it is an admin sledge-hammer acl-fixer whenever folders/file become inaccessible to admin on win7, which is often):

 Start > run cmd as Admin c:\> script.sh Access is denied. cmd> chmod 0777 script.sh c:\cygwin64\bin\bash.exe cmd> script.sh Access is denied. > assoc .sh .sh=bash > ftype bash bash=C:\cygwin64\bin\bash.exe -- "%1" %* > bash $ FILE=c:/cygwin64/bin/bash.exe $ FILE=$ # s,/,\,g # Compare these permissions using accesschk by Mark Russinovich 2015 $ accesschk.exe -lq $FILE $ accesschk.exe -lq c:/windows/system32/cmd.exe # [large output not shown] # === Solution: Change windows acl for bash === $ takeown /F $FILE /A > /dev/null $ icacls $FILE /t /q /c /reset $ icacls $FILE /t /q /c /grant :r Everyone:F $ icacls $FILE /t /q /c /setowner Administrators # ==== cmd> script.sh OK .. invokes bash 

Источник

Permission denied when running .sh scripts

program exited with code: 126 . This problem always occur when I try to execute my code. What might be the solution?

Читайте также:  Linux поиск файлов утилита

When I do that it tells me this chmod: cannot access `geany_run_script.sh’: No such file or directory. Should I run it from the terminal or what?

5 Answers 5

If you want to skip these (for now of course), you can create a directory/folder in your user-home directory and work on your C programmes (or others) there.

You can open the terminal (press Ctrl + Alt + T ) and cd to the target directory:

To give the file «the_file_name» execute permission (if the file-system allows you with the RW rights):

..what’s your working directory ie. what’s the location of your source code file? ..do you have some experience with windows CLI?

@wkhwilo-012, please copy your executable file Area_circumference and paste it (the file path will be pasted) to update your question’s body.

You need to give execute and read permissions. Follow this:

chmod u+r+x filename.sh ./filename.sh 

When we make a new script file then by default it has read and write permission. But if we want to execute them, then we should give execute permission as shown above.

Execution bit alone is not enough for shell scripts, one must be able to read the file as well to execute it (contrary to binaries which only need the execute permission bit)

Open your terminal application by pressing CTRL + ALT + T or with the apposite shortcut on the graphical enviroment (like Terminal or xTerm ).
In the uniform window which appears on the screen you’ll see a blinking character, it’s the terminal cursor : simply click on the window and write to enter text (typically commands) and press ENTER to confirm the input.
Before the cursor there is always listed your current position on the file system from the root directory («/») and your home (where your personal files are) is called «~».
To change directory/folder use cd EXISTENTFOLDER (replace EXISTENTFOLDER with the folder name); if you feel lost, simply type cd to return to your home directory in a blink!
Now let’s solve your problem:

  1. Use the cd command to find the directory with your source code. Use TAB to help you. If you execute ls -lh , you’ll see a list of possible paths to follow and files to execute.
  2. When you’ve find the blocked file execute chmod +x FILENAME (replace FILENAME with the name of your source code file).
  3. If you have multiple blocked files execute chmod +x * to unlock all files in the current directory. Never chmod +x dangerous or insecure files.
  4. Execute ./FILENAME YOUREVENTUALARGUMENTS to execute your executable file.
  5. Remember that if your compiled program tries to read/write outside your home directory you’ll need to execute it as root by using sudo ./FILENAME YOUREVENTUALARGUMENTS .

If you want to have a manual for a command execute man COMMAND (replace COMMAND with the exact command name, Linux is case sensitive).

Some shells have an Open terminal here command to simplify your life, search for it in the future and remember that the command shell can be your best friend, if you use it well. 😀

It’s all. If you need more help comment under here.
If I’m helping you press the UP arrow on the left; if you solve mark this answer as best answer.

Have a nice experience on Linux & Ubuntu.

Источник

Оцените статью
Adblock
detector