How to create /etc/passwd file?

This is a discussion on How to create /etc/passwd file? within the Linux Security forums, part of the System Security and Security Related category; Hi there, I am going to create 9000 users. I have all the user names in a txt file userlist....


Go Back   Usenet Forums > System Security and Security Related > Linux Security

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 06-19-2006
Ross
 
Posts: n/a
Default How to create /etc/passwd file?

Hi there,
I am going to create 9000 users.
I have all the user names in a txt file userlist.txt like this:
user_1
user_2
:
user_9000

I want the passwd file to be created like this:
user_1:x:1001:1000::/home/user_1:/bin/bash
user_2:x:1002:1000::/home/user_2:/bin/bash
:
user_1000:x:1001::/home/user_2000:/bin/bash
:
user_9000:x:1009::/home/user_9000:/bin/bash

How could I use bash with something like awk to create the passwd file?
And also the group, shadow files (all passwords can be the same)?
Thanks,
Ross


Reply With Quote
  #2 (permalink)  
Old 06-19-2006
Bit Twister
 
Posts: n/a
Default Re: How to create /etc/passwd file?

On Mon, 19 Jun 2006 16:42:01 -0400, Ross wrote:
> Hi there,
> I am going to create 9000 users.
> I have all the user names in a txt file userlist.txt like this:
> user_1
> user_2
>:
> user_9000
>
> I want the passwd file to be created like this:
> user_1:x:1001:1000::/home/user_1:/bin/bash
> user_2:x:1002:1000::/home/user_2:/bin/bash
>:
> user_1000:x:1001::/home/user_2000:/bin/bash
>:
> user_9000:x:1009::/home/user_9000:/bin/bash


looks like a simple while loop with two counters.

_uid=1000
_gid=999
while read user ; do
_uid=$(( $_uid + 1 ))
_gid=$(( $_gid + 1 ))
echo $user:x:$_uid:$_gid::/home/$user:/bin/bash >> /tmp/passwd
done < user_fn_here

for extra points
http://tldp.org/LDP/abs/html/index.html

Reply With Quote
  #3 (permalink)  
Old 06-19-2006
Ross
 
Posts: n/a
Default Re: How to create /etc/passwd file?

Thanks a lot for your idea!
But the $user comes with new line. The output is like this:
user_1^M:x:1001:1000::/home/user_1^M:/bin/bash
user_2^M:x:1002:1001::/home/user_2^M:/bin/bash
:

How could I eliminate the ^M?

In addition, I'd like to have users grouped like this: gid:1000 for users
user_1 to user_1000, and gid:1001 for users from user_1001 to user_2000,
etc.
Thanks again,
Ross

"Bit Twister" <BitTwister@mouse-potato.com> wrote in message
news:slrne9e3oo.bp7.BitTwister@wb.home.invalid...
> On Mon, 19 Jun 2006 16:42:01 -0400, Ross wrote:
>> Hi there,
>> I am going to create 9000 users.
>> I have all the user names in a txt file userlist.txt like this:
>> user_1
>> user_2
>>:
>> user_9000
>>
>> I want the passwd file to be created like this:
>> user_1:x:1001:1000::/home/user_1:/bin/bash
>> user_2:x:1002:1000::/home/user_2:/bin/bash
>>:
>> user_1000:x:1001::/home/user_2000:/bin/bash
>>:
>> user_9000:x:1009::/home/user_9000:/bin/bash

>
> looks like a simple while loop with two counters.
>
> _uid=1000
> _gid=999
> while read user ; do
> _uid=$(( $_uid + 1 ))
> _gid=$(( $_gid + 1 ))
> echo $user:x:$_uid:$_gid::/home/$user:/bin/bash >> /tmp/passwd
> done < user_fn_here
>
> for extra points
> http://tldp.org/LDP/abs/html/index.html
>



Reply With Quote
  #4 (permalink)  
Old 06-19-2006
Chris F.A. Johnson
 
Posts: n/a
Default Re: How to create /etc/passwd file?

