You are not logged in.

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

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


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

Inspector Parrot
Registered: 2011-11-29
Posts: 19,347

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)

"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman


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

Forum Moderator
From: Scotland
Registered: 2010-06-16
Posts: 7,982

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.

Mobo: MSI X299 TOMAHAWK ARCTIC // Processor: Intel Core i7-7820X 3.6GHz // GFX: nVidia GeForce GTX 970 // RAM: 32GB (4x 8GB) Corsair DDR4 (@ 3000MHz) // Storage: 1x 3TB HDD, 5x 1TB HDD, 2x 120GB SSD, 1x 275GB M2 SSD

Making lemonade from lemons since 2015.


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

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).


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

From: WAIS
Registered: 2009-10-23
Posts: 1,845

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.

" 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


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

From: Denmark
Registered: 2010-06-19
Posts: 667

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...


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

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.


Board footer

Powered by FluxBB