0

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(env.host):
        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
1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.