Name

dscl — stdin  stdout  - file  -- opt  --help  --version

Synopsis

dscl [arguments]

The dscl command has many uses, but for our purposes, it’s for creating, modifying, and deleting users. Normally you create users with System Preferences, under Users & Groups (Lion) or Accounts (earlier versions of OS X), and frankly this is the easiest method for a single user. But if you need to do it via the shell (say, for creating multiple users in bulk), dscl is the approved technique. In this section, we’ll create a user on the local Macintosh. First we need to choose:

  • A username. We’ll use zippy.

  • A password.

  • A unique positive integer for the user ID. We’ll use 550.

  • A default group for the user to belong to. We’ll use the staff group, whose group ID is 20.

There is no single command to create a user with all necessary attributes; you must issue multiple dscl commands to get the job done. First, we’ll create the user:[22]

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

Immediately set a password so intruders cannot log in:

sudo passwd zippy
Password: *******

Now specify the user ID, a positive integer that must be unique, i.e., no other users on your Macintosh have the same ID. You can discover the highest user ID in use by running:

dscl . list /users UniqueID | awk '{print $2}' \
  | sort -n | tail -1
214

which lists all users and their IDs, extracts the second item (the IDs), sorts them numerically, and then prints the last (highest) ID. Choose a new ID higher than 500, since users with ...

Get Macintosh Terminal Pocket Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.