Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python creating tmp directory permissions

Tags:

python

linux

I'm using a python script to create a copy of a linux filesystem. I'm having trouble with the permissions on the created /tmp directory. The /tmp directory should have 1777 permissions, i.e.:

ls -l /
drwxrwxrwt  17 root     root 16384 2011-03-01 09:50 tmp

when I do the following,

os.mkdir('/mnt/tmp',1777)

I get strange permissions:

ls -l /
d-wxr----t 2 root root  4096 2011-03-01 09:53 tmp

Then I wondered about umask and chmod, so I tried this:

os.mkdir('/mnt/tmp')
old_mask=os.umask(0000)
os.chmod('/mnt/tmp',1777)
os.umask(old_mask)

but I still get unexpected permissions:

ls -l /
d-wxrwS--t 2 root root  4096 2011-03-01 09:57 tmp

However, what DOES give me the correct permissions of the created directory is the following:

os.mkdir('/mnt/tmp')
os.system("chmod 1777 /mnt/tmp")

I should note that I'm running this script through sudo, but there is no mention of any umask settings in /etc/sudoers. Running it as the actual root user makes no difference. It is impossible to run it as a normal user, since I'm making a copy of the FS, which has to include the files accessible only to root.

Any ideas here? Any help would be greatly appreciated.

like image 662
Kyle Fransham Avatar asked Sep 29 '26 06:09

Kyle Fransham


2 Answers

You should provide the permissions as an octal number. In Python 2.x, simply use 01777 instead of 1777. In Python 3.x, use 0o1777.

like image 105
Sven Marnach Avatar answered Sep 30 '26 19:09

Sven Marnach


Your permission should be in octal (777 in octal is 511 in decimal).

In Python, like in C, 0555 is 555 in base 8 (octal). If you want 1777 in octal, use 01777 in your code.

like image 29
0x6adb015 Avatar answered Sep 30 '26 19:09

0x6adb015



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!