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

前端 未结 19 722
北荒
北荒 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:44

    I liked Tralemonkey's approach of echo thePassword | passwd theUsername --stdin though it didn't quite work for me as written. This however worked for me.

    echo -e "$password\n$password\n" | sudo passwd $user
    

    -e is to recognize \n as new line.

    sudo is root access for Ubuntu.

    The double quotes are to recognize $ and expand the variables.

    The above command passes the password and a new line, two times, to passwd, which is what passwd requires.

    If not using variables, I think this probably works.

    echo -e 'password\npassword\n' | sudo passwd username
    

    Single quotes should suffice here.

提交回复
热议问题