Solved

Editing sudoers with a script

Posted on 2011-03-13
4
942 Views
1 Endorsement
Last Modified: 2013-12-04
Hi folks,

I'm creating a bash script that will prepare a vanilla ubuntu server for a custom web app. I need to add www-data to the sudoers file for later automation purposes.

I had hoped that the following would work but with hindsight, it's clear that doing it via sudo isn't going to work!

sudo chmod 640 /etc/sudoers
sudo echo "www-data ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers
sudo chmod 440 /etc/sudoers

Can anyone suggest a way for me to automate the editing of sudoers? The less I need the user to perform manually, the better.

As a second request, can anyone suggest how to tighten the sudo entry for my needs? I have a perl script that is doing "sudo service myapp stop" so all www-data really needs is the ability to start and stop a single service.

Thanks,
Sean
1
Comment
Question by:srodden
  • 2
4 Comments
 
LVL 11

Accepted Solution

by:
Pieter Jordaan earned 250 total points
ID: 35126642
Hi

The correct way to do sudoers, is to give permissions to a group, and then add users to the permission group.
Or add multiple permissions to the same user.

Look at the example section here: http://linux.die.net/man/5/sudoers

I also wouldn't use the apache user for that.
Rather create another user called wwwadmin, and add it to the www-data group.
Use that user to stop and start apache.

Keep in mind that all permissions you give the apache user, can be exploited using the website.

Use visudo to open the sudoers editor.

Uncomment the "ALL=(ALL) ALL" line, and add the following to the end of the file.
Also change "servername" to your server hostname, and make sure the paths are correct for your OS.

wwwadmin servername=/etc/init.d/apache stop, /etc/init.d/apache start, /etc/init.d/apache restart
# this will give the wwwadmin user stop, start and restart access to the /etc/init.d/apache binary.

Your automation question:
If you are skilled in scripting, you can use sed to automate the sudo file editing.
Just remember to reload the file when you are done, or it will not see the changes.

There are some advice here: http://www.linuxquestions.org/questions/programming-9/edit-sudoers-by-script-645094/
sed manual: http://examplenow.com/sed/info

I would rather do it manually.

I hope that helps.
0
 
LVL 10

Assisted Solution

by:pfrancois
pfrancois earned 250 total points
ID: 35126889
I shouldn't add www-data to the sudoers file, but add the user www-data to the admin (or adm?) group, in your /etc/sudoers file.

It is not necessary to chmod /etc/sudoers in your script. Consider adding quotes like:
sudo 'echo "www-data ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers'

Open in new window


It is not clear to me who is about to execute the script you wrote: the main user of the ubuntu server you are preparing? Why don't you create a ubuntu server with www-data as member of the admin/adm group? Under which form are you going to release your server: as an iso file, an Ubuntu package, an appliance running into a virtual machine, etc... ?
0
 
LVL 10

Expert Comment

by:pfrancois
ID: 35126905
Oops, BitFreeze told more or less the same as I did while I was typing my answer.
0
 

Author Closing Comment

by:srodden
ID: 35162394
Thanks guys. I have restricted sudo rights for www-admin to the scripts in question but I couldn't easily find an elegant way to automate it when logged in as a non-root user. They'll just have to do it manually and use the script for the rest. Thanks for your feedback.
0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The term "Bad USB" is a buzz word that is usually used when talking about attacks on computer systems that involve USB devices. In this article, I will show what possibilities modern windows systems (win8.x and win10) offer to fight these attacks wi…
Recently, I read that Microsoft has analysed statistics for their security intelligence report. It revealed: still, the clear majority of windows users do their daily work as administrator. An administrative account is a burden, security-wise. My ar…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question