linux user group permission

最後更新: 2015-12-15

目錄

  • grp modules
  • os modules
  • pwd modules

OS

 

function

  • chmod
  • chown
  • grp

os.chmod(path, mode)

os.chown

os.chown(path, uid, gid)

To leave one of the ids unchanged, set it to -1.

import os

os.chown('/root/test/testfile', 1001, -1)

當 file 不存在時

OSError: [Errno 2] No such file or directory: '/root/test/test.txt'

 


grp modules

 

DOC: https://docs.python.org/2/library/grp.html

import grp

grp.getgrgid(gid)

grp.getgrnam(name)

# a list of all available group entries, in arbitrary order

grp.getgrall()
# 0     gr_name
# 1     gr_passwd
# 2     gr_gid
# 3     gr_mem

gid.gr_gid

 


pwd modules

 

Doc: https://docs.python.org/2/library/pwd.html

import pwd

# 
pwd.getpwuid(uid)

# Return the password database entry for the given user name.
pwd.getpwnam(name)

# Return a list of all available password database entries, in arbitrary order.
pwd.getpwall()

# Index
# 0     pw_name
# 1     pw_passwd
# 2     pw_uid
# 3     pw_gid
# 4     pw_gecos    # comment field
# 5     pw_dir
# 6     pw_shell

return a list: []
 


 

 

 

 

Creative Commons license icon Creative Commons license icon