How can I create a new macOS user using a pre-existing home directory?

I had some issues on my machine and had to reinstall macOS High Sierra from the recovery partition. Not sure if the setup didn't complete, or maybe it just didn't work as I expected, but I ended up at a login screen with no users on the machine. So I set a password for root from single user mode and then logged into Finder as root, and now I'd like to re-create my previous user from the existing home directory.

So on this machine there's still a /Users/username folder with all my stuff I want to use. If I simply make a new user called username and point it to that folder, will it work or will it erase that directory? Should I make a user with a different name, and then simply copy the contents over while (logged out of the new user, from a root terminal), or make a new user and then change the home directory to it? Maybe something involving the dscl utility?

As long as I can preserve all the user-installed applications, settings, keychain, etc., I'm happy.


Well, I backed up my home directory and just went for it, and it turns out all you have to do is go through the normal flow to add a new user in Users & Groups, and then when it comes time to select a home directory, choose the one you already have. It'll ask you this:

macOS new user existing home folder

Just select "Use Existing Folder" and everything ~just worked~. Didn't lose any data and all my settings and sessions and everything were back when I logged in as that user.


Note - I wasn't sure if permissions were going to line up, but they did. However, I might have gotten lucky. Before creating this user, when I did a directory listing in my old home directory, instead of the user name it showed the owner as just the ID, 501. After I created the user, the new user had ID 501 and so permissions all lined up. I'm not sure if the wizard was smart enough to select an ID for the new user based on the existing home directory, or if 501 is the default ID for the first non-root user. YMMV - if your existing home directory has a different ID or if you already have a non-root account on that machine, and it's not being clever about choosing the ID, you might have to chown everything, or create the user manually with with dscl tool and give it the ID you want.