3

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.

2 Answers 2

9

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Amazing! Thanks for the tip. For the record, I had to do: os.umask(00000) os.mkdir('/mnt/tmp',01777) to get it to work right. Thanks again!
1

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.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.