Log in

No account? Create an account

Previous Entry | Next Entry

Adduser for Mac OS X = dscl + niutil + nicl

I'm a fan of creating users using command line, after I figured out it is so much easier to use adduser in Solaris than to figure how to do it with gui.

useradd -d /export/home/shortname -m -u 501-s /bin/zsh -c "Full Username" shortname

After that passwd shortname and the new user will get the password. + add role.

To keep UIDs in order, I usually start with 501 and then increase them 1 by 1 ... (you already have 0) as I'm used to that scheme...
adduser [Man wiki]

So - it took me a long time to find out an equivalent for adduser in Mac OS X. Man adduser not found, apropos adduser - nothing appropriate ...

Ta-daa! dscl -directory service command line. That is basically adduser for mac.

sudo followed by some of these examples.

Create a new entry in the local (/) domain under the category /users.
dscl / -create /Users/portingunix

Create and set the shell property to bash.
dscl / -create /Users/portingunix UserShell /bin/bash

Create and set the user’s full name.
dscl / -create /Users/portingunix RealName "Porting Unix Applications To Mac OS X"

Create and set the user’s ID.
dscl / -create /Users/portingunix UniqueID 503

Create and set the user’s group ID property.
dscl / -create /Users/portingunix PrimaryGroupID 1000

Create and set the user home directory. (Despite the name NFSHomeDirectory, this is the local path to the home directory.)
dscl / -create /Users/portingunix NFSHomeDirectory /Local/Users/portingunix

Set the password.
dscl / -passwd /Users/portingunix PASSWORD
passwd portingunix

To make that user useful, you might want to add them to the admin group.
dscl / -append /Groups/admin GroupMembership portingunix
[ADC Developer notes]

nicl to add user to groups nicl examples

And now back to figuring how to get a bootable installation of Damn Small Linux on my usb memory stick ... when DSL is booted as a VM it does not install it as it should, and I ry to avoid using a Windows machine or trying to figure how OS X commands differ from the Linux 2.4 or 2.6 commands at this hour ...

And some more adds to this :

dscl does not give any errors when done on client .. but it does not seem to do anything on client. Maybe the man page should be better written...

After more days of digging, for Mac OS X non-Server, the syntax for the adduser equivalent would go something like this

niutil -create . /users/fred
niutil -createprop . /users/fred gid [groupID]
niutil -createprop . /users/fred uid [uniqueNumberOver1000]
niutil -createprop . /users/fred shell /bin/tcsh
niutil -createprop . /users/fred home /Users/fred
niutil -createprop . /users/fred realname "fred jones"
niutil -createprop . /users/fred passwd '*'
mkdir /Users/fred
mkdir /Users/fred/.ssh
chown -R fred /Users/fred
chgrp -R [groupID] /Users/fred
chmod 755 /Users/fred

Which indeed looks quite long and inefficient way to do it .. maybe that combined in one script, and script named adduser would do the trick. Maybe even the script prompting for the next value...

Still - adduser is practical, and it should be included in OS X client by default.

[niutil syntax]

-- Update : For Mac OS X 10.5, use dscl instead of niutil --



( 3 comments — Leave a comment )
Aug. 6th, 2007 03:34 am (UTC)
dscl commands
Thanks for posting this, however I found the dscl commands as listed didn't quite yield the same results as creating a user via System Prefs so I modified them so the result via the command line is essentially identical to System Prefs added users. It got me where I needed to be.


#get the next higher uid for new user
new_uid=`nidump passwd . | awk -F: '{print $3f}' | sort -n|tail -1`
new_uid=`expr $new_uid + 1`

#set group
gid=$new_uid #uid and gid are the same in accounts created through gui

#set full and short names for user
fullname='Porting Unix'

#set password

#create new group for user
dscl / create /groups/$group
dscl / create /groups/$group name $group
dscl / create /groups/$group passwd "*"
dscl / create /groups/$group gid $gid
dscl / create /groups/$group users $shortname

#create new user and password
dscl / -create /Users/$shortname
dscl / -create /Users/$shortname UserShell /bin/bash
dscl / -create /Users/$shortname RealName "$fullname"
dscl / -create /Users/$shortname UniqueID $new_uid
dscl / -create /Users/$shortname PrimaryGroupID $gid
dscl / -create /Users/$shortname home /Users/$shortname
dscl / -create /Users/$shortname authentication_authority ";ShadowHash;"
dscl / -create /Users/$shortname _shadow_passwd ""
dscl / -passwd /Users/$shortname $password

#create home directory from template for new user and set permissions
cp -R /System/Library/User\ Template/English.lproj /Users/$shortname
chown -R $shortname /Users/$shortname
chgrp -R $gid /Users/$shortname
chmod 755 /Users/$shortname
Aug. 6th, 2007 08:51 am (UTC)
Re: dscl commands
Thanks for the corrections :)

Apr. 21st, 2009 02:52 pm (UTC)
Re: dscl commands
i was wondering if this should be entered as it is shown or is there some parts that should be entered as I intend to name them? like where it's got the word password do iactually enter that or do i ener my own password? and as well for the following i'm also wondering -$fullname" ,$new_uid ,$gid ,my daughter has a macbook with Mac OS X 10.4.9 K build with an intel duo she lost the instal disk and i have no idea what she put as a password, nor does she! I realy could use some help.
( 3 comments — Leave a comment )

Latest Month

March 2011

Page Summary

Powered by LiveJournal.com
Designed by Tiffany Chow