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 easy to create the user via Bash e.g.:
[whoever@server ]# /usr/sbin/useradd newuser
Is it possible to assign a password in Bash, something functionally similar to this, but automatically:
[whoever@server ]# passwd newuser
Changing password for user testpass.
New UNIX password:
Retype new UNIX password:
passwd: all authentication tokens updated successfully.
[whoever@server ]#
You can run the passwd command and send it piped input. So, do something like:
I know I'm coming at this years later, but I can't believe no one suggested usermod.
Hell, just in case someone wants to do this on an older HPUX you can use
usermod.sam
.The -F is only needed if the person executing the script is the current user. Of course you don't need to use Perl to create the hash. You could use openssl or many other commands in its place.
usage:
./my_add_user.sh USER PASSWD
code:
I was asking myself the same thing, and didn't want to rely on a Python script.
This is the line to add a user with a defined password in one bash line:
--stdin
doesn't work on Debian. It says:This worked for me:
Here we can find some other good ways:
The code below worked in Ubuntu 14.04. Try before you use it in other versions/linux variants.