[Slackbuilds-users] uids/gids

B Watson yalhcru at gmail.com
Tue Mar 31 02:59:34 UTC 2020


On 3/30/20, Biff Eros <bifferos at gmail.com> wrote:

> I started off manually creating user/group creation scripts for
> Afterpkg, looking a bit like this:
> https://github.com/bifferos/afterpkg/blob/master/scripts/system/mongodb/before.sh

Interesting project... not sure I'd want to mix-and-match pip-installed
and SBo-installed stuff, but if someone does want to, your script looks
like it should handle it well.

The before.sh you linked does something horrible though. Nobody should
ever grep /etc/passwd and/or group to see if a user exists (what if
they're using LDAP authentication? NIS? In 15.0 we're going to have PAM,
what if they're using something we never even heard of before?)

Instead use getent. Like so:

---cut-here---
#!/bin/bash

# Create the mongo group
if getent group mongo >/dev/null ; then
  echo "mongo group exists, no need to add it"
else
  echo "Creating mongo group"
  groupadd -r -g 285 mongo
fi

# Create the mongo user
if getent passwd mongo >/dev/null ; then
  echo "mongo user exists, no need to add it"
else
  echo "Creating mongo user"
  useradd -u 285 -d /var/lib/mongodb -s /bin/false -g mongo mongo
fi
---cut-here---

Notice it's a pretty minor change: only the two "if grep" lines are
different. getent is is part of glibc, so it's guaranteed to be present
on any Slack box that's able to compile anything.

If you're generating the uid/gid creation scripts, I guess you only have
to change it in one place (the generator script).


More information about the SlackBuilds-users mailing list