5

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?

MDMoore313
  • 5,531
  • 6
  • 34
  • 73
markhorrocks
  • 471
  • 2
  • 9
  • 24
  • 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 06 '15 at 21:51
  • Why does the uid need to be hard coded? does this provide any insight on creating/using users with puppet? http://serverfault.com/questions/420749/puppet-get-users-home-directory – hookenz Apr 06 '15 at 22:20
  • This puppet runs on 30 odd servers and I *must* hard code the uid. – markhorrocks Apr 06 '15 at 22:25
  • 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 06 '15 at 22:50
  • 2
    you **wanted** 10017 ... you **got** 10017 ... is that what you __really__ wanted? – Skaperen Apr 07 '15 at 08:44
  • @Mike I'm guessing something to do with NFS.... – fukawi2 Apr 08 '15 at 02:22
  • 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 09 '15 at 15:52
  • @dawud has hit on the most *correct* part of this question. Who created mysql with such a high id? Do you actually have a collision internally somewhere in your puppet manifests? – Scott Pack Apr 11 '15 at 14:00
  • Yes, there was a collision but i resolved it using the answer below. – markhorrocks Apr 13 '15 at 17:53

2 Answers2

13

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).

Paul Haldane
  • 4,457
  • 1
  • 20
  • 31
2
cat /etc/passwd | awk -F: '{print $3}' | sort -n
markhorrocks
  • 471
  • 2
  • 9
  • 24
  • 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 07 '15 at 08: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 07 '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