title: Why do I have to spawn a new shell when doing remote sudo ssh commands to get proper file permissions?
body: <p>I'm using password-less key based login with sudo to execute remote commands. I have figured out that I have to spawn a new shell to execute commands that write to root areas of the remote file system. But, I would like a clear explanation of exactly why this is the case?</p>
  
  <p>This fails:</p>
  
  <pre><code>sudo -u joe ssh example.com "sudo echo test &gt; /root/echo_test"
  </code></pre>
  
  <p>with:
  bash: /root/echo_test: Permission denied</p>
  
  <p>This works fine:</p>
  
  <pre><code>sudo -u joe ssh example.com "sudo bash -c 'echo test &gt; /root/echo_test'"
  </code></pre>