Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I'm having more then 59 servers I need to create a script for user add.

purpose: if I'm creating a user in one server it will be reflected on all 59 servers. How can I accomplish this?

share|improve this question
1  
The behavior of UNIX commands for adding users depend on the particular form of UNIX you are using and, in the case of Linux, the distribution you are using. Debian has had issues with their useradd not being very scriptable, but CentOS's adduser can be readily automated. –  samiam Mar 6 at 4:09
1  
Have you considered tools like puppet or chef? –  Patrick Mar 6 at 4:51
add comment

3 Answers 3

I very much agree that LDAP or similar systems is the best way. If you insist on doing it manually, here's a way:

The best tool for this job is newusers. You will need to create a text file containing the list of users and their details. If you want to add the same user to each server, this file will only need one line.

  1. Create the list of users. The general format of the file is

    username:passwd:UID:GID:full name,room number,work phone,home phone,other:directory:shell
    

    So, in your case, you would need to use something like

    tom:password1:::"Tom Hanks","101","123456","654321","Tall"::
    danny:password2:::"Danny DeVito","102","222333","333222","Short"::
    

    Note that I have left the UID, GUID, directory and shell options empty. This means that default values will be used.

  2. Now that you have created the list, you will need to copy it to each remote machine and then add the new users. Save this little script as newusers.sh:

    #!/bin/bash
       while read ip; do
         scp users.txt root@$ip:/home/root
         ssh root@$ip newusers users.txt
       done
    
  3. Make the script executable (chmod a+x newusers.sh) and run it for each IP in your file:

    newusers.sh < IPs.txt
    

    The IPs.txt should contain a list of the IPs of the servers you want to copy this to, one per line:

    1.2.3.4
    foo.bar.com
    
  4. This will all be much easier if you have passwordless ssh set up. If you don't, run the following commands to use ssh keys allowing passwordless access (you will still need a passphrase):

    ssh-keygen -t rsa
    while read ip; do ssh-copy-id -i ~/.ssh/id_rsa.pub root@$ip; done < IPs.txt
    
share|improve this answer
1  
Note that having different UIDs on different machines could cause unexpected problems if other parts of the infrastructure start to be "synced" in a similar way using UIDs. –  Chris Down Mar 6 at 4:33
1  
The problem with LDAP is if it ever goes down. I never recommend LDAP for system accounts, only user accounts. –  Patrick Mar 6 at 4:53
add comment

Syncing users and groups between servers is exactly what LDAP is for. I highly recommend against using a homegrown solution for something like this, especially since there are a lot of variables to consider when synchronising users and groups between different servers.

A guide to set up LDAP is outside of the scope of this answer, but you can find a quick start guide for OpenLDAP, the most popular implementation, on its official website.

share|improve this answer
add comment

How about of "Configuration management software" as puppet, chef, ansible...

For me, the best way to admin lot of machines today.

For example.

If you want not deploy a puppet master can apply any configuration with "puppet apply"

File init.pp

group { "user_one":
  ensure => present,
  gid    => 500,
}

group { "dev_site_one":
  ensure => present,
  gid    => 502,
}

group { "dev_site_two":
  ensure => present,
  gid    => 503,
}

user { "user_one":
  ensure   => present,
  uid      => 500,
  gid      => 500,
  gid      => 501,
  gid      => 502,
  password => '$1$zi13KdCr$zJvdWm5h552P8b34AjxO11',
}

After, apply changes.

$ puppet apply init.pp

Reusing terdon's code...

#!/bin/bash
while read ip; do
 scp init.pp root@$ip:/home/root
 ssh root@$ip puppet apply init.pp
done

As you see, you can create user or do any other thing.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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