0

I'm writing a daemon in C which needs to do some things as a separate user from root. I will call this user "testuser". My program is installed by its makefile. My question is thus, when should this user testuser be added? I could add it at the end of the install target:

install:
        something
        something
        useradd -r testuser

however if the user is already added (for example by a previous install) this will fail. -useradd -r testuser would surpress this error but this seems like bad practice.

Also, another problem with doing it during make install would be if we are not actually installing the daemon for this system, e.g. make install DESTDIR=something.

Should I leave the useradd to the system administrator and simply make the daemon fail with an error message if the user is not present? What are the conventions?

2 Answers 2

0

You could just simply make an if statement : if the user exists already, you pass else you create it. Also, i don't see why you would create the same user for multiple modules. Every install as you call it should have a unique user associated to it.

2
  • I don't think it would be nice to make a new user for each install. What if I have installed it once, and then want to update it. I don't want to fill up the system with a bunch of users. Commented Jul 4 at 16:07
  • I know right , that's why i mentionned to do a conditional statement . If the user already exists then no user will be added.
    – Rouuufa
    Commented Jul 4 at 16:22
0

You can look at /etc/passwd to see if the user exists:

grep --quiet '^testuser:` /etc/passwd
if [ $? -ne 0 ]; then
    useradd -r testuser
fi

If testuser exists on a system, their userid is the first colon-separated field of a line in /etc/pssswd. Read man 5 passwd. If grep doesn't find it, it quietly exits with a non-zero status ($?, or $STATUS), and you do the useradd.

Also testuser is a bad choice of userids. Choose something more package-specific. Any package could have a testuser. When I see a testuser in my password file, I delete it. Testing's over, this is a production system.

You must log in to answer this question.

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