1

I have a problem where I need to change the ownership and permissions of one file that exists on more than 200 servers. I belong to 2 different groups and need to change the ownership from one group to another. I also need the permissions to be 644. This could be easily done if I had root access, but I don't. So instead I need to copy the file as one user and then remove the old file and then rename the recently copied file to the name of the old file. This becomes rather tedious, so I wanted to create a script to automate these tasks. I thought kornshell would do the trick but my script doesn't seem to be doing anything. Here is what I have so far:

lockhosts.sh

#!/usr/bin/ksh
sudo su - oradba
cd config
chmod 644 hostenv.cfg
chown orainst hostenv.cfg
exit
sudo su - orainst
cd config
cp -p hostenv.cfg hostenv.cfgnew
rm hostenv.cfg
mv hostenv.cfgnew hostenv.cfg

I figured this would work because those are all the commands I need to run to successfully change the ownership/permissions of the file.

All I need to do is change the hostenv.cfg file from this:

-rwxrw---- 1 oradba  dba  4433 May  1 21:43 hostenv.cfg

To this:

-rw-r--r-- 1 orainst dba  4433 May  1 21:43 hostenv.cfg

What can I do to fix my script? Also, if I need to build user prompts into my script to ask for passwords that is fine, but I thought the system would ask for me.

Here is what happens when I try to run my script:

lockhost.sh

My user is changed to oradba and nothing else happens... Until I type exit...

exit

logout
chmod: changing permissions of `hostenv.cfg': Operation not permitted
chown: changing ownership of `hostenv.cfg': Operation not permitted
  • I think the problem is with the - option to sudo. I believe doing this takes you to the home directory of the target user, which most probably doesn't contain the config directory you're changing into. Try passing the full path to cd. – Joseph R. Jun 3 '14 at 19:59
  • I started to notice that... So I hard coded the config directory and still nothing. The symptom I'm noticing is it stops performing the script (lockhost.sh) as the user changes. When I type exit it tries to run the rest of the script... Is there a way I can perform one command as orainst without running "sudo su - orainst" ? Kind of like when you type "sudo chmod 644 hostenv.cfg" to perform as root. Instead, perform it as orainst ? – etho201 Jun 3 '14 at 20:02
  • Do you have password-less sudo access? Maybe it blocks because it's waiting for you to enter a password. – Joseph R. Jun 3 '14 at 20:03
  • It normally asks for my password, but the general behavior seems to be if I've supplied the password during the current putty session then it won't ask again until I close out putty and connect to the server again. – etho201 Jun 3 '14 at 20:05
  • I added some information to the original question to show what happens when running the script. – etho201 Jun 3 '14 at 20:34
2

The problem is that your sudo su ... spawns another shell, and only when you exit from it does your script attempt to continue.

Assuming your sudo configuration allows it, you can do this instead:

sudo -iu oradba 'cd ... && chmod ... && chown ... '
sudo -iu orainst '...'

The &&'s could have been replaced by semicolons, it's just that this way, you guarantee that the chmod won't be attempted unless the cd is successful and so on.

If your sudo configuration doesn't allow it, you can do this instead:

sudo su -c 'cd ... & chmod ... && chown ... ' - oradba
sudo su -c '...' - orainst
| improve this answer | |
  • I think I used the wrong terminology... I belong to the dba group but I'm able to sudo su to both orainst and oradba. I need the ownership changed. ussedp56:oradba;default$ chgrp orainst hostenv.cfg chgrp: invalid group `orainst' – etho201 Jun 3 '14 at 19:53
  • Performing the commands I wrote above (in the lockhosts.sh file) accomplishes the task of changing the ownership and the permissions. Now I'm just trying to build those commands into a ksh or perl script in order to help automate the task. My script just doesn't seem to perform any of the steps; instead it looks like it only does "sudo su - oradba" and then it stops. So if I run it as my own username, I end up being oradba but nothing else happens. – etho201 Jun 3 '14 at 19:56
  • It appears that my sudo configuration doesn't allow it. "Sorry, user USER is not allowed to execute '/bin/ksh' as orainst on SERVER." – etho201 Jun 3 '14 at 20:45
  • So far the best I can do is create a script to run as oradba and a script to run as orainst. That works, but it would be nice to run just one script. – etho201 Jun 3 '14 at 21:00
  • @user2554129 Does the update help? – Joseph R. Jun 3 '14 at 21:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.