On 2006-06-19, Ross wrote:
> Hi there,
> I am going to create 9000 users.
> I have all the user names in a txt file userlist.txt like this:
> user_1
> user_2
>:
> user_9000
>
> I want the passwd file to be created like this:
> user_1:x:1001:1000::/home/user_1:/bin/bash
> user_2:x:1002:1000::/home/user_2:/bin/bash
>:
> user_1000:x:1001::/home/user_2000:/bin/bash
>:
> user_9000:x:1009::/home/user_9000:/bin/bash
>
> How could I use bash with something like awk to create the passwd file?
> And also the group, shadow files (all passwords can be the same)?


awk -v gid=1000 '{ uid = gid + 1
printf "%s:x:%d:%d::/home/%s:/bin/bash\n", $1, uid, gid++, $1
}' userlist.txt >> /etc/passwd

You will also have to create a shadow file.

--
Chris F.A. Johnson, author <http://cfaj.freeshell.org>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
Reply With Quote
  #5 (permalink)  
Old 06-19-2006
Michael Heiming
 
Posts: n/a
Default Re: How to create /etc/passwd file?

In comp.os.linux.networking Ross <nospam@ross.com>:
> Hi there,
> I am going to create 9000 users.
> I have all the user names in a txt file userlist.txt like this:
> user_1
> user_2
> :
> user_9000


> I want the passwd file to be created like this:
> user_1:x:1001:1000::/home/user_1:/bin/bash
> user_2:x:1002:1000::/home/user_2:/bin/bash
> :
> user_1000:x:1001::/home/user_2000:/bin/bash
> :
> user_9000:x:1009::/home/user_9000:/bin/bash


> How could I use bash with something like awk to create the passwd file?
> And also the group, shadow files (all passwords can be the same)?


awk 'BEGIN{u=1000}{u++;print $1":x:"u":1000::/home/"$1":/bin/sh"}' infile > out

But why the hassle? You can script useradd to do the job more
easily for you. As usual the fine manual 'man useradd' has the
info.

Good luck

BTW
Still curious what this has to do with networking?

--
Michael Heiming (X-PGP-Sig > GPG-Key ID: EDD27B94)
mail: echo zvpunry@urvzvat.qr | perl -pe 'y/a-z/n-za-m/'
#bofh excuse 182: endothermal recalibration
Reply With Quote
  #6 (permalink)  
Old 06-19-2006
Michael Heiming
 
Posts: n/a
Default Re: How to create /etc/passwd file?

In comp.os.linux.networking Ross <nospam@ross.com>:

> "Bit Twister" <BitTwister@mouse-potato.com> wrote in message
>> On Mon, 19 Jun 2006 16:42:01 -0400, Ross wrote:
>>> Hi there,
>>> I am going to create 9000 users.

[ short bash script to do it ]

>> http://tldp.org/LDP/abs/html/index.html


> Thanks a lot for your idea!
> But the $user comes with new line. The output is like this:
> user_1^M:x:1001:1000::/home/user_1^M:/bin/bash
> user_2^M:x:1002:1001::/home/user_2^M:/bin/bash
> :


> How could I eliminate the ^M?


Don't keep your files on a doze box, those can't even handle a
text file probably, as you just encountered. Alternatively run
'dos2unix' over the file.

Good luck

BTW
There's no need to create shadow + groups if you just use
'useradd' for the job.

--
Michael Heiming (X-PGP-Sig > GPG-Key ID: EDD27B94)
mail: echo zvpunry@urvzvat.qr | perl -pe 'y/a-z/n-za-m/'
#bofh excuse 223: The lines are all busy (busied out, that is --
why let them in to begin with?).
Reply With Quote
  #7 (permalink)  
Old 06-19-2006
Chris F.A. Johnson
 
Posts: n/a
Default Re: How to create /etc/passwd file?

