cancel
Showing results for 
Search instead for 
Did you mean: 

Copy/clone user in linux

 
bullz
Super Advisor

Copy/clone user in linux

Hello all,

I have a small query on user management.

I want to create a list of users, the shell, group of these users must the same replica of an existing user.

But only the condition is, the UID must be unique, the duplication of the UID is not required here, I just want to create many users with the same group and shells

* Points will be shared without fail :)
6 REPLIES
Gerardo Arceri
Trusted Contributor

Re: Copy/clone user in linux

Get the attributes from the existing user
suppose all users you want created are members of the groupa (primary group )and groupb groups, the shell is /bin/someshell and you want their password to be "somepass"

I'd create a txt file containing the usernames:
#cat userlist
usera
userb
userc

Then the following piece of script should do the trick

for user in `cat userlist`
do useradd $user -g groupa -G groupb -s /bin/someshell
echo somepass | passwd --stdin $user
done

Hope it was helpful!
Steven E. Protter
Exalted Contributor

Re: Copy/clone user in linux

Shalom,

On HP-UX there is a profile called /etc/skel which controls default user creation.

The useradd commadn in Linux, has extra parameters that will allow you to use group or user templates.

Its all in the man page for useradd.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
Ivan Ferreira
Honored Contributor

Re: Copy/clone user in linux

You can use a script like this:

#!/bin/bash

if [ $# -lt 2 ]
then
echo "Usage: $0 "
exit 1
fi

PROTOUSER=$1
NEWUSER=$2

PROTOSHELL=$(grep "$PROTOUSER:x" /etc/passwd | cut -f 7 -d ":")
PROTOGID=$(grep "$PROTOUSER:x" /etc/passwd | cut -f 3 -d ":")


echo "Adding new user $NEWUSER based on $PROTOUSER"
useradd -s $PROTOSHELL -g $PROTOGID $NEWUSER
Por que hacerlo dificil si es posible hacerlo facil? - Why do it the hard way, when you can do it the easy way?
Jared Middleton
Frequent Advisor

Re: Copy/clone user in linux

We run the free web-based system admin tool "Webmin" on all of our Linux servers. It has built in functionality for doing this (user & group batch export/import) and much more. Visit www.webmin.com
Alexander Chuzhoy
Honored Contributor

Re: Copy/clone user in linux

To create 100 users with usernames user1, user2.....user99,user100 (the password is the user's name) - useful for testing:

for i in `seq 1 100`
do useradd user$i -g
echo user$i | passwd --stdin user$i
done

bullz
Super Advisor

Re: Copy/clone user in linux

Thanks all,

I will look for option "Webmin".