niXforums Forum Index
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   PreferencesPreferences   Log in to check your private messagesLog in to check your private messages   Log inLog in 
·  nixdoc.net ·  man pages ·  Linux HOWTOs ·  FreeBSD Tips ·  Forums
navigation Forum index » *nix » Linux » security
How to create /etc/passwd file?
Post new topic   Reply to topic Page 1 of 2 [23 Posts] View previous topic :: View next topic
Goto page:  1, 2 Next
Author Message
Bit Twister
*nix forums Guru


Joined: 19 Feb 2005
Posts: 1546

PostPosted: Mon Jun 19, 2006 8:54 pm    Post subject: Re: How to create /etc/passwd file? Reply with quote

On Mon, 19 Jun 2006 16:42:01 -0400, Ross wrote:
Quote:
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
Back to top
Ross
*nix forums Guru Wannabe


Joined: 26 Mar 2005
Posts: 108

PostPosted: Mon Jun 19, 2006 9:16 pm    Post subject: Re: How to create /etc/passwd file? Reply with quote

Thanks a lot for your idea!
But the $user comes with new line. The output is like this:
user_1^MMad:1001:1000::/home/user_1^M:/bin/bash
user_2^MMad: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...
Quote:
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
Back to top
Chris F.A. Johnson
*nix forums Guru


Joined: 20 Feb 2005
Posts: 2268

PostPosted: Mon Jun 19, 2006 9:19 pm    Post subject: Re: How to create /etc/passwd file? Reply with quote

On 2006-06-19, Ross wrote:
Quote:
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 "%sMad:%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
Back to top
Michael Heiming
*nix forums Guru


Joined: 19 Feb 2005
Posts: 1423

PostPosted: Mon Jun 19, 2006 9:24 pm    Post subject: Re: How to create /etc/passwd file? Reply with quote

In comp.os.linux.networking Ross <nospam@ross.com>:
Quote:
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"Mad:"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
Back to top
Michael Heiming
*nix forums Guru


Joined: 19 Feb 2005
Posts: 1423

PostPosted: Mon Jun 19, 2006 9:31 pm    Post subject: Re: How to create /etc/passwd file? Reply with quote

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

Quote:
"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 ]


Quote:
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^MMad:1001:1000::/home/user_1^M:/bin/bash
user_2^MMad: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?).
Back to top
Chris F.A. Johnson
*nix forums Guru


Joined: 20 Feb 2005
Posts: 2268

PostPosted: Mon Jun 19, 2006 9:41 pm    Post subject: Re: How to create /etc/passwd file? Reply with quote

On 2006-06-19, Ross wrote:
Quote:

"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]

Quote:
Thanks a lot for your idea!
But the $user comes with new line. The output is like this:
user_1^MMad:1001:1000::/home/user_1^M:/bin/bash
user_2^MMad: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.

Quote:
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 "%sMad:%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
Back to top
Bit Twister
*nix forums Guru


Joined: 19 Feb 2005
Posts: 1546

PostPosted: Mon Jun 19, 2006 10:16 pm    Post subject: Re: How to create /etc/passwd file? Reply with quote

On Mon, 19 Jun 2006 17:19:33 -0400, Chris F.A. Johnson wrote:
Quote:
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,...... Sad
Back to top
Bit Twister
*nix forums Guru


Joined: 19 Feb 2005
Posts: 1546

PostPosted: Mon Jun 19, 2006 10:22 pm    Post subject: Re: How to create /etc/passwd file? Reply with quote

On Mon, 19 Jun 2006 17:16:37 -0400, Ross wrote:
Quote:

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.
Back to top
Chris F.A. Johnson
*nix forums Guru


Joined: 20 Feb 2005
Posts: 2268

PostPosted: Mon Jun 19, 2006 11:05 pm    Post subject: Re: How to create /etc/passwd file? Reply with quote

On 2006-06-19, Michael Heiming wrote:
Quote:
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^MMad:1001:1000::/home/user_1^M:/bin/bash
user_2^MMad: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.

Quote:
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
Back to top
Ertugrul Soeylemez
*nix forums Guru Wannabe


Joined: 28 Oct 2005
Posts: 126

PostPosted: Mon Jun 19, 2006 11:19 pm    Post subject: Re: How to create /etc/passwd file? Reply with quote

Michael Heiming <michael+USENET@www.heiming.de> (06-06-19 23:24:09):

Quote:
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.

Yes, and it's also more portable and optionally allows automatic home
directory creation based on a template, and so on. You shouldn't tamper
with /etc/passwd, /etc/group and /etc/shadow, unless you have a good
reason to do so. Use system tools instead.

To get you started:

for n in `seq 0 8999`; do
useradd -u $((1000 + $n)) -g $((1001 + $n / 1000)) user_$((1000 + $n))
done


Regards,
E.S.
Back to top
Michael Heiming
*nix forums Guru


Joined: 19 Feb 2005
Posts: 1423

PostPosted: Tue Jun 20, 2006 12:08 am    Post subject: Re: How to create /etc/passwd file? Reply with quote

In comp.os.linux.networking Chris F.A. Johnson <cfajohnson@gmail.com>:
Quote:
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^MMad:1001:1000::/home/user_1^M:/bin/bash
user_2^MMad: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/

Thx for reminding! ;-)

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

