I accidentally created a user with a duplicate UID. I need advice on how to fix this predicament.
As you can see, running dscl shows two users with the same uid:
dscl . -list /Users uid | sort -nrk 2
jenkins 9999
original-user 9999
This happened because I was tweaking a package installer script and incorrectly assigned an existing UniqueId when creating a new user. The UniqueId matches the uid for my own account. Now when I login to OSX (with my own account), OSX has lost all of my profile settings (ie. Finder shows home for the new user instead of my own, browser has lost my history, etc).
Although OSX shows the profile for the new user, Terminal correctly recognizes the user that I logged in as. And therefore I'm able to confirm my old files (ie. Document, Downloads) still exist intact.
This is what I ran to get myself in this situation:
uid=9999
gid=$uid
while dscl -search /Groups gid $gid | grep -q $gid; do
echo "gid $gid is not free, trying next"
gid=$(($gid + 1))
done
echo "Using gid $gid for jenkins"
dscl . -create /Groups/jenkins PrimaryGroupID $gid
dscl . -create /Users/jenkins UserShell /bin/bash
dscl . -create /Users/jenkins Password '*'
dscl . -create /Users/jenkins UniqueID $uid
dscl . -create /Users/jenkins PrimaryGroupID $gid
dscl . -create /Users/jenkins NFSHomeDirectory "$JENKINS_HOMEDIR"
dscl . -append /Groups/jenkins GroupMembership jenkins
How can I recover?
Best Answer
I can see two options:
Delete the
jenkins
user, create it again (with a unique UID this time), then repeat all the commands you used to create its files. This is probably the best option if you haven't created any extra data or customization since thejenkins
was created. Be careful when deleting thejenkins
user to make sure you don't deleteoriginal-user
.More kludgy: change the UID of one of the users by manually editing
/etc/passwd
, then usechown -R
to change the ownership of that user's files appropriately. This is kludgy and might work but also might not work.I'd go with option #1, personally.