<p>I've been given sudo access on one of our development RedHat linux boxes, and I seem to find myself quite often needing to redirect output to a location I don't normally have write access to.</p>

<p>The trouble is, this contrived example doesn't work:</p>

<pre><code>sudo ls -hal /root/ &gt; /root/test.out
</code></pre>

<p>I just receive the response:</p>

<pre><code>-bash: /root/test.out: Permission denied
</code></pre>

<p>How can I get this to work?</p>

[{'answer_id': 82274, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 3, 'selected': False, 'text': '<p>How about writing a script?</p>\n\n<p>Filename: myscript</p>\n\n<pre><code>#!/bin/sh\n\n/bin/ls -lah /root &gt; /root/test.out\n\n# end script\n</code></pre>\n\n<p>Then use sudo to run the script:</p>\n\n<pre><code>sudo ./myscript\n</code></pre>\n'}, {'answer_id': 82276, 'author': 'Penfold', 'author_id': 11952, 'author_profile': 'https://Stackoverflow.com/users/11952', 'pm_score': 5, 'selected': False, 'text': '<p>Make sudo run a shell, like this:</p>\n\n<pre><code>sudo sh -c "echo foo &gt; ~root/out"\n</code></pre>\n'}, {'answer_id': 82278, 'author': 'Cristian Ciupitu', 'author_id': 12892, 'author_profile': 'https://Stackoverflow.com/users/12892', 'pm_score': 12, 'selected': True, 'text': '<p>Your command does not work because the redirection is performed by your shell which does not have the permission to write to <code>/root/test.out</code>. The redirection of the output <strong>is not</strong> performed by sudo.</p>\n\n<p>There are multiple solutions:</p>\n\n<ul>\n<li><p>Run a shell with sudo and give the command to it by using the <code>-c</code> option:</p>\n\n<pre><code>sudo sh -c \'ls -hal /root/ &gt; /root/test.out\'\n</code></pre></li>\n<li><p>Create a script with your commands and run that script with sudo:</p>\n\n<pre><code>#!/bin/sh\nls -hal /root/ &gt; /root/test.out\n</code></pre>\n\n<p>Run <code>sudo ls.sh</code>. See Steve Bennett\'s <a href="https://stackoverflow.com/a/16514624/12892">answer</a> if you don\'t want to create a temporary file.</p></li>\n<li><p>Launch a shell with <code>sudo -s</code> then run your commands:</p>\n\n<pre><code>[nobody@so]$ sudo -s\n[root@so]# ls -hal /root/ &gt; /root/test.out\n[root@so]# ^D\n[nobody@so]$\n</code></pre></li>\n<li><p>Use <code>sudo tee</code> (if you have to escape a lot when using the <code>-c</code> option):</p>\n\n<pre><code>sudo ls -hal /root/ | sudo tee /root/test.out &gt; /dev/null\n</code></pre>\n\n<p>The redirect to <code>/dev/null</code> is needed to stop <a href="http://pubs.opengroup.org/onlinepubs/9699919799/utilities/tee.html" rel="noreferrer"><strong>tee</strong></a> from outputting to the screen. To <em>append</em> instead of overwriting the output file \n(<code>&gt;&gt;</code>), use <code>tee -a</code> or <code>tee --append</code> (the last one is specific to <a href="https://www.gnu.org/software/coreutils/manual/html_node/tee-invocation.html" rel="noreferrer">GNU coreutils</a>).</p></li>\n</ul>\n\n<p>Thanks go to <a href="https://stackoverflow.com/a/82274/12892">Jd</a>, <a href="https://stackoverflow.com/a/82279/12892">Adam J. Forster</a> and <a href="https://stackoverflow.com/a/82553/12892">Johnathan</a> for the second, third and fourth solutions.</p>\n'}, {'answer_id': 82279, 'author': 'Adam J. Forster', 'author_id': 15676, 'author_profile': 'https://Stackoverflow.com/users/15676', 'pm_score': 3, 'selected': False, 'text': "<p>Whenever I have to do something like this I just become root:</p>\n\n<pre><code># sudo -s\n# ls -hal /root/ &gt; /root/test.out\n# exit\n</code></pre>\n\n<p>It's probably not the best way, but it works.</p>\n"}, {'answer_id': 82331, 'author': 'user15453', 'author_id': 15453, 'author_profile': 'https://Stackoverflow.com/users/15453', 'pm_score': 1, 'selected': False, 'text': '<p>Maybe you been given sudo access to only some programs/paths? Then there is no way to do what you want. (unless you will hack it somehow)</p>\n\n<p>If it is not the case then maybe you can write bash script:</p>\n\n<pre><code>cat &gt; myscript.sh\n#!/bin/sh\nls -hal /root/ &gt; /root/test.out \n</code></pre>\n\n<p>Press <kbd>ctrl</kbd> + <kbd>d</kbd> :</p>\n\n<pre><code>chmod a+x myscript.sh\nsudo myscript.sh\n</code></pre>\n\n<p>Hope it help.</p>\n'}, {'answer_id': 82553, 'author': 'Jonathan', 'author_id': 6910, 'author_profile': 'https://Stackoverflow.com/users/6910', 'pm_score': 7, 'selected': False, 'text': '<p>Someone here has just suggested sudoing tee:</p>\n\n<pre><code>sudo ls -hal /root/ | sudo tee /root/test.out &gt; /dev/null\n</code></pre>\n\n<p>This could also be used to redirect any command, to a directory that you do not have access to. It works because the tee program is effectively an "echo to a file" program, and the redirect to /dev/null is to stop it also outputting to the screen to keep it the same as the original contrived example above.</p>\n'}, {'answer_id': 120174, 'author': 'dsm', 'author_id': 7780, 'author_profile': 'https://Stackoverflow.com/users/7780', 'pm_score': 6, 'selected': False, 'text': "<p>The problem is that the <strong>command</strong> gets run under <code>sudo</code>, but the <strong>redirection</strong> gets run under your user. This is done by the shell and there is very little you can do about it.</p>\n<pre><code>sudo command &gt; /some/file.log\n`-----v-----'`-------v-------'\n   command       redirection\n</code></pre>\n<p>The usual ways of bypassing this are:</p>\n<ul>\n<li><p>Wrap the commands in a script which you call under sudo.</p>\n<p>If the commands and/or log file changes, you can make the\nscript take these as arguments. For example:</p>\n<pre><code>sudo log_script command /log/file.txt\n</code></pre>\n</li>\n<li><p>Call a shell and pass the command line as a parameter with <code>-c</code></p>\n<p>This is especially useful for one off compound commands.\nFor example:</p>\n<pre><code>sudo bash -c &quot;{ command1 arg; command2 arg; } &gt; /log/file.txt&quot;\n</code></pre>\n</li>\n<li><p>Arrange a pipe/subshell with required rights (i.e. sudo)</p>\n<pre><code># Read and append to a file\ncat ./'file1.txt' | sudo tee -a '/log/file.txt' &gt; '/dev/null';\n\n# Store both stdout and stderr streams in a file\n{ command1 arg; command2 arg; } |&amp; sudo tee -a '/log/file.txt' &gt; '/dev/null';\n</code></pre>\n</li>\n</ul>\n"}, {'answer_id': 8213307, 'author': 'rhlee', 'author_id': 420540, 'author_profile': 'https://Stackoverflow.com/users/420540', 'pm_score': 7, 'selected': False, 'text': '<p>A trick I figured out myself was</p>\n\n<pre><code>sudo ls -hal /root/ | sudo dd of=/root/test.out\n</code></pre>\n'}, {'answer_id': 16131140, 'author': 'fardjad', 'author_id': 303270, 'author_profile': 'https://Stackoverflow.com/users/303270', 'pm_score': 3, 'selected': False, 'text': "<p>I would do it this way:</p>\n\n<pre><code>sudo su -c 'ls -hal /root/ &gt; /root/test.out'\n</code></pre>\n"}, {'answer_id': 16514624, 'author': 'Steve Bennett', 'author_id': 263268, 'author_profile': 'https://Stackoverflow.com/users/263268', 'pm_score': 5, 'selected': False, 'text': "<p>Yet another variation on the theme:</p>\n\n<pre><code>sudo bash &lt;&lt;EOF\nls -hal /root/ &gt; /root/test.out\nEOF\n</code></pre>\n\n<p>Or of course:</p>\n\n<pre><code>echo 'ls -hal /root/ &gt; /root/test.out' | sudo bash\n</code></pre>\n\n<p>They have the (tiny) advantage that you don't need to remember any arguments to <code>sudo</code> or <code>sh</code>/<code>bash</code></p>\n"}, {'answer_id': 19738137, 'author': 'jg3', 'author_id': 2094009, 'author_profile': 'https://Stackoverflow.com/users/2094009', 'pm_score': 5, 'selected': False, 'text': '<p><em>Clarifying a bit on why the tee option is preferable</em></p>\n\n<p>Assuming you have appropriate permission to execute the command that creates the output, if you pipe the output of your command to tee, you only need to elevate tee\'s privledges with sudo and direct tee to write (or append) to the file in question.</p>\n\n<p>in the example given in the question that would mean:</p>\n\n<pre><code>ls -hal /root/ | sudo tee /root/test.out\n</code></pre>\n\n<p>for a couple more practical examples:</p>\n\n<pre><code># kill off one source of annoying advertisements\necho 127.0.0.1 ad.doubleclick.net | sudo tee -a /etc/hosts\n\n# configure eth4 to come up on boot, set IP and netmask (centos 6.4)\necho -e "ONBOOT=\\"YES\\"\\nIPADDR=10.42.84.168\\nPREFIX=24" | sudo tee -a /etc/sysconfig/network-scripts/ifcfg-eth4\n</code></pre>\n\n<p>In each of these examples you are taking the output of a non-privileged command and writing to a file that is usually only writable by root, which is the origin of your question.</p>\n\n<p>It is a good idea to do it this way because the command that generates the output is not executed with elevated privileges.  It doesn\'t seem to matter here with <code>echo</code> but when the source command is a script that you don\'t completely trust, it is crucial.</p>\n\n<p>Note you can use the -a option to tee to append append (like <code>&gt;&gt;</code>) to the target file rather than overwrite it (like <code>&gt;</code>).</p>\n'}, {'answer_id': 20234210, 'author': 'jamadagni', 'author_id': 1503120, 'author_profile': 'https://Stackoverflow.com/users/1503120', 'pm_score': 2, 'selected': False, 'text': '<p>This is based on the answer involving <code>tee</code>. To make things easier I wrote a small script (I call it <code>suwrite</code>) and put it in <code>/usr/local/bin/</code> with <code>+x</code> permission:</p>\n\n<pre><code>#! /bin/sh\nif [ $# = 0 ] ; then\n    echo "USAGE: &lt;command writing to stdout&gt; | suwrite [-a] &lt;output file 1&gt; ..." &gt;&amp;2\n    exit 1\nfi\nfor arg in "$@" ; do\n    if [ ${arg#/dev/} != ${arg} ] ; then\n        echo "Found dangerous argument ‘$arg’. Will exit."\n        exit 2\n    fi\ndone\nsudo tee "$@" &gt; /dev/null\n</code></pre>\n\n<p>As shown in the <strong>USAGE</strong> in the code, all you have to do is to pipe the output to this script followed by the desired superuser-accessible filename and it will automatically prompt you for your password if needed (since it includes <code>sudo</code>).</p>\n\n<pre><code>echo test | suwrite /root/test.txt\n</code></pre>\n\n<p>Note that since this is a simple wrapper for <code>tee</code>, it will also accept tee\'s <code>-a</code> option to append, and also supports writing to multiple files at the same time.</p>\n\n<pre><code>echo test2 | suwrite -a /root/test.txt\necho test-multi | suwrite /root/test-a.txt /root/test-b.txt\n</code></pre>\n\n<p>It also has some simplistic protection against writing to <code>/dev/</code> devices which was a concern mentioned in one of the comments on this page.</p>\n'}, {'answer_id': 38021076, 'author': 'Nikola Petkanski', 'author_id': 581062, 'author_profile': 'https://Stackoverflow.com/users/581062', 'pm_score': 4, 'selected': False, 'text': '<p>The way I would go about this issue is:</p>\n\n<p>If you need to write/replace the file:</p>\n\n<pre><code>echo "some text" | sudo tee /path/to/file\n</code></pre>\n\n<p>If you need to append to the file:</p>\n\n<pre><code>echo "some text" | sudo tee -a /path/to/file\n</code></pre>\n'}, {'answer_id': 38632314, 'author': 'haridsv', 'author_id': 95750, 'author_profile': 'https://Stackoverflow.com/users/95750', 'pm_score': 4, 'selected': False, 'text': '<p>Don\'t mean to beat a dead horse, but there are too many answers here that use <code>tee</code>, which means you have to redirect <code>stdout</code> to <code>/dev/null</code> unless you want to see a copy on the screen. </p>\n\n<p>A simpler solution is to just use <code>cat</code> like this:</p>\n\n<pre><code>sudo ls -hal /root/ | sudo bash -c "cat &gt; /root/test.out"\n</code></pre>\n\n<p>Notice how the redirection is put inside quotes so that it is evaluated by a shell started by <code>sudo</code> instead of the one running it.</p>\n'}, {'answer_id': 46342096, 'author': 'user8648126', 'author_id': 8648126, 'author_profile': 'https://Stackoverflow.com/users/8648126', 'pm_score': 2, 'selected': False, 'text': '<pre><code>sudo at now  \nat&gt; echo test &gt; /tmp/test.out  \nat&gt; &lt;EOT&gt;  \njob 1 at Thu Sep 21 10:49:00 2017  \n</code></pre>\n'}]
['https://Stackoverflow.com/questions/82256', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/6910/']