Modifying Solaris “useradd” Default Behaviour

Posted on January 10, 2010

A colleague recently told me that he’d heard that you could create some file that would change the default parameters that “useradd” used, such as change the default shell. I hadn’t heard of this before and answered, “Ya, you just specify it as an argument to useradd!” Never the less, he was insistent that there was a way and wanted to know what it was. So our journey began.

If there was a way, we had two options. First, we could run “useradd” with truss and look at any files its trying to open and then search Google. But thats not very educational. So I opted for the second method, that is to go to src.opensolaris.org, search for the useradd.c code and read through it. Within about 2 minutes we had our answer.

Turns out he was absolutely right, useradd look at /usr/sadm/defadduser and if it exists reads default values from it. You can see the default parameters its looking for and the format in userdefs.h line 47. So from that we created the following file (/usr/sadm/defadduser):

## Ben's test defuseradd file: /usr/sadm/defadduser
## http://src.opensolaris.org/source/xref/onnv/onnv-gate/usr/src/head/userdefs.h#89
defrid=99
defgroup=99
defgname=other
defparent=/home
defskel=/etc/skel
defshell=/usr/bin/bash
definact=0
defexpire=
defauthorization=
defprofile=
defrole=
defproj=3
defprojname=default
deflimitpriv=
defdefaultpriv=
deflock_after_retries=

Now test it, and sure enough its working. In this case we’re only overriding the default shell, changing from /bin/sh to /usr/bin/bash:

$ useradd alex
$ tail -1 /etc/passwd 
alex:x:501:99::/home/alex:/usr/bin/bash

One word of caution is that you will want to specify all the values in the defaults file (/usr/sadm/defadduser)… if the value is empty it will use no value.

So this was a fun problem which not only showed a new flexibility that I was unaware of but showcased the awesome power of open source and accessibility via OpenGrok. Viva la code!