Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Store a user's Home folder on a USB drive System
This hint is for you if you want to put your home folder on a USB flash drive to take between Macs, or maybe you are just interested in the novelty of being able to carry all of your 'stuff' around. Please note, this hint is not for the faint of heart.

The following instructions make it so you can take your home folder with you wherever you go. All you need is an admin password on each machine you want to use it on and, of corse, a flash drive. The roomier the flash drive the better, but I got this to work on a 128 megabyte flash drive. I won't be able to store much music or many pictures in this home folder (because of the size), but it would be great for getting to my documents and settings anywhere. I have a friend who has a 1-gigabyte flash drive. He would be able to use this for music, pictures, and maybe even small video files.

To see how my hint relates to this earlier hint on moving your home folder to an external hard disk, see the information at the bottom of this hint.

Behavior:
When you login to your computer with your flash drive connected, your user environment will be loaded from it. When you login to your computer without your flash drive connected, your user environment will be what it has always been.

Warning:
With these modifications, it is possible to mess up your ability to login to your user account. While I don't think you can lose your data this way, you can certainly lose your ability to log on to the right place (when I was first figuring this out I had some moments of panic, myself). I recommend creating a new user to try this on first, before trying it on your normal user. Also, when your flash drive is connected, it will look as if all your data and settings are gone. Don't Panic, you can still access them at /Users/short_username on your hard disk (where short_username is your Mac OS X short user name). Or simply logout and remove your flash drive (you will probably have to shut down, because it could be dangerous for the data on your flash drive to remove it, if it should happen to be mounted). Once you get this working and are happy with it, then you can simply copy across whatever you want from your normal user to your flash drive's user.

Also, I don't know how this modification would behave if your Mac OS X short user name and passwords on the different machines don't match. Also-also, I don't know how the different filesystems behave, should you use something other than the DOS filesystem that comes with most flash drives. This is open for experimentation. There is also the slight potential for an error to develop among the workings of this hint. I did put a fail-safe mechanism in the script (it will try to remove empty folders), but this mechanism is untested. If the flash drive is removed after it is detected (during boot) and before you login, the Mac OS will think that you want it to create a new home folder inside the invisible Volumes folder. The solution to this is to remove the directories it creates inside the Volumes folder if the script, for some reason, does not. I have NOT tested this hint with FileVault, and it will probably cause problems if you try it with FileVault on!

