Operating System - HP-UX
1833875 Members
1963 Online
110063 Solutions
New Discussion

Re: how to create many users in a single step

 
Emanuele_4
Regular Advisor

how to create many users in a single step

Hello everybody!

I have 2 hpux server.
Server 1 has 200 users configured with their home and profiles..
Server 2 has his 200 users...

I have to add on server 1 ALL the users of server 2...creating all the home and profiles.

I'm thinkin to copy all the home dir (tar and copy) preserving permissions on files but I don't know how to create 200 users!

Should I use useradd 200 times?!? ;-(
Is there any way to batch-create many users in a single step?

Thanks in advance!

Emanuele
7 REPLIES 7
John Poff
Honored Contributor

Re: how to create many users in a single step

Hi,

If you copy over the home dirs for the users, and you aren't using shadow passwords, you can just copy the entries in /etc/passwd for the 200 users from server 1 to server 2. I've done that before. That way the users have the same passwords.

JP
Elmar P. Kolkman
Honored Contributor

Re: how to create many users in a single step

But there is one caveat: the userid's and groupid's shouldn't be different. And there shouldn't be a overlap in usernames.

A better way would be to merge the password files into NIS if you want both servers to keep working for the users. This will also keep the passwords synchronised.

But if you want to just merge, it can be done with a script too. Simplest way would be to NFS mount the /home directory of server 2 on server 1, copy /etc/passwd from server 2 to another location on server1, clean out all users that shouldn't be copied (root, lp, etc) and then script creating the users and homedirectories. If you use shadow password file, als copy that file from server 2 to server 1 (of course not to /etc) and you can put the crypted passwords in the active shadow file on server 1.
If you want, I can give a sample of the script to use, but need some more info on the questions/points I started with before doing a thing like that.
Every problem has at least one solution. Only some solutions are harder to find.
Emanuele_4
Regular Advisor

Re: how to create many users in a single step

Thanks to everybody...

I'm not using NIS and I'm not using shadows.

So I could merge the /etc/passwd files...but I'm afraid There could be broblem with duplicated names and duplicate user id and group id.

May be a script that is able to add every user reading from the original /etc/passwd could be better.

I could copy the home structure (tar with attributes) and add the users with the script?

Emanuele
Graham Cameron_1
Honored Contributor

Re: how to create many users in a single step

Well

You can either

1. Merge the password files and then check for uniqueness:
To check for duplicate username, use
awk -F: '{print $1}' /etc/passwd|sort|uniq -d
To check for duplicate id, use $3, for gid, use $4 (but duplicate group ids are normal).

or

2. Use awk to generate a whole load of useradd statements from the first passwd file, and run on the 2nd system
useradd will tell you if you are trying to add a user that already exists.

-- Graham
Computers make it easier to do a lot of things, but most of the things they make it easier to do don't need to be done.
Elmar P. Kolkman
Honored Contributor

Re: how to create many users in a single step

I would do both with the script, allowing it to set the correct owner/group on the homedirectories.

One other thing: /etc/group should probably be taken into account too.

In pseudo scripting it would become something like this:

cat passwd.server2 | while read line
do
username=`echo $line | cut -d: -f1`
passwd=`echo $line | cut -d: -f2`
homedir=`echo $line | cut -d: -f3`
:
:
if grep -q $username /etc/passwd
then
echo "User already exists... Skipping"
else
groupname=grep $groupid group.server2 | cut -d: -f1`
if grep -q $groupname /etc/group
then
:
else
groupadd $groupname
fi
useradd -d $homedir -s $loginshell -c "$realname" -g $groupname $username
(cd /home.server2 ; tar cf - $username) | (cd /home ; tar xf -)
chown -R $username:$groupname $username
fi
done


Some things miss and it depends on homedirectories using the same name as the login name, but it is a start. I'm sorry I don't have time to give a better, detailed version right now.
Every problem has at least one solution. Only some solutions are harder to find.
Leif Halvarsson_2
Honored Contributor

Re: how to create many users in a single step

Hi,
You should be aware of, when using the method with copying user home directorys from one server to another the users in the password and group files must be identic on the two servers. If you already have users/groups on the second server with the same user or group id you will get problems.
Bruno Ganino
Honored Contributor

Re: how to create many users in a single step

Edit /etc/passwd with command vi.
Write the first row and duplicate for 200 times, after change the necessary and save it.
(some procedure for group, in /etc/group)
Bruno
Torino (Turin) +2H