sudo python PermissionError [Errno 13] Permission denied

Pascal patatetom at gmail.com
Thu Dec 17 05:17:37 EST 2020


hi,

here, I have this simple script that tests if the /tmp/test file can be
opened in write mode :

$ cat /tmp/append
#!/usr/bin/python
with open('/tmp/test', 'a'): pass

the file does not exist yet :

$ chmod +x /tmp/append
$ ls -l /tmp/test
ls: cannot access '/tmp/test': No such file or directory

the script is launched as a simple user :

$ /tmp/append
$ ls -l /tmp/test
-rw-r--r-- 1 user user 0 Dec 17 10:30 /tmp/test

everything is ok.
now, the script fails if it is replayed as root user with the sudo command :

$ sudo /tmp/append
[sudo] password for user:
Traceback (most recent call last):
  File "/tmp/append", line 2, in <module>
    with open('/tmp/test', 'a'):
PermissionError: [Errno 13] Permission denied: '/tmp/test'

the problem is the same if the opening mode is 'w' or if "sudo -i" or "su -"
are used.

why can't root user under python manipulate the simple user file ?

regards, lacsaP.


More information about the Python-list mailing list