60

I am trying to upload image through admin page, but it keeps saying:

[Errno 13] Permission denied: '/path/to/my/site/media/userfolder/2014/05/26'

the folders userfolder/2014/05/26 are created dynamically while uploading.

In Traceback, i found that the error is occuring during this command:

In /usr/lib64/python2.6/os.py Line 157. while calling

mkdir(name, mode) 

meaning, it cannot create any folder as it doesnot have the permission to do this

I have OpenSuse as OS in Server. In httpd.conf, i have this:

<Directory /path/to/my/site/media>
   Order allow,deny
   Allow from all
</Directory>

Do I have to chmod or chown something?

7
  • Make sure the directory /path/to/my/site/media is writable by web server process.
    – falsetru
    May 26, 2014 at 13:04
  • 1
    ps aux | grep apache will show you what user own the process.
    – falsetru
    May 26, 2014 at 13:07
  • @falsetru this shows me root. so this is my django code, right?
    – doniyor
    May 26, 2014 at 13:07
  • What about ps aux | grep httpd | grep -v grep or ps aux | grep apache | grep -v grep ?
    – falsetru
    May 26, 2014 at 13:10
  • 1
    chown -R httpd:httpd /path/to/my/site/media (the second httpd is group name. You need to change it if the group of the httpd is different from httpd)
    – falsetru
    May 26, 2014 at 13:16

8 Answers 8

48

You need to change the directory permission so that web server process can change the directory.

  • To change ownership of the directory, use chown:

    chown -R user-id:group-id /path/to/the/directory
    
  • To see which user own the web server process (change httpd accordingly):

    ps aux | grep httpd | grep -v grep
    

    OR

    ps -efl | grep httpd | grep -v grep
    
3
  • I'm using gunicorn. I get same error message. I want to give gunicorn read/write access to a folder under /home/portfolio. How do I go about it?
    – KhoPhi
    Jan 30, 2015 at 22:45
  • @Rexford, You'd better post a separated question with gevent tag.
    – falsetru
    Jan 31, 2015 at 2:33
  • @falsetru I solved the issue by changing the owner of main folders and it's subdirectory/files to www-data. I am using flask+apache for backend server. Thanks for your guidance.
    – Ajay
    Jun 1, 2017 at 15:27
24

This may also happen if you have a slash before the folder name:

path = '/folder1/folder2'

OSError: [Errno 13] Permission denied: '/folder1'

comes up with an error but this one works fine:

path = 'folder1/folder2'
3
  • 15
    It is worth explaining why this happens. A / before the folder name in Unix indicates that the folder should exist in the root directory, which also houses all the major sensitive system folders e.g. /usr/, /bin/, etc. (try it, do cd / and see where you land up). Ordinary processes are not permitted to modify the root directory. Omitting a leading / defaults the folder to the current working directory, which is perfectly safe and which Unix will do for you. Aug 23, 2016 at 17:59
  • @mjp When i tried to remove the first slash from 'folder1/' it indeed worked and the error was gone, however i have no idea were that folder is now? So where is it? How do i access it? thanks
    – masky007
    Nov 1, 2018 at 2:42
  • I created a folder called folder1 in you current directory Feb 11, 2020 at 14:14
1

Probably you are facing problem when a download request is made by the maybe_download function call in base.py file.

There is a conflict in the permissions of the temporary files and I myself couldn't work out a way to change the permissions, but was able to work around the problem.

Do the following...

  • Download the four .gz files of the MNIST data set from the link ( http://yann.lecun.com/exdb/mnist/ )
  • Then make a folder names MNIST_data (or your choice in your working directory/ site packages folder in the tensorflow\examples folder).
  • Directly copy paste the files into the folder.
  • Copy the address of the folder (it probably will be ( C:\Python\Python35\Lib\site-packages\tensorflow\examples\tutorials\mnist\MNIST_data ))
  • Change the "\" to "/" as "\" is used for escape characters, to access the folder locations.
  • Lastly, if you are following the tutorials, your call function would be ( mnist = input_data.read_data_sets("MNIST_data/", one_hot=True) ) ; change the "MNIST_data/" parameter to your folder location. As in my case would be ( mnist = input_data.read_data_sets("C:/Python/Python35/Lib/site-packages/tensorflow/examples/tutorials/mnist/MNIST_data", one_hot=True) )

Then it's all done. Hope it works for you.

1

Another option is to ensure the file is not open anywhere else on your machine.

1
  • This only applies to Windows. It looks like this question is directed at Linux. There these restrictions do not exist
    – Dick Kniep
    Jan 3, 2019 at 13:52
1

supplementing @falsetru's answer : run id in the terminal to get your user_id and group_id

Go the directory/partition where you are facing the challenge. Open terminal, type id then press enter. This will show you your user_id and group_id

then type

chown -R user-id:group-id .

Replace user-id and group-id

. at the end indicates current partition / repository

// chown -R 1001:1001 . (that was my case)

1
  • Please add some more detail so that your answer stands on it's own. Nov 1, 2020 at 5:27
0

Simply try:

sudo cp /source /destination
0
0

Just close the file in case it is opened in the background. The error disappears by itself

0

The solution that worked out for me here when I was using python 3 os package for performing operations on a directory where I didn't have sufficient permissions and access to got resolved by running the python file with sudo (root) i.e.:

sudo python python_file_name.py

Any other utility that you might also plan on using to chmod or chown that directory would also only work when you run it with sudo.

# file_name.py
base_path = "./parent_dir/child_dir/"
user = os.stat(base_path).st_uid # for getting details of the current user owner of the dir
group = os.stat(base_path).st_gid # for getting details of the current group owner of the dir

print("Present owner and group of the specified path")
print("Owner:", user)
print("Group:", group)
os.chown(base_path, user, group) # change directory permissions

print("\nOwner id of the file:", os.stat(base_path).st_uid)
print("Group id of the file:", os.stat(base_path).st_gid)
os.mkdir(base_path+file_name,mode=0o666)

run the above file with sudo.

sudo python file_name.py

Hope this answer works out for you.

Forever indebted to stackoverflow and the dev community. All hail the devs.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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