Here is what you do:
First of all, all of my samples are for my system, so Justin is my short user name and ATTACHE is the name of my flash drive.
  1. Place the following script in /Library/Hooks/FlashDriveHome. You may have to create the folders Hooks and FlashDriveHome is the filename of the script. If you use TextEdit to make this script, make sure you choose Make Plain Text from the Format menu first. Here's the code:
    #! /bin/bash
    
    # This script checks to see if your flash drive is connected. If it is it will
    # change your home folder reference to the flash drive. If your flash
    # drive is not connected it will change the reference back to your
    # usual home folder.
    
    # Change 'Justin' to your Mac OS X short user name. (7 occurrences)
    # Change 'ATTACHE' to the name of your flash drive. (5 occurrences)
    # You can find the flash drive's name by typing 'df' in the terminal, but
    # this should be the same name you see in the Finder.
    
    # Remove empty directories that will confuse this script (this will not work
    # if the directories are not empty):
    rmdir /Volumes/ATTACHE/Users/Justin
    rmdir /Volumes/ATTACHE/Users
    rmdir /Volumes/ATTACHE
    
    # Check to see if your flash drive is connected (if there is a folder for
    # it in Volumes):
    if [ -s /Volumes/ATTACHE ]
    then
       niutil -destroyprop . /users/Justin home
       niutil -createprop . /users/Justin home /Volumes/ATTACHE/Users/Justin
    else
       niutil -destroyprop . /users/Justin home
       niutil -createprop . /users/Justin home /Users/Justin
    fi
    
  2. In System Preferences, under Accounts, make a new user called 'user template.' 'usertemplate' is fine for the short user name, and you don't need to worry about a password or anything else.

  3. Run these commands from a Terminal (you will be prompted for your admin password):
    sudo mv /Library/Hooks/FlashDriveHome.txt /Library/Hooks/FlashDriveHome
    sudo chmod +x /Library/Hooks/FlashDriveHome
    sudo /usr/bin/defaults write /Library/Preferences/com.apple.loginwindow LoginHook /Library/Hooks/FlashDriveHome
    sudo defaults write /Library/Preferences/SystemConfiguration/autodiskmount AutomountDisksWithoutUserLogin -bool true
    mkdir /Volumes/ATTACHE/Users/
    Some explanations:
    • Line one is only necessary if you used TextEdit to create the script.
    • Line two makes the script executable.
    • Line three makes the script run when the login window first appears -- as part of the boot up procedure.
    • Line four makes it so drives can be mounted when no one is logged in.
    • Line five creates a new Users folder in the hiddin /Volumes directory -- replace ATTACHE with the name of your flash drive.

  4. Note: before you do this next step, you may need to login as 'user template' to trash extra stuff to make room. For me, there wasn't enough room for the Garage Band demos, so this is what I did -- if you do and need to logout, make sure to unmount and remove your flash drive, as we have not finished setting it up yet! Also, aliases will be followed when copying this user to your flash drive.
    sudo cp -r /Users/usertemplate /Volumes/ATTACHE/Users/Justin
    sudo chown -R Justin /Volumes/ATTACHE/Users/Justin 
    Some explanations:
    • Line one will copy the user template to your flash drive under your user's short name. Replace ATTACHE with the name of your flash drive, and Justin with your Mac OS X's short user name.
    • Line two: make the same replacements here -- but it's only necessary if you have permissions enabled on your flash drive.

  5. With your flash drive connected, reboot, log out, then log back in. At this point, you can delete the user 'user template' if everything is working. I also recommend choosing a different desktop picture for your flash drive's user. That way, you can tell at a glance which home folder you are using.

  6. Copy the contents of your old Library/Keychains to the Library/Keychains in the flash drive's home folder (overwrite existing files).
This will work across a logout and log back in, but it always seems to behave more smoothly if you treat your flash drive as a non-hot-swappable device (shutting down the computer before connecting or removing the flash drive). If you set this up on another machine, you can skip the parts about making a user template and copying it to the flash drive, as you would have already done this. Go ahead and save something to your 'new' home folder (flash drive connected). Try this on another Mac: connect your flash drive and login, and you should see it there, as if your home folder was there to begin with!

This would be great if you have Macs at your work or school, so you can still get to all of your stuff (emails, Safari bookmarks, Documents, settings, etc). Good luck explaining why you need admin passwords in the terminal to the administrator, though.

I do have about two minor issues/quirks to report, but they are probably something one of you bright readers out there can figure out. But I think you'll all agree that they are not a big deal.
  1. On my machine, I had to replace the Finder sidebar icons with the appropriate ones (from my home folder on the flash drive, not those from 'user template') each time. A good way to get around this would be to drag your new home folder on your flash drive to your dock, and get to those things from there.
  2. For me, when the flash drive is first connected each time, I need to log out then back in.
To uninstall this modification:
  1. Very important: Remove your flash drive and reboot (you need to be on your normal user)
  2. Trash Library -> Hooks -> FlashDriveHome
Here is what I've tested this on: Attache 128-megabyte USB 2 flash drive formatted for DOS and an iBook G4 running 10.3.9. One other thing: I tested this with "ignored permissions" on the flash drive, but I don't think it matters. And, I have tried it with auto login on and off. Also, I got some information and the idea from the older hint linked above. That hint was set up to use an external hard drive (not a flash drive) for your user, but the computer wouldn't automatically determine which home folder to use. So, while my hint is similar, I've built upon the general concept of the earlier hint and I've added automation. I've also taken a different approach. The earlier hint uses the NetInfo Manager to change the home folder of a user, whereas I've written a script that determines what to change it to.

Enjoy!
    •    
  • Currently 3.20 / 5
  You rated: 5 / 5 (5 votes cast)
 
