Unable to edit the sudoers file using script


 
Thread Tools Search this Thread
Operating Systems HP-UX Unable to edit the sudoers file using script
# 1  
Old 09-24-2013
HP Unable to edit the sudoers file using script

Hi All,

I've made a script in order to delete the users. The script is deleting the users and removing its entry from the /etc/sudoers however it is changing the permissions of the /etc/sudoers file to the user from which it is executing the script.

Code:
#!/bin/ksh
#set -x
print "The script will delete the users"

print "Enter the user name"
read user_id

for i in `cat server_HP-UX.txt`
do
ssh -t $i "/usr/local/bin/sudo /usr/sbin/userdel -r $user_id; /usr/local/bin/sudo /usr/bin/sed '/$user_id/d' /etc/sudoers > /home/usermgmt/tmp.bkp; /usr/local/bin/sudo /usr/bin/mv /home/usermgmt/tmp.bkp /etc/sudoers; > /home/usermgmt/tmp.bkp; /usr/local/bin/sudo /usr/bin/chown root:root /etc/sudoers; /usr/local/bin/sudo /usr/bin/chmod 440 /etc/sudoers"
print "The user has been deleted on $i"
done

Any suggestions how to again change the permissions back to 440 and root:root ownership or is there any way so that its permissions are not changed.
# 2  
Old 09-24-2013
Batch-editing sudoers is a poor idea, imagine the havoc if you botched a change! As such, it is guarded against such intrusion.

You may also wish to read this.
# 3  
Old 09-24-2013
Use cp not mv!
cp preserves the target file's attributes.
Code:
#!/bin/ksh
#set -x
print "The script will delete the users"

print "Enter the user name"
read user_id

for i in `cat server_HP-UX.txt`
do
ssh -qxt $i "
/usr/local/bin/sudo /usr/sbin/userdel -r $user_id
/usr/local/bin/sudo /usr/bin/grep -vw '$user_id' /etc/sudoers > /home/usermgmt/tmp.bkp &&
/usr/local/bin/sudo /usr/bin/cp /home/usermgmt/tmp.bkp /etc/sudoers
"
print "The user has been deleted on $i"

print "The user has been deleted on $i"
done

The && means: run the next line only if successful.

Last edited by MadeInGermany; 09-25-2013 at 04:40 AM.. Reason: leave the tmp file; grep word match
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

File edit script exceptions how to pass to script

i wrote script for if file exists script do file edit problem with the script get stuck not existing as for exit i mentioned exit 0 , and how to give the exception for script it should add ./script -- add hi ./script --add "hi how are you" now below script with case it is working for... (0 Replies)
Discussion started by: markjohn1
0 Replies

2. UNIX for Beginners Questions & Answers

Unable to edit grub2 boot screen in centos 7

I have centos 7 gui installed on vmware workstation12 on my laptop.WhenI want to pause my splash screen while starting my centos 7 using the 'esc key' nothing happens and the system just boots up.I also see a entry for aci_memory_fail... entry during the boot process.help me fix the system. (1 Reply)
Discussion started by: sabsac
1 Replies

3. Homework & Coursework Questions

Edit the file in shell script

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: I am trying to automate hadoop installation procedure using shell script. It involves go to perticular directory... (3 Replies)
Discussion started by: Abdul Navaz
3 Replies

4. Shell Programming and Scripting

Unable to edit file remotely using ssh

Hi, I am trying to edit the /etc/sudoers file using ksh, however I am unable to do that. Please find the code below: ssh $i "/usr/local/bin/sudo /usr/bin/echo "$user_id ALL=(ALL) ALL" >> /etc/sudoers" I've tried changing the double quotes to single but it is still not working. The... (3 Replies)
Discussion started by: Kits
3 Replies

5. UNIX for Dummies Questions & Answers

UNIX script unable to edit

Hello every one, I am a newbie , I am trying to run the script .sh file , but when I run this, I need this backup file to store at some location called " /A/B " ,but this location should only consist the latest backup file. which mean at any time this location only consists the latest backup... (2 Replies)
Discussion started by: karthik25
2 Replies

6. UNIX for Dummies Questions & Answers

Deny to edit a specific file in sudoers

How do I deny a user to edit a specific file in directory but the user will have a capability to use sudo and execute any command? I will just deny him/her to edit sayy 5files in different directories in linux? example. He cannot edit /etc/modprobe.d/blacklist.conf and /etc/sshd.config? Then the... (6 Replies)
Discussion started by: lhareigh890
6 Replies

7. Shell Programming and Scripting

shell script to edit a file

i have a file called number which contains data as 1 2 3 4 5 6 7 8 9 0 9 8 7 6 5 4 3 2 1 0 0 1 2 3 4 needed a shell script to print the output as 1 7 7 1 4 and (2 Replies)
Discussion started by: jacky29
2 Replies

8. Shell Programming and Scripting

Need help in writing a script to edit a file

Hi all, I need help in writing a script to edit a file Here is the sample of my file abc xxx 123 456 789 045 def yyy 987 678 098 cdf zzz 435 543 jhg vvv 987 765 (2 Replies)
Discussion started by: leo.maveriick
2 Replies

9. Shell Programming and Scripting

How to EDIT file using VI in a script

Hello, How would i go about editing a file using VI within a shell script please? Basically, i want to open a file, clear it's contents and save the file. I found this on the web using "ex" but can't seem to get it to work: ex /home/oconnor/TOOLS/UNIXCMDS/test_ex <<eof_ex dd /*i put... (2 Replies)
Discussion started by: bobk544
2 Replies
Login or Register to Ask a Question