fix a missing user account in leopard


My friend was over yesterday and decided that he would upgrade his macbook to osx leopard (10.5). I'm not sure how it happened but it seems that something got on the dvd and caused a read error and a failed upgrade. After cleaning off the disk, the upgrade completed successfully. The only problem was that his user seemed to be gone. All of his attempts with his various passwords failed. He looked fairly dejected at being locked out of his now leopardized laptop.

The first thing we did to troubleshoot was boot the macbook into target disk mode (hold down T when powering it on). Looking around the filesystem revealed that his user folder still existed in /Users, with the username we expected.

Our next attempt was single user mode (hold down apple-S when powering on). Single user mode drops you into a root shell with readonly access to the filesystem. So we did the following:

fsck -yf 
mount -uw /
launchctl load /System/Library/LaunchDaemons/com.apple.DirectoryServices.plist 
launchctl load /System/Library/LaunchDaemons/com.apple.DirectoryServicesLocal.plist 
passwd #this will change root's password, you will be prompted to enter it twice
dscl
That last command will load the directory services command line app (they removed netinfo manager in leopard, otherwise you could easily reboot and fix the problem using a gui from the root account). At the dscl prompt we entered the following:
list Local/Default/Users
create Local/Default/Users/<your_old_username>
create Local/Default/Users/<your_old_username> UniqueID 501
create Local/Default/Users/<your_old_username> PrimaryGroupID 501
create Local/Default/Users/<your_old_username> NFSHomeDirectory /Users/<your_old_username>
create Local/Default/Users/<your_old_username> UserShell /bin/bash
create Local/Default/Users/<your_old_username> RealName 
After that was complete we rebooted the machine by typing shutdown -r now. When the machine booted up again, my friend was able to log into his account. All of his settings had been preserved since they all live in your home directory. The only exceptions were his user account picture, full name and password.

I hope someone else finds this useful, it took a good bit a of playing around to figure this out.