Networking Forums

Networking Forums > Computer Networking > Linux Networking > How to create /etc/passwd file?

Reply
Thread Tools Display Modes

How to create /etc/passwd file?

 
 
Ross
Guest
Posts: n/a

 
      06-19-2006, 08:42 PM
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
 
 
 
 
Bit Twister
Guest
Posts: n/a

 
      06-19-2006, 08:54 PM
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
 
Ross
Guest
Posts: n/a

 
      06-19-2006, 09:16 PM
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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
 
Chris F.A. Johnson
Guest
Posts: n/a

 
      06-19-2006, 09:19 PM
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
 
Michael Heiming
Guest
Posts: n/a

 
      06-19-2006, 09:24 PM
In comp.os.linux.networking Ross <(E-Mail Removed)>:
> 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 (E-Mail Removed) | perl -pe 'y/a-z/n-za-m/'
#bofh excuse 182: endothermal recalibration
 
Reply With Quote
 
Michael Heiming
Guest
Posts: n/a

 
      06-19-2006, 09:31 PM
In comp.os.linux.networking Ross <(E-Mail Removed)>:

> "Bit Twister" <(E-Mail Removed)> 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 (E-Mail Removed) | 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
 
Chris F.A. Johnson
Guest
Posts: n/a

 
      06-19-2006, 09:41 PM
On 2006-06-19, Ross wrote:
>
> "Bit Twister" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> 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
 
Bit Twister
Guest
Posts: n/a

 
      06-19-2006, 10:16 PM
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
 
Bit Twister
Guest
Posts: n/a

 
      06-19-2006, 10:22 PM
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
 
Chris F.A. Johnson
Guest
Posts: n/a

 
      06-19-2006, 11:05 PM
On 2006-06-19, Michael Heiming wrote:
> In comp.os.linux.networking Ross <(E-Mail Removed)>:
>
>> "Bit Twister" <(E-Mail Removed)> 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

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Makefile missing for .c file. How to create asdf Linux Networking 0 04-15-2009 10:11 PM
VPN Auto Create File Mr. Smith Windows Networking 1 08-10-2007 08:47 PM
standard xp user cant create file on C root Scott Windows Networking 1 02-15-2007 04:48 PM
NIS Client 's /etc/passwd bnswami@gmail.com Linux Networking 3 07-13-2006 06:53 PM
How to create BOOTP image file? Konstantin Korolyoff Windows Networking 0 12-01-2003 04:44 PM



1 2 3 4 5 6 7 8 9 10 11