Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I've been given a partially-complete RPM spec for a service we're writing. It gets as far as making the required directories, copying files, setting permissions, etc., but it doesn't make the required system account that the service will run under. I was told that it's best for the RPM to take care of this, so I've added

Requires(pre): /usr/sbin/useradd

useradd -r -d /path/to/program -s /bin/false myservice

This succeeds in making the user account (and associated group), so later on when it tries to set ownership / permissions on the service's files, that succeeds as well.

My current problem is, a) if the user account already exists, the RPM install fails because useradd fails (because the user already exists); and b) I don't know how to have rpm -e myservice also remove the associated user and group.

share|improve this question
// , Would you consider using FPM? – Nathan Basanese Sep 3 '15 at 23:57
up vote 12 down vote accepted

I actually solved this independently, by looking at other RPM specs that did similar things. If you just want to add a user (conditionally), use Ignacio's link. I did this:

Requires(pre): /usr/sbin/useradd, /usr/bin/getent
Requires(postun): /usr/sbin/userdel

/usr/bin/getent group myservice || /usr/sbin/groupadd -r myservice
/usr/bin/getent passwd myservice || /usr/sbin/useradd -r -d /path/to/program -s /sbin/nologin myservice

/usr/sbin/userdel myservice

This makes sure that the RPM "cleans up after itself" but still provides the ability to install even if the account already exists.

share|improve this answer
Although this answers the question, it is worth reading the note in the Fedora link link posted by Ignacio about why removing the user/group is not desirable. – CoverosGene Feb 28 '12 at 16:18
There is an UID and GID reuse issue (when the deleted user has the highest UID/GID), that makes any automated use of userdel a bad idea. – Bruno9779 Nov 16 '15 at 15:03
On my CentOS 6.7 I removed the /usr/sbin/groupadd command since the useradd command will create the group itself. Also the useradd will exit with an error when a group of the same name already exists. – Raffael Jun 1 at 15:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .