1829643 Members
1595 Online
109992 Solutions
New Discussion

Find it and mod it

 
SOLVED
Go to solution
Adam W.
Valued Contributor

Find it and mod it

Guru's,
I am hopelessly lost here. I need a script that will local all initiation files like .login .cschrc .logout .profile .bash_profile and so on..... in every users home directory and execute a chown to that user, while also setting those files permissions to absolute 740. I am lost.
There are two types of people in the world, Marines and those who wish they were.
12 REPLIES 12
T G Manikandan
Honored Contributor
Solution

Re: Find it and mod it

If you are using /etc/passwd

Test whether it lists the home directories:

for i in `cat /etc/passwd |grep bnoble |awk -F ":" '{print $6}'`; do echo $i; done

chmod the files:

for i in `cat /etc/passwd |awk -F ":" '{print $6}'`; do chmod 740 .profile;chmod 740 .bashrc ;chmod 740 .cshrc; done
T G Manikandan
Honored Contributor

Re: Find it and mod it

Sorry no bnoble, I was testing.


for i in `cat /etc/passwd |awk -F ":" '{print $6}'`; do echo $i; done


for i in `cat /etc/passwd |awk -F ":" '{print $6}'`; do chmod 740 .profile;chmod 740 .bashrc ;chmod 740 .cshrc; done
T G Manikandan
Honored Contributor

Re: Find it and mod it

for i in `cat /etc/passwd |awk -F ":" '{print $6}'`; do chmod 740 $i/.profile;chmod 740 $i/.bashrc ;chmod 740 $i/.cshrc; done
James R. Ferguson
Acclaimed Contributor

Re: Find it and mod it

Hi Adam:

Try:

#!/usr/bin/sh
awk -F":" '$3>100 {print $3,$4,$6}' /etc/passwd | \
while read UID GID DIR X
do
chown ${UID}:$GID ${DIR}/.profile
chmod 740 ${DIR}/.profile
done
exit

...add whatever files you need to "fix"...

Regards!

...JRF...
Adam W.
Valued Contributor

Re: Find it and mod it

James, in your example where you listed

chown ${UID}:$GID ${DIR}/.profile
chmod 740 ${DIR}/.profile


Would I simply add another line like :

chmod 740 ${DIR}/.forward and
chmod 740 ${DIR}/.ssh2 and so on? or can I list them all on the same line?
There are two types of people in the world, Marines and those who wish they were.
Patrick Wallek
Honored Contributor

Re: Find it and mod it

You could do either.

Adam W.
Valued Contributor

Re: Find it and mod it

Thanks to both of you! I have to add a couple things but you have me on the proper path. Thanks Guys!!!!

Last question How would I do this and only do the entries in /home/"whoever" without using the /etc/password ?
There are two types of people in the world, Marines and those who wish they were.
James R. Ferguson
Acclaimed Contributor

Re: Find it and mod it

Hi (again) Adam:

> Last question How would I do this and only do the entries in /home/"whoever" without using the /etc/password ?

Why? It's '/etc/password' that holds the account's location for the HOME direcotory, so that's why I used it and that's how I drove it.

The '/etc/password' file should be world-readable so that shoudn't be any issue.

Notice too, that I skipped accounts with UID<100 since by convention these are sytsem accounts setup during a cold-install (or added later as for 'ssh').

Regards!

...JRF...
Adam W.
Valued Contributor

Re: Find it and mod it

James, I only ask because we have several "generic accounts" that have a higher than 100 UID and would possibly need to be excluded, but since their home directories are listed in the /etc/passwd file, (and aren't /home/"name") I was hoping to exclude them, by only doing those accounts who's home directories reside in /home.
There are two types of people in the world, Marines and those who wish they were.
Dennis Handly
Acclaimed Contributor

Re: Find it and mod it

>by only doing those accounts who's home directories reside in /home.

You could look at /home/*. Or you could change JRF's script to look at the home directory:
awk -F":" '$3 > 100 && index($6, "/home/") {
print $3, $4, $6
}' /etc/passwd | \
James R. Ferguson
Acclaimed Contributor

Re: Find it and mod it

Hi (again) Adam:

> I only ask because we have several "generic accounts" that have a higher than 100 UID and would possibly need to be excluded.

Given that you know the UID's of the accounts you want to exclude, just do:

#!/usr/bin/sh
awk -F":" '{if ($3>100 && $3 != 107 && $3 != 108) {print $3,$4,$6}}' /etc/passwd | \
while read UID GID DIR X
do
print "chown ${UID}:$GID ${DIR}/.profile"
done
exit

...which in this example, would exclude accounts whose UID is less than 100 along with accounts having UID=107 and UID=108. You can form any boolean condition you need.

Regards!

...JRF...
Adam W.
Valued Contributor

Re: Find it and mod it

Right on James, Thanks again!!!
There are two types of people in the world, Marines and those who wish they were.