[sf-lug] permissions problem

Alex Kleider akleider at sonic.net
Sat May 27 23:46:18 PDT 2017


Thanks, Michael; the 'why' is much clearer now.
Alex

On 2017-05-27 21:32, Michael Paoli wrote:
> Or alternatively:
> $ sudo sh -c 'echo "added line" >> junk'
> 
> And why is (typically) more important than how.  :-)
> Understand they why and you'll generally well be able to figure out 
> how.
> 
> $ sudo echo "added line" >> junk
> The above is parsed/interpreted by the shell as follows:
> command/arg0: sudo
> first argument: echo
> second argument added line (taken as a single argument due to the 
> quoting)
> the >> gives us: redirection done by shell, open junk for append,
> (attempt to) create junk if it doesn't already exist and
> set that up as file descriptor 1 (standard output) for our command.
> The shell sets up redirection before attempting the command,
> so, in the earlier failure case, since the redirection fails
> (EPERM - fails to open the file for append(/write) due to insufficient
> permissions), the shell gives an error diagnostic and non-zero return
> at that point, before bothering to attempt the command itself.
> E.g.:
> $ sudo 'echo dLtjVLPyCuN' >> /etc/shadow
> -bash: /etc/shadow: Permission denied
> $ echo $?
> 1
> $ type foo
> -bash: type: foo: not found
> $ sudo foo bar baz >> /etc/shadow
> -bash: /etc/shadow: Permission denied
> $ foo bar baz >> /etc/shadow
> -bash: /etc/shadow: Permission denied
> $
> Note also with the above with the redirection, the shell
> didn't even attempt to execute sudo nor foo, as the redirection failed,
> so the shell never bothered to attempt to execute foo or sudo.
> 
>> From: "Alex Kleider" <akleider at sonic.net>
>> Subject: Re: [sf-lug] permissions problem
>> Date: Sat, 27 May 2017 20:28:53 -0700
> 
>> Thanks, Daniel. Much appreciated.
>> 
>> On 2017-05-27 20:20, Daniel Gimpelevich wrote:
>>> On Sat, 2017-05-27 at 20:16 -0700, Alex Kleider wrote:
>>>> $ sudo echo "added line" >> junk
>>>> -bash: junk: Permission denied  # why didn't sudo allow me to do the
>>>> job?
>>>> 
>>>> I don't understand why the last command didn't go through.
>>> 
>>> It's because the ">>" is given to the bash instance in which you're
>>> typing it, rather than to sudo.
>>> 
>>>> Has anyone a suggestion why and (more importantly) how to add a line
>>>> to
>>>> a privileged file? I suppose I could temporarily change ownership 
>>>> but
>>>> that seems a long way round.
>>>> tia
>>> 
>>> $ echo "added line"|sudo tee -a junk >/dev/null
> 
> 
> _______________________________________________
> sf-lug mailing list
> sf-lug at linuxmafia.com
> http://linuxmafia.com/mailman/listinfo/sf-lug
> Information about SF-LUG is at http://www.sf-lug.org/<br>
> Related Information <br>
> http://www.shallowsky.com/blog/<br>
> http://explainshell.com/ <br>



More information about the sf-lug mailing list