Creating a user group in Linux using python

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

A web search did not reveal much about what I want except python user groups .. so I had to ask about it.

I found out about the grp module by doing a search here on SO, but couldn't find anything about creating a group.

EDIT . I donโ€™t know if I should 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. Thanks.

+6
python linux usergroups
source share
4 answers

I do not know about the python module for this, but the formats / etc / group and / etc / gshadow are pretty standard, so if you want, you can just open the files, analyze their current contents and then add a new group if necessary.

Before you do this, think:

  • What happens if you try to add a group that already exists in 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 incompatible changes are made to the group format, a couple issues a line down.
  • NIS, LDAP, Kerberos, ...

If you do not agree with these problems, just use the subprocess module and run groupadd. It will be less likely to break your customers' cars.

Another thing you could do would be less fragile than writing your own code is to make the code in groupadd.c (in the shadow package) in Python and do it this way. I donโ€™t see you buying a lot, not just doing it, and that would add more complexity and fragility to your assembly.

+10
source share

I think that you should use programs from the command line from your program, a lot of care was taken to ensure that they did not break the groups file if something went wrong.

However, the file format is pretty straightforward to write something yourself if you decide to go this way

+5
source share

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

The GID name is provided by writing to / etc / group on the underlying Unix / Linux / POSIX systems, but this is really just the convention used in custom Unix / Linux / POSIX tools. Other network directories also exist, as Jack Lloyd mentioned.

The man page group (5) describes the format of the / etc / group file, but it is not recommended to write it directly. Your distribution will list policies for distributing unnamed GIDs, for example, reserving certain spaces for different purposes (fixed system groups, dynamic system groups, user groups, etc.). The range of these number spaces differs from different distributions. These policies are typically encoded in command line tools that sysadmin uses to assign unnamed GIDs.

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

+1
source share

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

0
source share

All Articles