Question

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?

Was it helpful?

Solution

I can see two options:

  1. 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 the jenkins was created. Be careful when deleting the jenkins user to make sure you don't delete original-user.

  2. More kludgy: change the UID of one of the users by manually editing /etc/passwd, then use chown -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.

OTHER TIPS

On Mac OS X, you can change a UID of a local user using System Preferences > Users & Groups.

Open the padlock there to allow changes to be made and then right-click on the user you like to change.

NOTE: You should not be logged in to that account at the time you do the change.

Also, POSIX permissions will follow the UID, so after changing it you may need to change the user ownership for that home folder or you would face not to be able to log in to the changed account.

Command for changing ownership would be:

sudo chown -R <account_name> /path/to/userhomefolder
Licensed under: CC-BY-SA with attribution
Not affiliated with apple.stackexchange
scroll top