cp: cannot create regular file: Permission denied

71,957

/tmp Directory has all the permissions (read/write) for all users. but if you made /tmp/foo by your own account, it has its permissions just for you! if you want to make it writable for other users (or programs) change its permission with this command:

chmod 777 /tmp/foo

If you have any other files inside this directory from before, add -R flag to above command.

Update: Use this command to change /tmp/foo owner from your own to apache default user:

sudo chown www-data:www-data /tmp/foo -R

also please check your apache2 configuration to see which user it has for running the php scripts.

Share:
71,957
GorTeX
Author by

GorTeX

Updated on September 18, 2022

Comments

  • GorTeX
    GorTeX over 1 year

    I am running a PHP script on my Apache server and from the script I need to copy some files (to run a Bash script that copies files). I can copy to a directory /tmp with no problems, but when I want to copy to /tmp/foo then I get this error:

    cp: cannot create regular file '/tmp/foo/file.txt': Permission denied
    

    even though the permissions for the directory /tmp and /tmp/foo are set to the same value.

    Do you know what is the problem?

    • Siva
      Siva over 5 years
      can you share the result of whoami and ll /tmp
    • Hamid Yousefi
      Hamid Yousefi over 5 years
      @SivaPrasath /tmp is always for root by default! the two commands results is not related in this case, becase /tmp has all the permissions for all users
    • Siva
      Siva over 5 years
      @HamidYousefi yes, tmp is for root, but not for /tmp/foo OP might be running the script as a different user with /tmp/foo belongs to.
    • GorTeX
      GorTeX over 5 years
      @SivaPrasath whoami is apache and ll /tmp returns drwxrwxrwx for /tmp/foo
    • GorTeX
      GorTeX over 5 years
      @SivaPrasath Enforcing -what does it mean? ...but my problem has been solved by Hamid
    • Siva
      Siva over 5 years
    • ctrl-alt-delor
      ctrl-alt-delor over 5 years
      Hello welcome. To make things easier in the future, when asked for clarification, please clarify by amending the question (not by adding a comment), show commands, and output in full. This will help us with answering you question more quickly. Thanks, and happy questioning.
  • GorTeX
    GorTeX over 5 years
    Thanks for the answer, but it doesnt work. I have set the permissions for /tmp/foo to 777 and still I get the same error. When I run the script as a normal user then it is okay. But problem is when I run it from a php script
  • Hamid Yousefi
    Hamid Yousefi over 5 years
    Can I see your scripts please!? and are you using php or php-fpm? your problem is, by default php using apache configured user for running scripts, while you made /tmp/foo by your own account! I will update my answer, check it again please.
  • GorTeX
    GorTeX over 5 years
    php script: <?php shell_exec(" bash /path/to/script.sh ) ?> and script.sh is #!/bin/bash cp /my/folder/file.txt /tmp/foo
  • GorTeX
    GorTeX over 5 years
    How can I find which user apache uses for running php scripts? When I put to my php script bash command whoami then I get "apache"
  • Hamid Yousefi
    Hamid Yousefi over 5 years
    can you give me the output of this command: ls -alh /tmp | grep foo if the directory has foo name.
  • GorTeX
    GorTeX over 5 years
    drwxrwxrwx 4 apache apache foo
  • Hamid Yousefi
    Hamid Yousefi over 5 years
    just rm -rf /tmp/foo and then add this line before your shell_exec(" bash /path/to/script.sh ) This one: if (!file_exists('/tmp/foo')) mkdir('/tmp/foo', 744); test it please.
  • GorTeX
    GorTeX over 5 years
    Now it is working! Thank you very very much. Still dont understand why it didnt work before but I am glad it does now :)
  • Hamid Yousefi
    Hamid Yousefi over 5 years
    I really became happy :) you had permission issue that I can't tell you exactly where it made problem. but now your problem solved ;)
  • GorTeX
    GorTeX over 5 years
    permissions for the /tmp/foo now is d-wxr-x--T..havent seen that before
  • Hamid Yousefi
    Hamid Yousefi over 5 years