【问题标题】:PermissionError: exception when running an os.chmod() with sudo. Python3 3PermissionError:使用 sudo 运行 os.chmod() 时出现异常。蟒蛇3 3
【发布时间】:2023-03-18 17:13:01
【问题描述】:

我正在尝试更改文件或目录的所有权。当我将函数移动到一个单独的文件时,它可以完美地工作并且所有权发生了变化。当我尝试将它合并到一个大型脚本中时,它会出错并出现以下异常。我以 sudo 身份运行脚本。

Traceback (most recent call last):
  File "mount_py3.py", line 206, in <module>
    main()
  File "mount_py3.py", line 197, in main
    own_recursive(mount_point)
  File "mount_py3.py", line 112, in own_recursive
    os.chown(mtpt, uid, gid)
PermissionError: [Errno 1] Operation not permitted: '/srv/general'

目录列表显示以下内容。

$ ll /srv
total 8
drwxr-xr-x  3 root root 4096 Mar 21 06:01 ./
drwxr-xr-x 26 root root 4096 Mar 22 14:01 ../
drwxrwxrwx  2 root root   39 Mar 14 01:44 general/

我实际上在两个单独的区域中调用 os.chmod,并以交互方式获取所有者和组的名称。这是一些相关的代码。

        while True:
            inp_user = input("Enter the name of the default group:  ")

            try:
                uid = pwd.getpwnam(inp_user)[2]

            except KeyError:
                print("User name does not exist on this system. ")
                print("Exiting.  ")
                continue

            break

        while True:
            inp_group = input("Enter the default group name:  ")

            try:
                gid = grp.getgrnam(inp_group)[2]

            except KeyError:
                print("Group name does not exist on this system. ")
                print("Try again.  ")
                continue

            break

        os.setuid(0)
        os.chown(mtpt, uid, gid)

我期待一些不太相关的代码,但这就是失败的原因。

【问题讨论】:

  • 这能回答你的问题吗? How to execute os.* methods as root?
  • Samy - 命令将传递我的帐户 UID 和 GID。主要为此,我使用自己的用户和组集来支持 DBA。当我将 os.chown 函数移动到它自己的 python 文件时,它的效果很好。

标签: python


【解决方案1】:

放弃这个帖子。开了一个新的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-11-02
    • 1970-01-01
    • 2018-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-27
    相关资源
    最近更新 更多