How to automatically add user account AND password with a Bash script?

前端 未结 19 709
北荒
北荒 2020-12-07 06:53

I need to have the ability to create user accounts on my Linux (Fedora 10) and automatically assign a password via a bash script(or otherwise, if need be).

It\'s eas

19条回答
  •  太阳男子
    2020-12-07 07:34

    The solution that works on both Debian and Red Hat. Depends on perl, uses sha-512 hashes:

    cat userpassadd
        #!/usr/bin/env bash
    
        salt=$(cat /dev/urandom | tr -dc A-Za-z0-9/_- | head -c16)
        useradd -p $(perl -e "print crypt('$2', '\$6\$' . '$salt' . '\$')") $1
    

    Usage:

    userpassadd jim jimslongpassword
    

    It can effectively be used as a one-liner, but you'll have to specify the password, salt and username at the right places yourself:

    useradd -p $(perl -e "print crypt('pass', '\$6\$salt\$')") username
    

提交回复
热议问题