responding to a double password prompt with Fabric

BrainLikeADullPencil asked:

I’m using this Fabric function to try to add a user to a Ubuntu server. It’s not throwing an error, but I can’t login with the user that I’m trying to add after the script’s finished. If I add a user manually, after I run the adduser myusername command, I get prompted twice to enter a password. In this script, the repeated prompt (as I call it) is being dealt with (uneffectively, I believe) by passing the password twice to this echo command

 if not sudo("adduser %s | echo -e '%s\n%s\n'" % (new_user,passwd,passwd)).failed:

Can you explain how that would more appropriately be dealt with in this function?

def user_add(new_user, passwd=False):
    """Add new user"""
    with settings(hide('running', 'stdout', 'stderr'), warn_only=True):
        # if is_host_up(
        if not passwd:
                passwd = generate_passwd()
            if not sudo("adduser %s | echo -e '%s\n%s\n'" % (new_user,passwd,passwd)).failed:
                run('echo "{user} ALL=(ALL:ALL) ALL" >> /etc/sudoers'.format(user=new_user))
                ...other code not included

My answer:

adduser is just a Debian/Ubuntu-specific front-end to the more normal useradd command, which takes a variety of options on the command line, including a pre-hashed password as its -p option. For this you should probably be calling useradd with the desired options instead of adduser.

On most other Linux distributions, adduser is simply symlinked to useradd, or absent entirely.

View the full question and any other answers on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.