0

I am able to make an ssh connection without password to that remote server. Also if i execute a single line command instead of a bash script, it works without any problems. But if i try to execute a bash script via ssh, it says

bash: /home/username/script.sh: Permission denied

How can i fix this? I can execute single line commands via ssh without any problems.

13
  • Is the executable bit set on the script? Also how do you execute the command? ssh-login, then command or ssh foo@bar "command"?
    – FelixJN
    Nov 3, 2021 at 11:40
  • is /home/username/script.sh executable on remote hosts ? (e.g. has flag x set )
    – Archemar
    Nov 3, 2021 at 11:41
  • 2
    Maxine please edit your question to include information. If you put it in the comments it can get missed or sometimes even accidentally deleted
    – roaima
    Nov 3, 2021 at 11:48
  • 2
    @maxine Please either elaborate how you did so, so others may take this as reference, or delete the question if you feel that others would not profit from your findings. It is always annoying to find someone with the same problem and a mere "I solved it" as the last comment with no solution provided.
    – FelixJN
    Nov 3, 2021 at 11:55
  • 1
    @Damir thanks a lot. that will be useful for me. i am still new at linux.
    – maxine
    Nov 3, 2021 at 12:13

1 Answer 1

1

If you are trying to run this:

/home/username/LastBootupTime.sh

and it has these permissions:

-rwxr--r-- 1 root root 42 Nov  3 14:01 LastBootUpTime.sh

then only root will be able to do so. Everyone else will get the error bash: /home/username/LastBootUpTime.sh: Permission denied.

This is because the permissions are such that:

  • rwx - the owner (root in this instance) can read/write/execute the script
  • r-- - the group (root group in this instance) can read but neither write nor execute the script
  • r-- - everyone else can read but neither write nor execute the script

Now, in your case you've explicitly specified this command:

bash /home/user/LastBootupTime.sh

so although you need read permission you do not need execute permission. Therefore there's another reason for the error message. It's most likely that one of the directories is inaccessible to users.

Perhaps your real command is this:

bash /root/LastBootupTime.sh

In this instance only the root user can access the /root directory, so everyone else will get a permission denied error. The solution is to put the script into a suitably accessible directory (I use /usr/local/bin on my systems; other people prefer /opt/bin, but you could place it anywhere that worked for you).

4
  • The bash: script.sh: Permission Denied message also turns up when executing a script with shebang instruction and no execetuable permissions. @Maxime did not run bash script.sh as written. Maybe this might confuse a reader.
    – FelixJN
    Nov 3, 2021 at 11:59
  • thanks "chmod 777 script.sh" solved my problem. this answer is approved. sadly i am a guest user i am not able to give an upvote. but approved the answer
    – maxine
    Nov 3, 2021 at 12:03
  • @maxine chmod 777 is a really bad idea - it allows anyone on the server to edit your script. Better is chmod u=rwx,go=rx (equivalent to chmod 755 but easier to read)
    – roaima
    Nov 3, 2021 at 13:35
  • @roaima thanks a lot for that info. i am still new at linux and learn slowly. this will help me a lot in the future.
    – maxine
    Nov 3, 2021 at 14:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.