Using sudo to write to a file as root from a script

Denis McMahon denismfmcmahon at gmail.com
Fri Aug 9 00:36:21 EDT 2013


On Thu, 08 Aug 2013 23:11:09 -0500, Adam Mercer wrote:

> Hi
> 
> I'm trying to write a script that writes some content to a file root
> through sudo, but it's not working at all. I am using:
> 
>   channel = 'stable'
>   config_file = '/opt/ldg/etc/channel.conf'
>   command = ['echo', '-n', channel, '|', 'sudo', 'tee', config_file]
>   p = subprocess.Popen(command, stdout=subprocess.PIPE,
>   stderr=subprocess.PIPE)
>   out, _ = p.communicate()
> 
> But it seems as if this isn't doing anything.
> 
> I just want to write the contents of the variable channel to the file
> /opt/ldg/etc/channel.conf. But whatever I try just doesn't work. Can
> anyone offer any pointers?

Do you find anything with:

$ grep sudo /var/log/auth.log

(you may need to specify a different log)

Is the process that's trying to use the sudo command allowed to do so 
without a password?

man sudoers

Note - after editing /etc/sudoers you must set the permissions back to 440

-- 
Denis McMahon, denismfmcmahon at gmail.com



More information about the Python-list mailing list