On 2006-06-19, Ross wrote:
>
> "Bit Twister" <BitTwister@mouse-potato.com> wrote in message
> news:slrne9e3oo.bp7.BitTwister@wb.home.invalid...
>> On Mon, 19 Jun 2006 16:42:01 -0400, Ross wrote:
>>> Hi there,
>>> I am going to create 9000 users.
>>> I have all the user names in a txt file userlist.txt like this:
>>> user_1
>>> user_2
>>>:
>>> user_9000
>>>
>>> I want the passwd file to be created like this:
>>> user_1:x:1001:1000::/home/user_1:/bin/bash
>>> user_2:x:1002:1000::/home/user_2:/bin/bash
>>>:
>>> user_1000:x:1001::/home/user_2000:/bin/bash
>>>:
>>> user_9000:x:1009::/home/user_9000:/bin/bash

>>
>> looks like a simple while loop with two counters.
>>
>> _uid=1000
>> _gid=999
>> while read user ; do
>> _uid=$(( $_uid + 1 ))
>> _gid=$(( $_gid + 1 ))
>> echo $user:x:$_uid:$_gid::/home/$user:/bin/bash >> /tmp/passwd
>> done < user_fn_here


[please don't top post]

> Thanks a lot for your idea!
> But the $user comes with new line. The output is like this:
> user_1^M:x:1001:1000::/home/user_1^M:/bin/bash
> user_2^M:x:1002:1001::/home/user_2^M:/bin/bash
>:
>
> How could I eliminate the ^M?


In bash:

CR=$'\r'
user=${user%"$CR"}

The script will be much faster in awk; see below.

> In addition, I'd like to have users grouped like this: gid:1000 for users
> user_1 to user_1000, and gid:1001 for users from user_1001 to user_2000,
> etc.


awk -v uid=1000 -v gid=999 '
NR % 1000 == 1 { ++gid }
{ sub( "\r","")
printf "%s:x:%d:%d::/home/%s:/bin/bash\n", $1, ++uid, gid, $1
}
' userlist.txt >> /etc/passwd


--
Chris F.A. Johnson, author <http://cfaj.freeshell.org>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
Reply With Quote
  #8 (permalink)  
Old 06-19-2006
Bit Twister
 
Posts: n/a
Default Re: How to create /etc/passwd file?

On Mon, 19 Jun 2006 17:19:33 -0400, Chris F.A. Johnson wrote:
> You will also have to create a shadow file.


Then there is creating home directory, copy initial user files from
/etc/skel, set owner/group userX,...... :(


Reply With Quote
  #9 (permalink)  
Old 06-19-2006
Bit Twister
 
Posts: n/a
Default Re: How to create /etc/passwd file?

On Mon, 19 Jun 2006 17:16:37 -0400, Ross wrote:
>
> In addition, I'd like to have users grouped like this: gid:1000 for users
> user_1 to user_1000, and gid:1001 for users from user_1001 to user_2000,
> etc.


Use an if statement around the code bumping the group id to decide
when to bump the group variable.



Reply With Quote
  #10 (permalink)  
Old 06-20-2006
Chris F.A. Johnson
 
Posts: n/a
Default Re: How to create /etc/passwd file?

On 2006-06-19, Michael Heiming wrote:
> In comp.os.linux.networking Ross <nospam@ross.com>:
>
>> "Bit Twister" <BitTwister@mouse-potato.com> wrote in message
>>> On Mon, 19 Jun 2006 16:42:01 -0400, Ross wrote:
>>>> Hi there,
>>>> I am going to create 9000 users.

> [ short bash script to do it ]
>
>>> http://tldp.org/LDP/abs/html/index.html

>
>> Thanks a lot for your idea!
>> But the $user comes with new line. The output is like this:
>> user_1^M:x:1001:1000::/home/user_1^M:/bin/bash
>> user_2^M:x:1002:1001::/home/user_2^M:/bin/bash
>> :

>
>> How could I eliminate the ^M?

>
> Don't keep your files on a doze box, those can't even handle a
> text file probably,


s/probably/properly/

There is nothing improper about a Windows text file; the standard
allows CR/LF line endings.

> as you just encountered. Alternatively run
> 'dos2unix' over the file.



--
Chris F.A. Johnson, author <http://cfaj.freeshell.org>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 05:03 AM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0