[24,627 views]  

Store a user's Home folder on a USB drive | 2 comments | Create New Account
Click here to return to the 'Store a user's Home folder on a USB drive' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Store a user's Home folder on a USB drive
Authored by: jstripli on Apr 29, '05 02:46:26PM

I have not tried it yet, however it seems to me a "safer" way may be to have the script mount the flash drive over the home folder when the drive is inserted. That way you don't risk getting locked out of the machine if the home folder location gets hosed.



[ Reply to This | # ]
Store a user's Home folder on a USB drive
Authored by: ptb@mac.com on Oct 29, '05 05:30:33PM
I have to simplified this script so fewer changes would need to be made from user to user. Unfortunately, after trying to login without the portable hard drive connected, a dialog appeared: You are unable to log in to the user account "user" at this time I'm giving up at this point, but wanted to share what I came up with in case it helps someone else.

#!/bin/bash

# PortableHome.sh ##################################################
# http://www.macosxhints.com/article.php?story=20050423203849332

# Change name for your portable storage device #####################
# To determine "name", in a Terminal window, type: /sbin/mount
# Look for the storage device name displayed after "/Volumes/".

name="Domicilium";

### You do not need to make any other modifications to this file ###


# Description ######################################################
# If you want to transport your home directory between any number of
# Macs using any portable storage device, this script makes it easy.
# Upon login, if the portable storage device is connected, it will
# load your user environment from the home directory stored there.
# Otherwise, it will use the home directory on the internal drive.

# Requirements #####################################################
# --> Account with administrator privileges to install and configure
# --> Portable storage device: iPod, FireWire, USB, or flash drive

# Recommendations ##################################################
# --> Create an additional user with admin privileges, as a backup
# --> Try this with a test account before switching your primary
# --> Shutdown before connecting or removing the storage device


# Initial one-time portable storage device setup ###################
# Connect the formatted portable storage device before continuing.
# To determine "devicename", in a Terminal window, type: /sbin/mount
# Look for the storage device name displayed after "/Volumes/".

# Type the following commands in a Terminal window, except:
# replace "devicename" with the appropriate storage device name,
# and replace "usernamehere" with your short user name.

#    /usr/bin/sudo /bin/mkdir -m 1775 "/Volumes/devicename/Users"

#    /usr/bin/sudo /usr/sbin/chown root:admin 
#       "/Volumes/devicename/Users"

#    /usr/bin/sudo /bin/cp -Rp 
#       "/System/Library/User Template/English.lproj" 
#       "/Volumes/devicename/Users/usernamehere"

#    /usr/bin/sudo /usr/sbin/chown -R usernamehere:staff 
#       "/Volumes/devicename/Users/usernamehere"


# Installation on each Mac #########################################
# As administrator save this file as: /Library/Hooks/PortableHome.sh

# Type the following commands in a Terminal window:

#    /usr/bin/sudo /bin/chmod +x /Library/Hooks/PortableHome.sh

#    /usr/bin/sudo /usr/bin/defaults write 
#       /Library/Preferences/com.apple.loginwindow LoginHook 
#       /Library/Hooks/PortableHome.sh

#    /usr/bin/sudo /usr/bin/defaults write 
#       /Library/Preferences/SystemConfiguration/autodiskmount 
#       AutomountDisksWithoutUserLogin -bool true


# Expected behavior ################################################
# Test if the portable storage device is connected and home exists.
# If so, change the defined location of home to the portable drive.
# Otherwise, change the defined location of home to internal drive.
#
# There is no need to do a -destroyprop first, because by definition
# with the -createprop switch, even if a named property already
# exists, it is overwritten.


# Variable substitutions ###########################################
path="/Volumes/${name}";
user=$1;


# Script ###########################################################
if [ -s "${path}/Users/${user}" ]
then
   /usr/bin/niutil -createprop . /users/${user} home ${path}/Users/${user}
else
   /usr/bin/niutil -createprop . /users/${user} home /Users/${user}
fi

exit 0


[ Reply to This | # ]