Solved

Editing sudoers with a script

Posted on 2011-03-13
4
920 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

Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Nessus Scan 1 64
Centros 7 Boot after new install 6 53
Full Backup software for Linux 5 51
Thin secure Windows 10 5 48
Our Group Policy work started with Small Business Server in 2000. Microsoft gave us an excellent OU and GPO model in subsequent SBS editions that utilized WMI filters, OU linking, and VBS scripts. These are some of experiences plus our spending a lo…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

757 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now