255

I am running Ubuntu on computer 1 and computer 2. I compiled a C++ program on computer 1, and I can execute it from the terminal using ./program_name. It runs fine.

However, when I try to do this on computer 2, it says: bash: ./program_name: permission denied

What's wrong and what can I do about it?

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Kian
  • 3,189
  • 3
  • 18
  • 16

3 Answers3

486

chmod u+x program_name. Then execute it.

If that does not work, copy the program from the USB device to a native volume on the system. Then chmod u+x program_name on the local copy and execute that.

Unix and Unix-like systems generally will not execute a program unless it is marked with permission to execute. The way you copied the file from one system to another (or mounted an external volume) may have turned off execute permission (as a safety feature). The command chmod u+x name adds permission for the user that owns the file to execute it.

That command only changes the permissions associated with the file; it does not change the security controls associated with the entire volume. If it is security controls on the volume that are interfering with execution (for example, a noexec option may be specified for a volume in the Unix fstab file, which says not to allow execute permission for files on the volume), then you can remount the volume with options to allow execution. However, copying the file to a local volume may be a quicker and easier solution.

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
  • 5
    I used to think this would only happen on non Unix/Linux native partitions such as FAT32, and would not happen on `/tmp`. It turns out my tmpfs `/tmp` suffers same problem, and I have to copy the executable file into a real native volume, i.e. my home folder ~ in a ext4 partition. – RayLuo Jun 19 '17 at 03:55
  • 21
    I, encountered this problem today, so I STFW, the first search result is in purple color, which means I've read that before. "That is a promising sign", I spoke to myself, and then click that search result. It brought me to this page, this answer which doesn't work in my case, and then finally the comment above which works. And then I notice that comment was written by me, 2 months ago. Wow, what a life. – RayLuo Aug 27 '17 at 06:22
  • 5
    Sorry for piggybacking on this Q/A ... please also think that maybe **noexec** is in **fstab** which mounts the drive without any execution privileges ... "I heard" that "some people" lost quite a bit of time looking for that ... – Simon Opelt Oct 18 '17 at 11:30
  • @SimonOpelt Yep, that's exactly the issue in my case. – Izkata Jun 01 '18 at 21:15
  • it is possible to provide permanent permission to a file in order to avoid repeat the chmod manipulation each time I want to use it ? – Webwoman Sep 13 '18 at 18:49
  • 1
    @Webman: `chmod` makes a durable change. If the permissions change after you use `chmod` to enable them, then something else is changing them. (This may include something else deleting the file and recreating it or the volume the file is on being dismounted and remounted. If the volume is being remounted, there are ways to specify default permissions for files on external volumes, but that is beyond the scope I can answer now.) – Eric Postpischil Sep 13 '18 at 18:52
  • 2
    @SimonOpelt "other people" are thankful – Matt Jacobsen Dec 10 '18 at 14:50
42

Try this:

sudo chmod +x program_name
./program_name 
Vitor Villar
  • 1,855
  • 18
  • 35
  • Thanks for your advice. I did just try that -- but now it says bash: ./program_name cannot execute binary file – Kian Sep 23 '13 at 13:42
  • Could it be because computer 2 is 32bit and computer 1 is 64bit? I guess maybe I should just compile it on computer 2. Thank you for your help everyone. – Kian Sep 23 '13 at 13:55
  • Yes, if you compile some program in 64 bits, and try to execute in a system 32 bits, does not work. you need recompile the source code on the computer 2. – Vitor Villar Sep 23 '13 at 13:59
  • 2
    `sudo` is generally not necessary, unless the program is in a directory where you don't have write permission (in which case, how did you install it there in the first place?) – tripleee Oct 21 '19 at 15:02
15

Sounds like you don't have the execute flag set on the file permissions, try:

chmod u+x program_name
Arnab Nandy
  • 6,472
  • 5
  • 44
  • 50
Sam Roberts
  • 199
  • 3