Is there a way to set a (new) user's primary group in Cygwin's /etc/passwd file without hand-editing the file?

I have a local group set up for members of the Dev Team on a Windows Server 2008 R2 box so that we can all modify a particular group files, but the regular users can only read them. As some of the work we do uses scripts that rely on Cygwin tools, this group is also in the /etc/group file.

When I need to add a new user to the "Dev Team" group, I add them in Server Manager, and then use mkpasswd to add that user to Cygwin's /etc/passwd file. Unfortunately, they get the regular Domain Users group assigned as their primary group and I then have to go in and edit the passwd file to change the group.

I now need to write some instructions for someone else who is not au fait with UNIX/Linux/Cygwin so that they can set up new Dev Team users and obviously "hand editing" /etc/passwd is a recipe for disaster if you don't know what you're doing.

So, is there a way of getting mkpasswd to set a different primary group, or another tool like Linux's usermod which can be used for the purpose of changing the group in a more controlled manner?


OK, here's the solution I'm working with so far, using awk in a shell script:

# Extract group number for group from /etc/group
GROUP=$(grep "^${GROUPNAME}:" /etc/group | awk -F: '{print $3}')

# Create passwd entry for user and substitute extracted group number
# before adding to /etc/passwd
mkpasswd -d "${DOMAIN}" -u "${USER}" | awk -F: '{print $1":"$2":"$3":'${GROUP}':"$5":"$6":"$7}' >> /etc/passwd

(suggestions for improvements welcome - I'm not an awk guru)

It'd still be nice to find out (if) there's a "proper" way to do it, though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.