使用python在linux中创建用户组

发布于 2024-08-07 15:11:09 字数 319 浏览 2 评论 0原文

我想在CentOS系统上使用python创建一个用户组。当我说“使用 python”时,我的意思是我不想做类似 os.system 的事情并给出 unix 命令来创建一个新组。我想知道是否有任何 python 模块可以处理这个问题。

在网上搜索并没有透露太多关于我想要的东西,除了 python 用户组..所以我不得不问这个。

我通过在 SO 上搜索了解了 grp 模块,但找不到有关创建组的任何信息。

编辑:我不知道是否必须为此提出一个新问题,但我也想知道如何将(现有)用户添加到新创建的组中。

任何帮助表示赞赏。 谢谢。

I want to create a user group using python on CentOS system. When I say 'using python' I mean I don't want to do something like os.system and give the unix command to create a new group. I would like to know if there is any python module that deals with this.

Searching on the net did not reveal much about what I want, except for python user groups.. so I had to ask this.

I learned about the grp module by searching here on SO, but couldn't find anything about creating a group.

EDIT: I dont know if I have to start a new question for this, but I would also like to know how to add (existing) users to the newly created group.

Any help appreciated.
Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

哥,最终变帅啦 2024-08-14 15:11:09

我不知道有一个 python 模块可以做到这一点,但是 /etc/group 和 /etc/gshadow 格式非常标准,所以如果你愿意,你可以打开文件,解析它们当前的内容,然后添加新组如果需要的话。

在执行此操作之前,请考虑:

  • 如果您尝试添加系统上已存在的组,会发生什么
  • 情况 当程序的多个实例尝试同时添加组时会发生
  • 什么情况 当发生不兼容的更改时,您的代码会发生什么情况对组格式进行了一些发布,包括
  • NIS、LDAP、Kerberos,...

如果您不愿意处理这些类型的问题,只需使用 subprocess 模块并运行 groupadd。损坏客户机器的可能性会大大降低。

您可以做的另一件事是,将代码包装在 Python 中的 groupadd.c(在影子包中)中,并以这种方式执行,这比您自己编写的代码更安全。不过,与仅仅执行它相比,我认为这不会给你带来太多好处,而且它会增加你的构建的复杂性和脆弱性。

I don't know of a python module to do it, but the /etc/group and /etc/gshadow format is pretty standard, so if you wanted you could just open the files, parse their current contents and then add the new group if necessary.

Before you go doing this, consider:

  • What happens if you try to add a group that already exists on the system
  • What happens when multiple instances of your program try to add a group at the same time
  • What happens to your code when an incompatible change is made to the group format a couple releases down the line
  • NIS, LDAP, Kerberos, ...

If you're not willing to deal with these kinds of problems, just use the subprocess module and run groupadd. It will be way less likely to break your customers machines.

Another thing you could do that would be less fragile than writing your own would be to wrap the code in groupadd.c (in the shadow package) in Python and do it that way. I don't see this buying you much versus just exec'ing it, though, and it would add more complexity and fragility to your build.

空气里的味道 2024-08-14 15:11:09

我认为您应该使用程序中的命令行程序,我们已经非常小心地确保在出现问题时它们不会破坏组文件。

然而,如果您选择这样做,文件格式非常简单,可以自己编写一些东西

I think you should use the commandline programs from your program, a lot of care has gone into making sure that they don't break the groups file if something goes wrong.

However the file format is quite straight forward to write something yourself if you choose to go that way

爱的十字路口 2024-08-14 15:11:09

没有图书馆要求创建组。这是因为实际上不存在创建群组这样的事情。 GID 只是分配给进程或文件的编号。所有这些数字都已存在 - 您无需执行任何操作即可开始使用 GID。具有适当的权限,您可以调用 chown(2) 将文件的 GID 设置为任意数字,或调用 setgid(2) 设置当前进程的 GID(还有更多内容,使用有效 ID,补充 ID 等)。

在基本 Unix/Linux/POSIX 系统上,为 GID 命名是通过 /etc/group 中的条目完成的,但这实际上只是 Unix/Linux/POSIX 用户区工具遵守的约定。正如 Jack Lloyd 所提到的,还存在其他基于网络的目录。

手册页 group(5) 描述了 /etc/group 文件的格式,但不建议您直接写入该文件。您的发行版将具有关于如何分配未命名 GID 的策略,例如为不同目的(固定系统组、动态系统组、用户组等)保留某些空间。这些数字空间的范围在不同的分布上是不同的。这些策略通常编码在系统管理员用来分配未命名 GID 的命令行工具中。

这意味着在本地添加组的最佳方法是使用命令行工具。

There are no library calls for creating a group. This is because there's really no such thing as creating a group. A GID is simply a number assigned to a process or a file. All these numbers exist already - there is nothing you need to do to start using a GID. With the appropriate privileges, you can call chown(2) to set the GID of a file to any number, or setgid(2) to set the GID of the current process (there's a little more to it than that, with effective IDs, supplementary IDs, etc).

Giving a name to a GID is done by an entry in /etc/group on basic Unix/Linux/POSIX systems, but that's really just a convention adhered to by the Unix/Linux/POSIX userland tools. Other network-based directories also exist, as mentioned by Jack Lloyd.

The man page group(5) describes the format of the /etc/group file, but it is not recommended that you write to it directly. Your distribution will have policies on how unnamed GIDs are allocated, such as reserving certain spaces for different purposes (fixed system groups, dynamic system groups, user groups, etc). The range of these number spaces differs on different distributions. These policies are usually encoded in the command-line tools that a sysadmin uses to assign unnamed GIDs.

This means the best way to add a group locally is to use the command-line tools.

魂牵梦绕锁你心扉 2024-08-14 15:11:09

如果您正在研究 Python,请尝试这个程序。使用起来相当简单,并且可以轻松自定义代码 http://aleph -null.tv/downloads/mpb-adduser-1.tgz

If you are looking at Python, then try this program. Its fairly simple to use, and the code can easily be customized http://aleph-null.tv/downloads/mpb-adduser-1.tgz

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文