Just that it sucks if you edit/transfer text on one and copy to
the other. Strange enough there's zero problem between any kind
of unix system. Just M$ has this problem, perhaps because it
isn't an OS but a fine? ;-)

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

So much for standards...

--
Michael Heiming (X-PGP-Sig > GPG-Key ID: EDD27B94)
mail: echo zvpunry@urvzvat.qr | perl -pe 'y/a-z/n-za-m/'
#bofh excuse 246: It must have been the lightning storm we had
(yesterday) (last week) (last month)
Back to top
Chris F.A. Johnson
*nix forums Guru


Joined: 20 Feb 2005
Posts: 2268

PostPosted: Tue Jun 20, 2006 5:47 pm    Post subject: Re: How to create /etc/passwd file? Reply with quote

On 2006-06-19, Ertugrul Soeylemez wrote:
Quote:
Michael Heiming <michael+USENET@www.heiming.de> (06-06-19 23:24:09):

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.

Yes, and it's also more portable

It is not portable; there are different versions, with different
syntax, even among Linux distros. Other *nixen may not have it at
all.


--
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
Back to top
Ross
*nix forums Guru Wannabe


Joined: 26 Mar 2005
Posts: 108

PostPosted: Tue Jun 20, 2006 7:58 pm    Post subject: Re: How to create /etc/passwd file? Reply with quote

Thanks a bunch to all guys!
Your replies are very helpful!
Ross

"Ross" <nospam@ross.com> wrote in message
news:UeOdndoExoWHkQrZnZ2dnUVZ_q-dnZ2d@magma.ca...
Quote:
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
Back to top
Michael Heiming
*nix forums Guru


Joined: 19 Feb 2005
Posts: 1423

PostPosted: Tue Jun 20, 2006 8:15 pm    Post subject: Re: How to create /etc/passwd file? Reply with quote

In comp.os.linux.networking Ertugrul Soeylemez <never@drwxr-xr-x.org>:
Quote:
Michael Heiming <michael+USENET@www.heiming.de> (06-06-19 23:24:09):

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.

Yes, and it's also more portable and optionally allows automatic home

If more portable or not, 'useradd' should ease up things
tremendously.

Basically the versions of useradd on many unix[tm] work the same
as the Linux version, the later has a nice option to hand over a
crypt() string as password. Haven't seen this option in any other
unix[tm], though it might have been added since I checked.

Halfway recent *BSD versions can do the same through 'pw' (iirc).
No rocket science to rewrite with awk one version to another.

Quote:
directory creation based on a template, and so on. You shouldn't tamper
with /etc/passwd, /etc/group and /etc/shadow, unless you have a good
reason to do so. Use system tools instead.

Indeed, strange enough the better idea of doing so occurred me
just after typing the line awk.

Quote:
To get you started:

for n in `seq 0 8999`; do
useradd -u $((1000 + $n)) -g $((1001 + $n / 1000)) user_$((1000 + $n))
done

Nice line of bash. ;-)

--
Michael Heiming (X-PGP-Sig > GPG-Key ID: EDD27B94)
mail: echo zvpunry@urvzvat.qr | perl -pe 'y/a-z/n-za-m/'
#bofh excuse 134: because of network lag due to too many people
playing deathmatch
Back to top
Ertugrul Soeylemez
*nix forums Guru Wannabe


Joined: 28 Oct 2005
Posts: 126

PostPosted: Tue Jun 20, 2006 9:20 pm    Post subject: Re: How to create /etc/passwd file? Reply with quote

"Chris F.A. Johnson" <cfajohnson@gmail.com> (06-06-20 13:47:12):

Quote:
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.

Yes, and it's also more portable

It is not portable; there are different versions, with different
syntax, even among Linux distros. Other *nixen may not have it at
all.

I didn't say it's portable. I said, it's _more_ portable. Editing
/etc/passwd directly may even be unportable on the same distribution
with a different version.


Regards,
E.S.
Back to top
Google

Back to top
Display posts from previous:   
Post new topic   Reply to topic Page 1 of 2 [23 Posts] Goto page:  1, 2 Next
View previous topic :: View next topic
The time now is Sat Jan 10, 2009 12:41 am | All times are GMT
navigation Forum index » *nix » Linux » security
Jump to:  

Similar Topics
Topic Author Forum Replies Last Post
No new posts Running php file everyday on scheduled time sachin PHP 1 Fri Jul 21, 2006 12:49 pm
No new posts Regarding thesaurus iso file Srikanth modules 0 Fri Jul 21, 2006 10:42 am
No new posts how can i get a file descriptor not used? mars system 0 Fri Jul 21, 2006 7:41 am
No new posts small GTK "Open file" dialog David Siroky Debian 0 Fri Jul 21, 2006 7:30 am
No new posts Trouble Declaring 3D Array in Header File free2klim C++ 1 Fri Jul 21, 2006 4:07 am

Work at Home Jobs | Loans | Internet Advertising | Myspace Proxy | Debt Consolidation
Copyright © 2004-2005 DeniX Solutions SRL
 
Other DeniX Solutions sites: Unix/Linux blog |  electronics forum |  medicine forum |  science forum | 
Privacy Policy


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.3008s ][ Queries: 16 (0.1443s) ][ GZIP on - Debug on ]