如何按名称更改目录的用户和组权限?

时间:2021-08-28 16:44:14

os.chown is exactly what I want, but I want to specify the user and group by name, not ID (I don't know what they are). How can I do that?

操作系统。chown正是我想要的,但是我想按名称指定用户和组,而不是按ID(我不知道它们是什么)。我怎么做呢?

4 个解决方案

#1


85  

import pwd
import grp
import os

uid = pwd.getpwnam("nobody").pw_uid
gid = grp.getgrnam("nogroup").gr_gid
path = '/tmp/f.txt'
os.chown(path, uid, gid)

#2


19  

Since Python 3.3 https://docs.python.org/3.3/library/shutil.html#shutil.chown

因为Python 3.3 https://docs.python.org/3.3/library/shutil.html shutil.chown

import shutil
shutil.chown(path, user=None, group=None)

Change owner user and/or group of the given path.

更改给定路径的所有者用户和/或组。

user can be a system user name or a uid; the same applies to group.

用户可以是系统用户名或uid;同样的道理也适用于群体。

At least one argument is required.

至少需要一个参数。

Availability: Unix.

可用性:Unix。

#3


2  

Since the shutil version supports group being optional, I copy and pasted the code to my Python2 project.

由于shutil版本支持组是可选的,所以我将代码复制并粘贴到我的Python2项目中。

https://hg.python.org/cpython/file/tip/Lib/shutil.py#l1010

https://hg.python.org/cpython/file/tip/Lib/shutil.py l1010

def chown(path, user=None, group=None):
    """Change owner user and group of the given path.

    user and group can be the uid/gid or the user/group names, and in that case,
    they are converted to their respective uid/gid.
    """

    if user is None and group is None:
        raise ValueError("user and/or group must be set")

    _user = user
    _group = group

    # -1 means don't change it
    if user is None:
        _user = -1
    # user can either be an int (the uid) or a string (the system username)
    elif isinstance(user, basestring):
        _user = _get_uid(user)
        if _user is None:
            raise LookupError("no such user: {!r}".format(user))

    if group is None:
        _group = -1
    elif not isinstance(group, int):
        _group = _get_gid(group)
        if _group is None:
            raise LookupError("no such group: {!r}".format(group))

    os.chown(path, _user, _group)

#4


-2  

You can use id -u wong2 to get the uid of a user
You can do this with python:

您可以使用id -u wong2获取用户的uid,您可以使用python实现:

import os 
def getUidByUname(uname):
    return os.popen("id -u %s" % uname).read().strip()

Then use the id to os.chown

然后使用id到os.chown

#1


85  

import pwd
import grp
import os

uid = pwd.getpwnam("nobody").pw_uid
gid = grp.getgrnam("nogroup").gr_gid
path = '/tmp/f.txt'
os.chown(path, uid, gid)

#2


19  

Since Python 3.3 https://docs.python.org/3.3/library/shutil.html#shutil.chown

因为Python 3.3 https://docs.python.org/3.3/library/shutil.html shutil.chown

import shutil
shutil.chown(path, user=None, group=None)

Change owner user and/or group of the given path.

更改给定路径的所有者用户和/或组。

user can be a system user name or a uid; the same applies to group.

用户可以是系统用户名或uid;同样的道理也适用于群体。

At least one argument is required.

至少需要一个参数。

Availability: Unix.

可用性:Unix。

#3


2  

Since the shutil version supports group being optional, I copy and pasted the code to my Python2 project.

由于shutil版本支持组是可选的,所以我将代码复制并粘贴到我的Python2项目中。

https://hg.python.org/cpython/file/tip/Lib/shutil.py#l1010

https://hg.python.org/cpython/file/tip/Lib/shutil.py l1010

def chown(path, user=None, group=None):
    """Change owner user and group of the given path.

    user and group can be the uid/gid or the user/group names, and in that case,
    they are converted to their respective uid/gid.
    """

    if user is None and group is None:
        raise ValueError("user and/or group must be set")

    _user = user
    _group = group

    # -1 means don't change it
    if user is None:
        _user = -1
    # user can either be an int (the uid) or a string (the system username)
    elif isinstance(user, basestring):
        _user = _get_uid(user)
        if _user is None:
            raise LookupError("no such user: {!r}".format(user))

    if group is None:
        _group = -1
    elif not isinstance(group, int):
        _group = _get_gid(group)
        if _group is None:
            raise LookupError("no such group: {!r}".format(group))

    os.chown(path, _user, _group)

#4


-2  

You can use id -u wong2 to get the uid of a user
You can do this with python:

您可以使用id -u wong2获取用户的uid,您可以使用python实现:

import os 
def getUidByUname(uname):
    return os.popen("id -u %s" % uname).read().strip()

Then use the id to os.chown

然后使用id到os.chown