You are not logged in.

#1 2012-10-10 11:28:32

Karussell
Member
Registered: 2012-02-16
Posts: 1

Edit /etc/sudoers via script

Hi to all Archers

First of all thank you for this great distribution with this awesome and unique community and documentation.
I tried many distris before but i havent found any comparable thing yet.

I got a question about editing the /etc/sudoers file via script
I`m thinking about something like

echo $USER $HOSTNAME=pacman -Syu >> /etc/sudoers

I tired it already with a simple text file but i`m a little bit scared because of all the things i`ve heard like
"DO always use visudo"

what can happen if i do so?

With best Regards
Karussell

Offline

#2 2012-10-10 11:43:07

Trilby
Forum Moderator
From: Massachusetts, USA
Registered: 2011-11-29
Posts: 13,843
Website

Re: Edit /etc/sudoers via script

This seems like both a very bad idea, and a very odd one.

What would be the purpose of this script?  You would not distribute this script right, it would just be for your own use, right?  If you bundled such a command with anything you distributed I would say that would be a huge problem ... you may end up with a pack of torch yielding villagers at your door.

If it's for your own use, why have a script at all as you'd only do it once.  Just edit your sudoers file (with visudo) and move on.

This smells a bit like an X-Y problem.  What are you actually trying to accomplish, and why?

Edit: I didn't even mention, that even if you managed to insert that line into sudoers, it is not properly formatted.  I suspect this would fail ... and you should hope it did, otherwise sudo would be rendered inoperable.

Last edited by Trilby (2012-10-10 11:46:37)


InterrobangSlider
• How's my coding? See this page.
• How's my moderating? Feel free to email any concerns, complaints, or objections.

Offline

#3 2012-10-10 11:48:53

WorMzy
Forum Moderator
From: Scotland
Registered: 2010-06-16
Posts: 5,278

Re: Edit /etc/sudoers via script

So long as you get the formatting right, I can't see a problem. The point of visudo is to make sure that the sudoers file is usable before committing the changes, giving you a chance to fix a mistake.

Can I suggest using /etc/sudoers.d/ instead of /etc/sudoers though. May make things easier for you.


Sakura:-
Mobo: ASUS P8Z77-V PRO // Processor: Intel Core i7-3770K 3.4GHz // GFX: nVidia GeForce GTX 970 Ti // RAM: 32GB (4x 8GB) Corsair DDR3 (@ 2133MHz) // Storage: 1x 3TB Seagate SATAII 5x 1TB Samsung SATAII, 2x 120GB Corsair SSD

Offline

#4 2012-10-10 12:17:03

DSpider
Member
From: Romania
Registered: 2009-08-23
Posts: 2,273

Re: Edit /etc/sudoers via script

The syntax is wrong. It needs the full path to the binary.

See why you need visudo? Because it would've caught it. It would've said:

visudo: >>> /etc/sudoers: syntax error near line 7 <<<
What now?

The correct syntax would be:

# echo $USER $HOSTNAME=NOPASSWD: /usr/bin/pacman -Syu >> /etc/sudoers

Of course, this would only work if you were logged in as root.

For sudo, you would have to use tee.


"How to Succeed with Linux"

I have made a personal commitment not to reply in topics that start with a lowercase letter. Proper grammar and punctuation is a sign of respect, and if you do not show any, you will NOT receive any help (at least not from me).

Offline

#5 2012-10-10 12:23:07

skanky
Member
From: WAIS
Registered: 2009-10-23
Posts: 1,838

Re: Edit /etc/sudoers via script

Also worth noting that order of entries can have an impact on whether or not they work. So always appending something to the end of the file may not get the desired result.


"...one cannot be angry when one looks at a penguin."  - John Ruskin
"Life in general is a bit shit, and so too is the internet. And that's all there is." - scepticisle

Offline

#6 2012-10-10 23:50:54

mhertz
Member
From: Denmark
Registered: 2010-06-19
Posts: 664

Re: Edit /etc/sudoers via script

As long as you get the format right and know what you're doing(!), then it's fine; I run sed on /etc/sudoers as part of my auto-install-script...

Offline

#7 2012-10-12 03:19:54

igndenok
Member
From: Sidoarjo, Indonesia
Registered: 2010-06-07
Posts: 160

Re: Edit /etc/sudoers via script

WorMzy wrote:

Can I suggest using /etc/sudoers.d/ instead of /etc/sudoers though. May make things easier for you.

I agree with this, using sudoers.d make things easier. And maybe you can modified your script to check/create a file using $USER as filename and put your original command in it.
And if you want to remove $USER from sudoers, just delete that file.


Ask, and it shall be given you.
Seek, and ye shall find.
Knock, and it shall be opened unto you.

Offline

Board footer

Powered by FluxBB