#!/bin/sh # changeUID.sh # # # Created by Eddie Kelley on 2/27/07. # Copyright 2007 Kelley Computing. All rights reserved. # # This script will attempt to change a user's UID (UniqueID) to a provided UID # clear # get_user_name function - this prompts for the username get_user_name() { echo "Please enter short name of user who's UID you wish to modify:" read USERNAME echo "You have entered: $USERNAME - is this correct? (y/n)" read VERIFY_USERNAME if [ "$VERIFY_USERNAME" != "y" ]; then get_user_name else VERIFY_USER_EXISTS=`dscl . -search /users name $USERNAME | grep -w $USERNAME | wc -l` if [ $(($VERIFY_USER_EXISTS)) -eq 1 ]; then OLD_UID=`dscl . -read /users/$USERNAME UniqueID | sed -e s/UniqueID..//` get_new_UID else echo "Error: $USERNAME does not exist" get_user_name fi fi } # get_new_UID function - after getting the user's shortname, prompt for a new UID get_new_UID() { echo "Please enter the new UID for user: $USERNAME (Current UID: $OLD_UID)" read NEW_UID echo "You have entered: $NEW_UID - is this correct? (y/n)" read VERIFY_UID if [ "$VERIFY_UID" != "y" ]; then get_new_UID else VERIFY_UNIQUE_UID=`dscl . -search /users UniqueID $NEW_UID | grep -w $NEW_UID | wc -l` if [ $(($VERIFY_UNIQUE_UID)) -gt 0 ]; then echo "UID: $NEW_UID already exists!" get_new_UID else VERIFY_GID_EXISTS=`dscl . -search /groups name $USERNAME | grep -w $USERNAME | wc -l` if [ $((VERIFY_GID_EXISTS)) -eq 1 ]; then echo "A group with the name:$USERNAME was found." echo "Would you like to change its GID? (y/n)" read CHANGE_GID fi change_UID fi fi } repair_permissions() { # change ownership (chown) of the user's home directory to match the new UID echo "Fixing ownership for home directory at: /Users/$USERNAME..." if [ "$CHANGE_GID" = "y" ]; then `sudo chown -R $NEW_UID:$NEW_UID /Users/$USERNAME` else `sudo chown -R $NEW_UID /Users/$USERNAME` fi # find all files owned by this user (in /Applications, and /Library), and change their ownership to the new values echo "Finding files located in /Applications and /Library to repair permissions on (this may take a while)..." if [ "$CHANGE_GID" = "y" ]; then `sudo find /.Trashes /Applications /Library -group $OLD_UID -exec chgrp $NEW_UID {} \;` fi `sudo find /.Trashes /Applications /Library -user $OLD_UID -exec chown $NEW_UID {} \;` # change the name of a couple files that have the old UID in their name `sudo mv /.Trashes/$OLD_UID /.Trashes/$NEW_UID` } # change_UID function - this does the actual work change_UID() { echo "Are you sure that you want to change the UID for user: $USERNAME to: $NEW_UID ? (y/n)" read VERIFY_START if [ "$VERIFY_START" != "y" ]; then echo "User cancelled operation" exit 0; else # change the entry for PrimaryGroupID to the new GID for selected user's group if [ "$CHANGE_GID" = "y" ]; then echo "Updating PrimaryGroupID at path: /users/$USERNAME using dscl..." `sudo dscl . -create /groups/$USERNAME PrimaryGroupID $NEW_UID` VERIFY_GID_CHANGE=`sudo dscl . -read /groups/$USERNAME PrimaryGroupID | sed -e s/PrimaryGroupID..//` if [ "$VERIFY_GID_CHANGE" = "$NEW_UID" ]; then echo "The GID for group: $USERNAME was changed to: $NEW_UID" else echo "There was an error changing the GID for group: $USERNAME" fi fi # change the entry for gid to the new GID for selected user if [ "$CHANGE_GID" = "y" ]; then #if the gid of selected user matches the old uid, change it to the new one if [ "`sudo dscl . -read /users/$USERNAME gid | sed -e s/gid..//`" = "$OLD_UID" ]; then echo "Updating gid at path: /users/$USERNAME using dscl..." `sudo dscl . -create /users/$USERNAME gid $NEW_UID` VERIFY_GID_CHANGE=`sudo dscl . -read /users/$USERNAME gid | sed -e s/gid..//` if [ "$VERIFY_GID_CHANGE" = "$NEW_UID" ]; then echo "The GID for user: $USERNAME was changed to: $NEW_UID" else echo "There was an error changing the GID for user: $USERNAME" fi fi fi # repair file permissions for other files owned by this user repair_permissions # change the entry for UniqueID to the new UID for selected user echo "Updating UniqueID at path: /users/$USERNAME using dscl..." `sudo dscl . -create /users/$USERNAME UniqueID $NEW_UID` VERIFY_UID_CHANGE=`dscl . -read /users/$USERNAME UniqueID | sed -e s/UniqueID..//` if [ "$VERIFY_UID_CHANGE" = "$NEW_UID" ]; then echo "The UID for user: $USERNAME was changed from: $OLD_UID to: $NEW_UID" else echo "There was an error changing the UID for user: $USERNAME" fi # We have to reboot the computer because loginwindow gets confused echo "You should now restart the computer." fi } # main function - just call our get_user_name function to start the script main() { echo "changeUID" echo "" echo "This script will allow you to change a user's UniqueID (UID)" echo "Press Control-C at any time to abort" echo "" get_user_name } main