1833873 Members
3197 Online
110063 Solutions
New Discussion

Re: Useradd script

 
SOLVED
Go to solution
mtfahey
Frequent Advisor

Useradd script

I need to add about 50 users and would like to do it with a shell script. These users are on other servers. I need to keep their UID the same on all servers. They can all have the same password and force them to change it on log in. HP-UX 11.23, korn shell. Points to be awarded!
9 REPLIES 9
spex
Honored Contributor

Re: Useradd script

1) Where are you pulling the list of users/UIDs from (e.g. multiple copies of /etc/passwd on different servers, a single textfile, etc.)?
2) Is it safe to assume that a user's UID is unique?
mtfahey
Frequent Advisor

Re: Useradd script

I'm pulling the information from /etc/passwd on the master server. All the UIDs are unique.
mtfahey
Frequent Advisor

Re: Useradd script

Just to add -> I would like to create a text file from the master server and use the information from it to create the users on another server.
Peter Nikitka
Honored Contributor
Solution

Re: Useradd script

Hi,

first make sure, that no new UID is already present in /etc/passwd of the new server.
Now extract the lines of the requested users from the original passwd to a file e.g. pw2add.
If the HOME will be different, modify pw2add.

Having transformed the file to the new system, it depends on whether you use plain passwd authentication or the tcb (trusted system).
For plain passwd this is enough:
cp /etc/passwd /etc/passwd.1
cat pw2add >>/etc/passwd
awk -F: '{print $3,$4,$6}' pw2add |
while read uid gid dir
do
mkdir -p $dir && chown $uid:$gid $dir
done

If your system is trusted, let us know.
Just appending to /etc/passwd is not enough in that case.

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
mtfahey
Frequent Advisor

Re: Useradd script

To extract the users I need from /etc/passwd I was going to use the following command:

cat /etc/passwd |awk -F":" '$3 >= 1100 && $3 <= 2999 {print $1 ":" $2 ":" $3 ":"$4 ":" $5 ":" $6 ":" $7}' > pw2add

Peter Nikitka
Honored Contributor

Re: Useradd script

Hi,

the command you want to use is possible ...
>> cat /etc/passwd |awk -F":" '$3 >= 1100 && $3 <= 2999 {print $1 ":" $2 ":" $3 ":"$4 ":" $5 ":" $6 ":" $7}' > pw2add
<<
... but I want to add some comments:

- No need for the 'cat' - awk can open files by itself:
awk -F":" '$3 >= 1100 && $3 <= 2999 {print $1 ":" $2 ":" $3 ":"$4 ":" $5 ":" $6 ":" $7}' /etc/passwd >pw2add
- Having exactly seven fields in the input file, there is no need to numerate the all explictly - the action can be simplified to
... {print $0} ...
wjich is identical to
... {print} ...
which is the default, so this is equivalent:
awk -F: '$3 >= 1100 && $3 <= 2999' /etc/passwd >pw2add

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
mtfahey
Frequent Advisor

Re: Useradd script

Yeah, I just read that in the book I just bought. Thank you very much!
mtfahey
Frequent Advisor

Re: Useradd script

Thank you all for assistance.
mtfahey
Frequent Advisor

Re: Useradd script

thanx