Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I'm setting up a new machine (well actually, an Ubuntu VM) and am trying to write a script to setup a few common things that I use when doing this (Git, curl, vim + janus).

So my script looks a bit like this:

#setup
#!/bin/sh
sudo apt-get install git
sudo apt-get install curl
...

It doesn't seem great to have 'sudo' mixed in with my command -- that is just my security-spidey-sense tingling. It seems like something like the following might also work:

sudo setup

Is there a better way to do this? What are your rules of thumb when you write scripts and need elevated permissions?

share|improve this question

2 Answers 2

up vote 8 down vote accepted

There is no problem using multiple 'sudo' calls in scripts.

I find it better than running the whole scripts as root as the risks are limited by restricting the privilege elevation to the commands that really need them.

share|improve this answer
    
This is a good point that I didn't think about. I actually like the idea of fine-grained 'sudo'. Thank-you for your comments! –  Jimmy Lyke Sep 8 '11 at 2:01

I've done it both ways. I think the security risks are the same: if someone edits the script, you'll execute undesired commands. So make sure write permissions are restricted.

I tend to put sudo in the script if I don't want the whole script to run as root. If the script runs for a long time (I write scripts to build gcc or other big projects), multiple calls to sudo may prompt the user more than once, which can be annoying.

share|improve this answer
    
Great point on the editing of this script. I did not think about the length of some of the tasks -- have you considered nesting scripts together: Calls that need sudo put into a separate script, use 'sudo' on that one to get it started. It is a blend of your answer and jillagre's answer. –  Jimmy Lyke Sep 8 '11 at 2:05

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.