Home‎ > ‎Server config‎ > ‎

OSX Change user's UID

#! /bin/sh# actually uses some bash extensions; see comment on line 36# # change the UID of a login on Mac OS X 10.11 El Capitan## run this script using sudo *from another user account*!!!# after logging out of the account to be changed.## Usage: sudo change-uid <user-name> <new-UID># # sbin is in the path to pick up /usr/sbin/chownPATH=/usr/bin:/bin:/usr/sbin:/sbin# Two arguments: the user name and the new UID.case $# in	2)	USERNAME=$1 ; NEW_UID=$2 ;;	*)	echo 'Usage: change-uid <user-name> <new-UID>' 1>&2; exit 1 ;;esac# Obtain the current UID for the specified user.OLD_UID=`dscl . -read /Users/$USERNAME UniqueID | cut -d ' ' -f2`# Validate that the new UID is a string of numbers.case $NEW_UID in	(*[!0-9]*|'')	echo 'Error: new UID is not numeric.\nUsage: change-uid <user-name> <new-UID>'1>&2; exit 1 ;;	(*)		echo "changing $USERNAME UID from $OLD_UID to $NEW_UID" ;;esac# First change ownership of the locked files.# Otherwise, the find for the not-locked files will complain# when it fails to change the UID of the locked files.echo "chown uchg files in /Users/$USERNAME"find -x /Users/$USERNAME -user $OLD_UID -flags uchg -print | while readdo	# use bash-ism REPLY to accommodate spaces in the file name	chflags nouchg "$REPLY"	chown -h $NEW_UID "$REPLY"	chflags uchg "$REPLY"doneif [ -d /Users/Shared ]then	echo "chown uchg files in /Users/Shared"	find -x /Users/Shared -user $OLD_UID -flags uchg -print | while read	do		chflags nouchg "$REPLY"		chown -h $NEW_UID "$REPLY"		chflags uchg "$REPLY"	donefi# Now change ownership of the unlocked files.echo "chown /Users/$USERNAME"find -x /Users/$USERNAME -user $OLD_UID -print0 | xargs -0 chown -h $NEW_UIDif [ -d /Users/Shared ]then	echo "chown /Users/Shared"	find -x /Users/Shared -user $OLD_UID -print0 | xargs -0 chown -h $NEW_UIDfiecho "chown /Library"find -x /Library -user $OLD_UID -print0 | xargs -0 chown -h $NEW_UIDecho "chown /Applications"find -x /Applications -user $OLD_UID -print0 | xargs -0 chown -h $NEW_UIDecho "chown /usr"find -x /usr -user $OLD_UID -print0 | xargs -0 chown -h $NEW_UIDecho "chown /private/etc"find -x /private/etc -user $OLD_UID -print0 | xargs -0 chown -h $NEW_UIDecho "chown /private/var"find -x /private/var -user $OLD_UID -print0 | xargs -0 chown -h $NEW_UIDecho "chown /.DocumentRevisions-V100"find -x /.DocumentRevisions-V100 -user $OLD_UID -print0 | xargs -0 chown -h $NEW_UIDecho "chown /.MobileBackups"find -x /.MobileBackups -user $OLD_UID -print0 | xargs -0 chown -h $NEW_UID# Next rename files that contain the users UID in their name.echo "rename files"find -dx /.DocumentRevisions-V100 /.MobileBackups \	/Library /private/var /System/Library/Caches /var \	-regex ".*[^0-9]${OLD_UID}[^0-9/]*" \	-print | while readdo	DIR=`dirname "$REPLY"`	FILE=`basename "$REPLY"`	NEW_FILE=`echo "$FILE" | sed -e s/$OLD_UID/$NEW_UID/g`	mv "$REPLY" "$DIR"/"$NEW_FILE"donesync# Finally, change the UID of the user.echo "changing $USERNAME UniqueID to $NEW_UID"dscl . -change /Users/$USERNAME UniqueID $OLD_UID $NEW_UID