I have added a new user in Ubuntu using puppet with a hard coded uid of 10017. The uids must be the same across workstations so that when user data is restored everything lines up.

Now I find out that uid is not unique. Mysql is using it. How can I test a server to find out if a uid (10018) exists?

  • 1
    It is uncommon for a system account to use a number as high as 10017, how was this mysql account created in the first place? – dawud Apr 6 '15 at 21:51
  • Why does the uid need to be hard coded? does this provide any insight on creating/using users with puppet? serverfault.com/questions/420749/… – hookenz Apr 6 '15 at 22:20
  • 4
    why must you use a uid though.. you can use the username in place of the uid in puppet.. i've found that hard coding uid/gid leads to a giant mess. – Mike Apr 6 '15 at 22:50
  • 2
    you wanted 10017 ... you got 10017 ... is that what you really wanted? – Skaperen Apr 7 '15 at 8:44
  • 1
    it needs to be hardcoded because we have users who use different filesystems on different machines. If the uuids don't line up, when we restore the data, we're not sure who owns what – markhorrocks Apr 9 '15 at 15:52

Answering the specific question (how do I tell if a uid is in use) ...

getent passwd 1234

Will exit with 0 if uid is in use and 2 if not. As Mark says it will also output any matching entries. When using within a shell script I'd expect to discard the output and just test the exit code.

Note that using getent has the advantage of any techniques that look at /etc/passwd directly because it will also consult any other data sources you have configured (NIS, LDAP etc).

  • 1
    It actually returns nothing or the user record. – markhorrocks Apr 7 '15 at 10:51
cat /etc/passwd | awk -F: '{print $3}' | sort -n
  • a colleague gave me this, I prefer it as it gives me a sorted list without trial and error but the above is a valid answer and so gets the points. Thanks Paul, – markhorrocks Apr 7 '15 at 8:34
  • 1
    An alternative to this is sort -t: --key=3 -n /etc/passwd which shows all entries from passwd file ordered by uid – Paul Haldane Apr 7 '15 at 10:58
  • Since you only need to know about one ID, you could just cat /etc/passwd | awk -F: '{print $3}' | grep -q '^10017$' && echo yes || echo no skip the sort and silently test for the id, – Jesse Chisholm Mar 17 '20 at 17:06

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.