1834594 Members
3857 Online
110069 Solutions
New Discussion

Re: /etc/passwrd

 
Rick Barr
New Member

/etc/passwrd

I am looking for the easiest way to strip out the lines of the password file that do not contain passwords.

My thinking is to lock in on the "::" on each line were the password would go.

Any ideas..
11 REPLIES 11
Praveen Bezawada
Respected Contributor

Re: /etc/passwrd

Hi
I am not sure if this is the easiest way, but you could do

while eline;do
pass=`echo $eline | awk -F: '{ print $2 }'`
if [ ! -z "$pass" ]; then
echo $eline >> tmpfile
fi
done< /etc/passwd
mv tmpfile /etc/passwd

...BPK...
James R. Ferguson
Acclaimed Contributor

Re: /etc/passwrd

Hi Rick:

Try this:

# awk -F: '$2 ~/\*/ {print}' /etc/passwd

Regards!

...JRF...
Mark Greene_1
Honored Contributor

Re: /etc/passwrd

If you are not the only admin on the system, or if any of the applications running on the system have access to make/modify entries in the passwd file, use vipw to edit the file, and then use '/' to search for the pattern "::" as you stated. This is the best way to ensure that you do not end-up with a passwd file with corrupt entries in it.

--
mark
the future will be a lot like now, only later
James R. Ferguson
Acclaimed Contributor

Re: /etc/passwrd

Hi (again) Rick:

OK (sorry), for truly enpty fields, do this:

# awk -F: '$2 ~/^$/ {print}' /etc/passwd

Regards!

...JRF...
A. Clay Stephenson
Acclaimed Contributor

Re: /etc/passwrd

Hi Rick:

An awk one-liner should do the trick:

cat /etc/passwd | awk 'BEGIN { FS = ":" } { if (length($2) < 1) print $0 }'

That should do it.
If it ain't broke, I can fix that.
Jeff Machols
Esteemed Contributor

Re: /etc/passwrd

Just make sure you make a backup of the password file before you do any of this. Making these kind of modfications can be dangerous.

Simon Abbott
Frequent Advisor

Re: /etc/passwrd

Hello Rick,

I'd do it the same way as James but without wishing to be pedantic, if you want to strip out those lines with a NULL password field and keep the other lines you need only lines where $2 is not NULL, ie

awk -F: '$2 !~ /^$/ {print}' /etc/passwd > newpasswd_file

Simon.
I'm still working on that one
Roger Baptiste
Honored Contributor

Re: /etc/passwrd

hi rick,

On a tangent, if the system is a trusted system, the passwords will be under the /tcb directory structure. The password field in the /etc/passwd file with be just * .

HTH
raj
Take it easy.
Sridhar Bhaskarla
Honored Contributor

Re: /etc/passwrd

To get the lines that do not have a password entry

awk -F : '$2 ~/^$/ {print $0}' /etc/passwd

To replace all the fields that look like ::

sed 's/::/:*:/' /etc/passwd > pass
mv pass /etc/passwd

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
Krishna Prasad
Trusted Contributor

Re: /etc/passwrd

Try

awk -F: '$2 !~/^[A-z]/ && $2 !~/^[0-9]/ { print } ' /etc/passwd
Positive Results requires Positive Thinking
Robin Wakefield
Honored Contributor

Re: /etc/passwrd

Hi Rick,

If you have perl, this will do an in-line edit, and store a backup automatically in /etc/passwd.orig:

perl -naF: -i.orig -e 'print $_ unless ($F[1] eq "")' /etc/passwd

Rgds, Robin.