Script for updating the comments field on /etc/passwd on redhat linux


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script for updating the comments field on /etc/passwd on redhat linux
# 1  
Old 12-19-2008
Script for updating the comments field on /etc/passwd on redhat linux

Hi there,

I have more that 300 servers that I need to updated the comments field on /etc/passwd for users that have a blank comments fields. The users have accounts on different servers. I have created a list of these users on a text file called update_passwd.txt.

I need a script that will compare this file with /etc/passwd first backup the passwd database and update the comment field on /etc/passwd with the details on the text file if it's blank.

I have a script that I have created not sure if it will do the job as I have not worked a lot with scripts.

Below is my script and attached is my text file:

#!/usr/bin/bash
FILE1=/tmp/update_passwd.txt
FILE2=/etc/passwd
cp $FILE2 /etc/passwd.orig
for i in `cat $FILE1 | awk -F":" '{ print $2 }'`
do
FIELD1=`cat $FILE1 | grep ${i} |awk -F":" '{ print $3 }'`
grep $i ${FILE2}
if [ $? -eq 0 ]
then
usermod -c "${FIELD1}" $i
fi
done

I will highly appreciate your assistance.Smilie
# 2  
Old 12-19-2008
Try this, the passwd file is updated with the awk command:

Code:
#!/usr/bin/bash

FILE1=/tmp/update_passwd.txt
FILE2=/etc/passwd
cp $FILE2 /etc/passwd.orig

awk 'BEGIN{FS=OFS=":"} NR==FNR{a[$2]=$3;next}
$1 in a && $5==""{$5=a[$1]} {print}' "$FILE1" "$FILE2" > passwd_new

mv passwd_new $FILE2

Regards
# 3  
Old 12-22-2008
Thanks Franklin52 your script is working 100%.
# 4  
Old 01-07-2009
Hi guys,

I'm experiencing problems after executing the script above from Franklin52. Please don't get me wrong the script is doing what it's suppose to do but it looks like it does not update the /etc/shadow or the finger information. I then get errors after running it e.g. when I login with any account on the server I get an error that ID cannot find name for user. When I copy the /etc/passwd.orig that the script backs up to /etc/passwd to fix the problem nothing is fixed until I execute chfn -f on a existing account like genlnx.

Please have a look at the errors below:

+-----------------------------------------------------------------------------+
genlnx@server1's password:
Last login: Tue Jan 6 13:07:06 2009 from 192.168.1.2
id: cannot find name for user ID 514
id: cannot find name for user ID 514
[I have no name!@server1 ~]$

To fix the problem need to execute the following command.

[root@server1 etc]# chfn -f genlnx
Changing finger information for root.
Finger information changed.
[root@server1 etc]#

I will highly appreciate your assistance.

Regards
The Duke
# 5  
Old 01-07-2009
Don't log out of root.
Check permissions and ownership of /etc/passwd against another server. That script could change them.
Check syntax of /etc/passwd with "pwck".
Read "man pwconv" in the context of your machine, but don't run it unless you are really sure.
Overall it would be much much safer to use "usermod" to update the fields.
# 6  
Old 01-07-2009
Hi Methyl,

I did use pwconv but did not fix the problem hance I ended up using chfn.
My first script is using usermod and this is not working and Franklin52 script is working except for this problem with finger information.

Can anyone assist me with a script or update Franklin52 script to incorporate usermod when updating the comments field.

The Duke will high appreciate your help.
# 7  
Old 01-10-2009
Hi Guys,

May I please get help with this problem, I have been trying a lot of things and I'm not winning this script keeps deleting user account.I did it on a different server then used chfn but it did not solve the problem. Please help me I have 300 server that I need to update the comment field on and I'm desparate for this to work.

+-----------------------------------------------------------------------------+
genlnx@server2's password:
Last login: Tue Jan 10 16:07:06 2009 from 192.168.1.3
id: cannot find name for user ID 415
id: cannot find name for user ID 415
[I have no name!@server2 ~]$
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Solaris

Passwd -l or -u modifies lastchg field in /etc/shadow file

Hi, I have a Solaris 10 box where password aging is not functioning properly. Using the passwd command with the -l or -u options causes the lastchg field in the /etc/shadow file to be modified. Therefore, if a user's password is set to expire in 90 days and they are 1 day away, all they have... (4 Replies)
Discussion started by: cschar
4 Replies

2. Red Hat

Redhat Cluster updating kernel setting

hello, I was going through clustering documentation for Redhat, it says "Enable IP Aliasing support in the kernel by setting the CONFIG_IP_ALIAS kernel option to y. When specifying kernel options, under Networking Options, select IP aliasing support", I knew I should update /etc/sysctl.conf with... (1 Reply)
Discussion started by: bobby320
1 Replies

3. Shell Programming and Scripting

How to make a command same as PICO in linux but with comments

Hello, i need to make a linux command but i dont know how.. my proposal is to make a command same as pico but with comments.. for example if i enter picoc, it will run like pico but with this comments.. #================================== #Script Name: #By: #Date: #Purpose: ... (2 Replies)
Discussion started by: jrdncchr
2 Replies

4. Red Hat

Updating RedHat 5.1

Hi all, Can any one tell me how I can update RedHat 5.1 without it getting upgraded to 5.5? Thanks (1 Reply)
Discussion started by: Obi-Wan
1 Replies

5. Shell Programming and Scripting

Sed script, changing all C-comments to C++-comments

I must write a script to change all C++ like comments: // this is a comment to this one /* this is a comment */ How to do it by sed? With file: #include <cstdio> using namespace std; //one // two int main() { printf("Example"); // three }//four the result should be: (2 Replies)
Discussion started by: black_hawk
2 Replies

6. Shell Programming and Scripting

simple CSH Script behaves differently on Solaris and RedHat Linux

I have a simple csh-script on a Solaris Workstaion which invokes the bc calculator: #!/bin/csh set shz=2 set zshift=5 set shzp=`bc -l <<END \ scale = 3 \ -1. * $shz + $zshift \ END` echo $shzp The result ($shzp) in this case is 3 (-1*2+5). It works fine on Solaris 8. ... (2 Replies)
Discussion started by: two reelers
2 Replies

7. Shell Programming and Scripting

Updating the comments field on /etc/passwd

Hi there, I have more that 300 servers that I need to updated the comments field on /etc/passwd for users that have a blank comments fields. The users have accounts on different servers. I have created a list of these users on a text file called update.txt check below. I need a script that... (11 Replies)
Discussion started by: Linux Duke
11 Replies

8. UNIX for Dummies Questions & Answers

Trying to extract a field from /etc/passwd file..

Hello, was looking for some help on extracting a field from the passwd file. So far I have made a copy of the passwd file and changed my rights so I can edit it. Every user's password is coded as an :x:, and my goal was to change that x to a blank, and then try to extract any user with that field... (2 Replies)
Discussion started by: xBuRnTx
2 Replies

9. UNIX for Dummies Questions & Answers

Updating Kernels and video drivers in Redhat 8.0

Hi. I have a few "newbie" questions. This is a 2 part question which I was unable to find an answer. First, I have recently installed the kernel patch and video driver from Nvidia for my GeForce Ti 4800 video card for 3d on a fresh install of Redhat 8.0. I need to do all of the updates for Redhat.... (1 Reply)
Discussion started by: jeremiebarber
1 Replies
Login or Register to Ask a Question