[sudo-users] appending to /etc/hosts as a non root user using sudo

Aaron Spangler aaron777 at gmail.com
Thu Jan 6 22:11:20 EST 2005


You have a couple of options.

Option1)   Try flipping your quotes around.  Text wrapped in single
quotes ' do not allow $dev to be expanded.  Use double quotes "
instead.  Maybe something like this:
sudo sh -c "echo '$dev\t$id\t#' $loc >> /etc/hosts".

Option2)  Write the script so that it checks the parameters coming in
as well as check to see what privileges the script are being called
with.  Lets call the script 'append_script.sh'.  (see below).  You
could even make the script be owned by root and be mode 700 to ensure
the user must run it from sudo.  Then all you have to do is simply
grant sudo privileges to run the script.

#!/bin/sh
# append_script.sh
#
# usage: sudo ./append_script.sh <dev> <loc> <ip> 
#
#
# pick off options
dev=$1
loc=$2
ip=$3
#
# TODO:
# ## put business logic to validate options passed in.
# ## show usage information and exit if conditions are not ment
#
#
#  Now ensure they first ran it with sudo
id | grep euid=root > /dev/null
if [ $? ne 0 ];then
  echo "usage: sudo $0 <dev> <loc> <ip>"
  exit 2
fi
#
# Now finally do the append
#
echo $dev"\t"$id"\t""#" $loc >> /etc/hosts
#
#END



On Thu, 06 Jan 2005 16:17:58 -0500, DBSMITH at ohiohealth.com
<DBSMITH at ohiohealth.com> wrote:
> All,
> 
> I was looking in the archives and found some data related to what I want
> to accomplish, but not quite.
> It told me to :
> 
> % sudo sh -c 'echo foo >> /var/log/bar'
> 
> With a sudoers entry like:
> 
> dude             somehost = /bin/sh -c /bin/echo foo >> /var/log/bar
> 
> but what I want is to append to /etc/hosts using sudo from with a script
> that holds variables.
> For example,
> 
> dev=172.25.123.123
> loc=ISF
> id=prt207
> 
> sudo echo $dev"\t"$id"\t""#" $loc >> /etc/hosts
> 
> but this is given me permission errors.
> so then I tried:
> 
> sudo sh -c 'echo $dev"\t"$id"\t""#" $loc >> /etc/hosts'
> with the line in my sudoers file  /usr/bin/sh -c
> 
> I am running 1.6.6
> Any ideas?
> 
> thank you
> Derek B. Smith
> OhioHealth IT
> UNIX / TSM / EDM Teams
> 
> ____________________________________________________________
> sudo-users mailing list <sudo-users at sudo.ws>
> For list information, options, or to unsubscribe, visit:
> http://www.sudo.ws/mailman/listinfo/sudo-users
>



More information about the sudo-users mailing list