LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-11-2014, 03:32 AM   #1
apss_evaluator
Member
 
Registered: Mar 2012
Posts: 115

Rep: Reputation: Disabled
visudo entry in one script


Hi Linux Guru's

I'm just wondering if creating for an entry to /etc/sudoers using "visudo" is possible under 1 script?

It just happened I messed up /etc/sudoers using cat the >> /etc/suders and forgot to place "#" in in comment line.

here is what I intended to insert on /etc/sudoers

if ! grep -Fqi '# pogi is enabled' /etc/sudoers; then
echo "# pogi is enabled" >> /etc/sudoers;
echo "pogi ALL=(ALL) NOPASSWD:/opt/papa/bin/restart_pogi.sh" >> /etc/sudoers;
echo "# oat entry ends" >> /etc/sudoers;
else
exit
fi



I just saw using visudo is validating the /etc/sudoers if the entry is invalid or can cause a corruption. Can you post me an example alternative on updating the sudoers using visudo command?
 
Old 11-11-2014, 03:41 AM   #2
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and Scientific Linux
Posts: 5,753

Rep: Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288
Hi,

you could make your script edit a copy of /etc/sudoers, then use visudo to validate it before copying it to /etc/sudoers. Eg something like
Code:
cp /etc/sudoers /tmp/sudoers.edit
autoeditvisudo.sh /tmp/sudoers.edit # This is you command/scipt that edits the file
visudo --check -f /tmp/sudoers.edit
if [ "$?" = "0" ] ; then
  cp /etc/sudoers /etc/sudoers.back
  cp /tmp/sudoers.edit /etc/sudoers
fi
See the visudo man page for more information.

Evo2.
 
1 members found this post helpful.
Old 11-11-2014, 03:43 AM   #3
apss_evaluator
Member
 
Registered: Mar 2012
Posts: 115

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by evo2 View Post
Hi,

you could make your script edit a copy of /etc/sudoers, then use visudo to validate it before copying it to /etc/sudoers. Eg something like
Code:
cp /etc/sudoers /tmp/sudoers.edit
autoeditvisudo.sh /tmp/sudoers.edit # This is you command/scipt that edits the file
visudo --check -f /tmp/sudoers.edit
if [ "$?" = "0" ] ; then
  cp /etc/sudoers /etc/sudoers.back
  cp /tmp/sudoers.edit /etc/sudoers
fi
See the visudo man page for more information.

Evo2.
that was a fast response!, I'll try this now. thanks Evo2!
 
Old 11-11-2014, 03:49 AM   #4
SAbhi
Member
 
Registered: Aug 2009
Location: Bangaluru, India
Distribution: CentOS 6.5, SuSE SLED/ SLES 10.2 SP2 /11.2, Fedora 11/16
Posts: 664

Rep: Reputation: 80
you cant directly edit the file sudoers file with cat >> /etc/sudoers or echo "blah blah" >>/etc/sudoers not even with sudo permissions because the redirection is carried out by the shell and by then the permissions will be dropped.

however if you are doing this with script, you can run
Code:
"echo "blah blah" >>/etc/sudoers" with sudo <scriptname.sh>

EDIT:

evo2's method is better:

Code:
using visudo -c -f /etc/sudoers.temp
can validate the temp file and then you can replace it.

Last edited by SAbhi; 11-11-2014 at 03:52 AM. Reason: referring a better mothod:
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
run script after each cron entry bic Linux - Server 7 06-14-2013 12:46 AM
Need a script for dns entry changes JATA01 Programming 2 10-08-2012 02:13 PM
[SOLVED] Bash script to simulate keyboard entry? PaulFXH Linux - Desktop 5 02-11-2011 01:32 PM
Crontab entry to schedule a script every 14 days senrooy Linux - General 12 06-03-2009 09:54 AM
A visudo script? djsodom Linux - General 1 02-27-2007 08:53 AM


All times are GMT -5. The time now is 05:06 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration