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....
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
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 |
|
|||
|
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 |
|
|||
|
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 > |
|
|||
|
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 |
|
|||
|
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 |
|
|||
|
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?). |
|
|||
|
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 |
|
|||
|
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. |
|
|||
|
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 |
![]() |
| Thread Tools | |
| Display Modes | |
|
|