Skip to content

Instantly share code, notes, and snippets.

@rwenz3l
Created May 30, 2017 06:40
Show Gist options
  • Save rwenz3l/96d7a3be20d1d03f42fbf577b1603036 to your computer and use it in GitHub Desktop.
Save rwenz3l/96d7a3be20d1d03f42fbf577b1603036 to your computer and use it in GitHub Desktop.
#! /bin/sh
#
# Credit to: www.byan-roper.org
#
#
# 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/chown
PATH=/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
if dscl . -ls /Users | egrep -v '_.*|com.apple.*|daemon|root|nobody' | grep $USERNAME
then
: # USERNAME is a valid user-name
else
echo "'$USERNAME' is not a valid user on this system" >&2
exit 1
fi
if [ `whoami` = "$USERNAME" ]
then
echo "You cannot run this script from the '$USERNAME' account." >&2
echo "You must run this script from a different account." >&2
exit 1
fi
if users | grep -q $USERNAME
then
echo "'$USERNAME' is logged in. '$USERNAME' must be logged out to run this script." >&2
exit 1
fi
# 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 '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 read
do
# use bash-ism REPLY to accommodate spaces in the file name
chflags nouchg "$REPLY"
chown -h $NEW_UID "$REPLY"
chflags uchg "$REPLY"
done
if [ -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"
done
fi
# Now change ownership of the unlocked files.
echo "chown /Users/$USERNAME"
find -x /Users/$USERNAME -user $OLD_UID -print0 | xargs -0 chown -h $NEW_UID
if [ -d /Users/Shared ]
then
echo "chown /Users/Shared"
find -x /Users/Shared -user $OLD_UID -print0 | xargs -0 chown -h $NEW_UID
fi
echo "chown /Library"
find -x /Library -user $OLD_UID -print0 | xargs -0 chown -h $NEW_UID
echo "chown /Applications"
find -x /Applications -user $OLD_UID -print0 | xargs -0 chown -h $NEW_UID
echo "chown /usr"
find -x /usr -user $OLD_UID -print0 | xargs -0 chown -h $NEW_UID
echo "chown /private/etc"
find -x /private/etc -user $OLD_UID -print0 | xargs -0 chown -h $NEW_UID
echo "chown /private/var"
find -x /private/var -user $OLD_UID -print0 | xargs -0 chown -h $NEW_UID
echo "chown /.DocumentRevisions-V100"
find -x /.DocumentRevisions-V100 -user $OLD_UID -print0 | xargs -0 chown -h $NEW_UID
echo "chown /.MobileBackups"
find -x /.MobileBackups -user $OLD_UID -print0 | xargs -0 chown -h $NEW_UID
sync
# Finally, change the UID of the user.
echo "changing $USERNAME UniqueID to $NEW_UID"
dscl . -change /Users/$USERNAME UniqueID $OLD_UID $NEW_UID
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment