Generating user home folder in Terminal

I'm exploring bash shell scripting (Terminal) to manage my routine tasks with and have one process that's proving challenging to reproduce in the command line. Occasionally I will need to recreate a Network User's home directory in order to start them with a "clean" account. This involves changing their home folder's location in the Server app (e.g. "None - Services Only"), renaming their old home folder to userName-old, and then setting the location back to its original. Server app then creates a new blank home folder for the user, with the correct internal directories, and I can begin copying over their personal files into it.

User uploaded file

But to save myself a bunch of clicking and typing, I want to execute this process via the command line. I'm fairly green at this, but my nearest guess is that I need to run the dscl command to either create or change the user's NFSHomeDirectory entry (source). The resulting commands would read something like this in El Capitan:

sudo dscl . -create /Users/userName NFSHomeDirectory /Users/userName

This seems to do what it's supposed to do. When I follow this up with

dscl
. -read /Users/userName

I see the directory info for the user with the correct path, but no actual home folder is generated in the Finder like what happens in Server app when a blank home folder is created by a path change.


How can I use Terminal commands to replicate this behavior and generate a fresh home folder for an existing user?


Thank you,

Chris

Mac mini, OS X Server

Posted on Jan 20, 2018 10:22 PM

Reply
3 replies

Jan 22, 2018 2:47 PM in response to Antonio Rocco

Thanks for the comprehensive, updated workflow! The final mkdir command creates a new, blank home directory for a user, which is a little different from what Server App does when it both creates the directory and populates it with sub directories and (I'm guessing) the appropriate permissions.


Curbing my skepticism, I went ahead and tried this out, beginning with a few lines to rename the old home and create the new one:

# switch directories to the root home folder directory

cd $HOMEDIR_PATH

# rename old user directory and create new, blank on in its place

sudo mv $uName $uName-old

mkdir $uName

$HOMEDIR_PATH and $uName are the values of the main home folder repository for all of our users and specified username, respectively. I went ahead and copied over the user's files, set flags and permissions, and attempted a login.


Success! 😀


For some reason I thought that the Server App-generated home directory template, the Library folder specifically, was important to this process. Turns out a new blank folder with correct permissions works just fine.


Thanks for showing me that the jail cell of my mind had no door! 😁

Jan 22, 2018 2:47 PM in response to DATech

Using 'testuser' as an example:


Create the user in the local domain under the category /Users:

dscl . -create /Users/testuser UniqueID 501


Set the user’s shell property to bash:

dscl . -create /Users/testuser UserShell /bin/bash


Create the user’s full name:

dscl . -create /Users/testuser RealName “Test User”


Create and set the password:

dscl . -passwd /Users/testuser password


Make the user part of the Administrator (admin account) Group:

dscl . -create /Users/testuser PrimaryGroupID 80


Or you can make the user part of the local Staff (non-admin account) Group:

dscl . -create /Users/testuser PrimaryGroupID 20


Create and set the user’s home directory:

dscl . -create /Users/testuser NFSHomeDirectory /Local/Users/appleadmin


The following will also do for creating the home folder:

mkdir /Users/testuser


The above has worked consistently well for me over the years and can be scripted and added to your deployment solution.

Jan 23, 2018 5:15 AM in response to DATech

As long as the home folder itself exists and is named appropriately with the relevant permissions (ie: user is the owner with r/w access; staff and others read only) then the default home folder folders will be created on successful login. These folders will be generated from the default home folder template and be given their correct permissions. In my experience and AFAIK that's how it has always worked since at least OS X Jaguar and probably before.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Generating user home folder in Terminal

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.