rset(1) : Formulas

Add/Modify Users

Operating systems have never provided a unified method of managing local user accounts. Every platform provides a unique interface for adding or modifying user accounts, and this has frequently been used as justification for abstracting all systems related changes using traditional configuration management tools.

A helper script for manage local users solves this problem by defining a single function that will add a user if it doesn't exist, or update an existing user if it is already defined.

OpenBSD

userset() {
    # args: username [args ...]
    username=$1
    shift

    if userinfo -e $username; then
        usermod -o "$@" $username
    else
        useradd "$@" $username
    fi

    # ensure home directory exists
    eval $(userinfo $username | awk '{ print $1 "=" $2 }')
    [ -d $dir ] || install -d -o $login $groups $dir
}

Examples:

userset eradman -u 1000 -G wheel,operator,wsrc,staff,dialer -c "Eric Radman"
userset unlockwww -u 1003 -g 10 -c "AuthPF" -s /usr/sbin/authpf

GNU/Linux

userset() {
    # args: username [useradd args ...] [usermod args ...]
    username=$1
    shift

    if ! id $username > /dev/null; then
        useradd "$@" $username
    else
        # knock off parameters that do not apply
        while [ $# -gt 1 ]; do
            case $1 in
                -m) shift ;;
                 *) break ;;
            esac
            usermod "$@" $username > /dev/null
        done
    fi
}

Examples:

userset laura -m -u 1004 -g 1004 -s /bin/bash
userset abigail -m -u 1006 -g 1006 -s /bin/bash

FreeBSD

userset() {
    # args: username [useradd args ...] [usermod args ...]

    username=$1
    shift

    if ! pw usershow $username 2>/dev/null 1>/dev/null; then
        pw useradd $username "$@"
    else
        # knock off parameters that do not apply
        while [ $# -gt 1 ]; do
            case $1 in
                -m) shift ;;
                 *) break ;;
            esac
            pw usermod $username "$@"
        done
    fi
}

Examples:

userset abigail -m -u 1006 -G guest -c "Abigail"