create users

1,003 views
Skip to first unread message

gregworley

unread,
May 3, 2011, 3:31:17 PM5/3/11
to golang-nuts
is there a way to systematically create a new user in linux using go?
right now I'm using a python script to do this, and I don't see
adduser in the syscall package.

Russ Cox

unread,
May 3, 2011, 3:38:44 PM5/3/11
to gregworley, golang-nuts
On Tue, May 3, 2011 at 15:31, gregworley <gr...@worleyworks.com> wrote:
> is there a way to systematically create a new user in linux using go?

not short of using exec.Run.

Brad Fitzpatrick

unread,
May 3, 2011, 3:43:03 PM5/3/11
to gregworley, golang-nuts
What does your existing python script do that Go can't?

adduser isn't a system call (in the userspace <-> kernel sense) ... it's just a program that sets things up in the right places owned by the right uid/gids and registers the name<->uid map somewhere.  I doubt your Python script is doing all that right now, is it?  Or is your Python just shelling out to e.g. /usr/sbin/adduser?

If you run your Go binary as root, though, you should be able to do anything that /usr/sbin/adduser does.

gregworley

unread,
May 3, 2011, 3:55:30 PM5/3/11
to golang-nuts
thanks Brad - no, the python script isn't doing this - it's using
p.expect to script /usr/sbin/adduser - p.expect deals with the input
required to execute /usr/sbin/adduser

But, what you're suggesting is probably the right way to do it - set
it up directly by implementing what /usr/sbin/adduser is doing.

Brad Fitzpatrick

unread,
May 3, 2011, 3:57:57 PM5/3/11
to gregworley, golang-nuts
That sounds potentially fragile, or at least error-prone.  I wouldn't recommend that... I was just saying "in theory".

You could just do what your Python script is doing with Go:  exec.Run gives you the child process's stdin and stdout to talk to, like your Python expect.

gregworley

unread,
May 3, 2011, 4:10:40 PM5/3/11
to golang-nuts
thanks

Gustavo Niemeyer

unread,
May 3, 2011, 4:17:15 PM5/3/11
to gregworley, golang-nuts
> thanks Brad - no, the python script isn't doing this - it's using
> p.expect to script /usr/sbin/adduser - p.expect deals with the input
> required to execute /usr/sbin/adduser

I don't recall adduser requiring any interaction, and several packages
actually rely on that to add a user over the installation of the
package. Check out the arguments with adduser --help and simply use
exec.Run.

--
Gustavo Niemeyer
http://niemeyer.net
http://niemeyer.net/blog
http://niemeyer.net/twitter

gregworley

unread,
May 3, 2011, 4:53:58 PM5/3/11
to golang-nuts
thanks again,
in case anyone comes across this, the relevant adduser option is:
--gecos GECOS
Set the gecos field for the new entry generated.
adduser will not ask for finger information if this option is given
Reply all
Reply to author
Forward
0 new messages