1

Currently, I do sudo chown -R ubuntu:ubuntu /home/ubuntu/<folder>.

But, when I run a Python script which would be creating a folder named testoutput and write files to it, the folder is created with the root permissions, and hence, getting permission denied error while writing files to the folder.

How do I ensure that even new files in the range get the same permissions when doing chown? Is there a way to ensure that in Ubuntu?

The permissions of the new folder i :

drwxr-xr-x 2 root   root

while the permission of the existing folders is:

drwxrwxr-x 2 ubuntu ubuntu
Dawny33
  • 10,543
  • 21
  • 82
  • 134
  • Possible duplicate of [How do you create in python a file with permissions other users can write](https://stackoverflow.com/questions/36745577/how-do-you-create-in-python-a-file-with-permissions-other-users-can-write) – Kallz Aug 17 '17 at 12:14
  • '''https://stackoverflow.com/questions/5624359/write-file-with-specific-permissions-in-python''' may this can help – Kallz Aug 17 '17 at 12:15

2 Answers2

2

In nearly all operating systems, the owner of a new file is determined by the user context from which the file is created. I.e. you are most likely running the repsective python script as root. If you recursively chown the base directory testoutput afterwards - as root of course - you shuld be fine. If you do not want root to be the initial owner, consider not running the respective script as root in the first place, but as the desired owner. Alternatively have a look at ACLs.

Richard Neumann
  • 2,986
  • 2
  • 25
  • 50
1

This is governed by umask setting which is set by default here to

root@brad:~# grep ^UMASK /etc/login.defs
UMASK           022
root@brad:~#

(though pam and other services for remote login etc pp have overrides).

With this we have:

root@brad:~# umask                 # four digits because of suid bit
0022
root@brad:~# mkdir tempDir1
root@brad:~# ls -ld tempDir1
drwxr-xr-x 2 root root 4096 Aug 17 07:22 tempDir1
root@brad:~# umask 002
root@brad:~# umask
0002
root@brad:~# mkdir tempDir2
root@brad:~# ls -ld tempDir?
drwxr-xr-x 2 root root 4096 Aug 17 07:22 tempDir1
drwxrwxr-x 2 root root 4096 Aug 17 07:22 tempDir2
root@brad:~#

And you see how changing the middle digit from 022 to 002 made the group permission writeable.

Dirk Eddelbuettel
  • 360,940
  • 56
  • 644
  • 725