18

Is there any way to add, remove, or modify user accounts via the command line? I'm not talking about an account for a system process or daemon, I mean is there any way to add a user account, change, user real names, icons, FileVault protection, Admin/normal/limited user status, and basically do everything else that can be done in the System Preferences Users preference pane, but via some command line utility?

2
  • 3
    dscl? Maybe? A starting point for looking, I've never tried it myself. You'll note that your user account is completely absent from /etc/passwd. You might have luck looking at the OSX Server docs. Dec 10, 2010 at 19:10
  • I've not used dscl on a non-server Mac, but it stands to reason you could get it to work. The OSX Server docs do give an overview of what you can do with the dscl tool. Dec 10, 2010 at 20:35

2 Answers 2

20

To create an account in the Users folder

dscl / -create /Users/addfromcli

Create and set the shell property to bash.

dscl / -create /Users/addfromcli UserShell /bin/bash

Create and set the user’s full name. (Displayed in some of the panels/login screen)

dscl / -create /Users/addfromcli RealName "Added From Cli"

Create and set the user’s ID. (this is the unique id, you need to find out what id your last user create was and add one)

dscl / -create /Users/addfromcli UniqueID 503

Create and set the user’s group ID property. (This can be your default group)

dscl / -create /Users/addfromcli PrimaryGroupID 1000

Create and set the user home directory.

dscl / -create /Users/addfromcli NFSHomeDirectory /Local/Users/addfromcli

Set the password. (dont use password as a password)

dscl / -passwd /Users/addfromcli PASSWORD

Some more details can be found on OS X Daily

4
  • Wrapping those command lines in `` would greatly improve readability. Dec 15, 2010 at 3:54
  • I did this (only with / changed to localonly), but the home directory of the new user didn't appear... Dec 4, 2012 at 21:42
  • Ok, after exiting from the single user mode, I was able to see the new user in the login menu, and then there appeared the new home directory in the filesystem. So my above comment was not about any real problem. Dec 4, 2012 at 21:56
  • 2
    When I enter these commands I get the error Data source (/) is not valid.
    – Daniel W.
    Apr 20, 2022 at 15:37
8

Solution by @Infamy seems to be for 10.5.

On OS X 10.6 each line needs to be changed to look like the following:

sudo dscl localhost -create /Local/Default/Users/addusername

That is, replace / by localhost, prepend /Local/Default to /Users and invoke the entire command with sudo.

3
  • I did this (only with / changed to localonly), but the home directory of the new user didn't appear... Dec 4, 2012 at 21:43
  • Ok, after exiting from the single user mode, I was able to see the new user in the login menu, and then there appeared the new home directory in the filesystem. So my above comment was not about any real problem. Dec 4, 2012 at 21:56
  • I created a user sudo dscl localhost -create /Local/Default/Users/envguard but when I want to change owner of a file using chown envguard:staff filename I get the error chown: envguard: illegal user name. Why is MacOS crappy like that when unix did it well for a hundred of years....
    – Daniel W.
    Apr 20, 2022 at 15:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .