1819721 Members
3092 Online
109606 Solutions
New Discussion юеВ

Re: /etc/passwd file

 
SOLVED
Go to solution
OldSchool
Honored Contributor

/etc/passwd file

I need a script snippet to populate my passwd file with the users real names. The names are in the /etc/passwd files from 2 other systems. The servers run independent of each other and where not set up on NIS.

Any help would be greatly appreciated.

Tom
16 REPLIES 16
Steven E. Protter
Exalted Contributor

Re: /etc/passwd file

webadmin:*:40:1::/usr/obam/server/nologindir:/usr/bin/false
ids:*:101:101:IDS/9000 Administrator:/home/ids:/sbin/sh

System is trusted, * instead of encrypted password.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
John Poff
Honored Contributor

Re: /etc/passwd file

Hi,

Here is one way to do it. First, create an ASCII file containing the account name and the real user name from the other system. Call it names.txt, and assume it looks something like this:

jpoff John Poff
tomv Tom Vormwald

Now, a little script snippet like this would read this file and use the usermod command to update the name field:

while read user LINE; do usermod -c \"$LINE\" $user ; done
Just one way to do it.

JP
OldSchool
Honored Contributor

Re: /etc/passwd file

Could it be run from the command line like this
>while read user LINE; do usermod -c \"$LINE\" $user ; done
Or should I place it in it's own shell and run the script?

Thanks,
Tom
John Poff
Honored Contributor

Re: /etc/passwd file

You can put it in a shell script or run it from the command line. I'm lazy and it is a short snippet, so I'd probably just run it from the command line.

JP
OldSchool
Honored Contributor

Re: /etc/passwd file

Thanks, I'll give it a go...
OldSchool
Honored Contributor

Re: /etc/passwd file

I created the names.txt file with the user real name -c first, then put the login name:

Bob James rc00123

then ran the command:
while read user LINE; do usermod -c \"$LINE\" $user ; done
Error message below:
User name must be specified
Usage: usermod [-u [-o]] [-g [-G [, [-d [-m]] [-s ] [-c
] [-f <inactive>] [-e <expire>] [-l <new logname>] <login><BR /><BR /><BR />What did I miss?<BR /><BR />
John Poff
Honored Contributor

Re: /etc/passwd file

You'll need to put the login name first and then the real name. The code snippet will read the login name and put it in the $user variable, and everything else on the line will go into the $LINE variable. That way, if you have a real user name with several words they will all wind up in the $LINE variable.

JP
John Poff
Honored Contributor

Re: /etc/passwd file

To clarify my last post, your login name needs to come first in the names.txt file. Your command line in the script snippet is find.

JP
OldSchool
Honored Contributor

Re: /etc/passwd file

I get the same error again that way
OldSchool
Honored Contributor

Re: /etc/passwd file

BTW, this is an untrusted system. Only the back-up servers for 2 production servers.

Thanks
John Poff
Honored Contributor

Re: /etc/passwd file

And you changed your names.txt to look like this?

rc00123 Bob James

JP
OldSchool
Honored Contributor

Re: /etc/passwd file

Yes,

rc00123 Bob James
rc01223 Jesse Jones

etc...

Same error comes up as before. Any thoughts?
John Poff
Honored Contributor
Solution

Re: /etc/passwd file

Weird. It works ok under my account, but not as root. I did find a way to make it work though. Try this for your code snippet:

while read user LINE; do eval usermod -c \"$LINE\" $user; done

JP
OldSchool
Honored Contributor

Re: /etc/passwd file

while read user LINE; do eval usermod -c \"$LINE\" $user; done

Worked like a charm, thanks so much. Saved my New Year.

Thanks Again,

Tom
Sanjay_6
Honored Contributor

Re: /etc/passwd file

Hi,

you can also try this from command line,

# cat names.txt |while read user line
>do
>usermod -c "$line" $user
>done

You will not be able to run this command "usermod" for any user who is currently logged in. it will give you an error. However for users who are not logged in, it would still work okay.

Hope this helps.

regds
OldSchool
Honored Contributor

Re: /etc/passwd file

while read user LINE; do eval usermod -c \"$LINE\" $user; done

After importing the names for the 2 other servers into a file (names.txt) and running the above command the /etc/passwd file is now populated with the users real names.

Many thanks.

Tom