How to create user account via a bash script?

5,111

Solution 1

Generate the password with perl perl -e 'print crypt("passwd", "passwd");'.

Then just use a bash script to call useradd via ssh on each server (assuming you already setup ssh keys for password-less login, otherwise this will get old, fast)

pass=$(perl -e 'print crypt("passwd", "passwd");')
for srv in $(cat server.list); do
   ssh $srv useradd -m -p "$pass" username
done

and check the manpage for the useradd command Manpage icon.

Solution 2

I would suggest to use ansible if you want to create a user account across a large number of servers. Ofcourse, it's not limited to just user accounts and it's very easy to get setup and started with using ansible. The documentation is quite nice!

All you do is add all your 200 servers into an inventory file and run a simple command to create all the accounts. Take a look at this example of adding a user.

Share:
5,111

Related videos on Youtube

karthick87
Author by

karthick87

Updated on September 18, 2022

Comments

  • karthick87
    karthick87 over 1 year

    I need to create a new user account with password in more than 200 systems. What will be the easiest way? Is it possible to do this task via bash script or perl script?

    • Paddy Landau
      Paddy Landau almost 12 years
      Have a look at the commands addgroup and adduser.
  • karthick87
    karthick87 over 11 years
    Could you share here an example, on using ansible with ssh private key..
  • Amar
    Amar over 11 years
    As part of your initial setup of ansible you will be adding your ssh private key to ssh-agent, see the Getting Started section. The example itself would just be that one line command as shown in the example link I have given above. viz. ansible all -m user -a "name=test1 comment=TestUser1 group=users shell=/bin/bash"