3

I wish to write text from stdin to a file that is owned by root.

I'm doing this programmatically across an SSH connection - essentially from a script - so using a text editor is out of the question. There is no terminal. The process is entirely automated.

Given that [a] root elevation can be obtained via sudo, and [b] files can be written to using cat with redirection, I assumed the following would work:

ssh user@host sudo cat >the-file

Unfortunately, the redirection is applied to sudo, not to cat. How can I apply redirection to cat in this example?

Sod Almighty
  • 1,768
  • 1
  • 16
  • 29
  • In fact, you could use a text editor even across a SSH connection, editors like vim or nano should work fine. Try to explain better your case with SSH connection. You already logged when you try to redirect? – Paulo Alexandre Aug 21 '16 at 23:04
  • Your sudo command does not appear to be correct. It should be `sudo -u user -h host cat > the-file`. That works on my system. "I'm doing this across an SSH connection, so using a text editor is out of the question". That conclusion is not as self evident as you make it. Editors such as `vim`, `nano` and even `emacs -nw` work just fine in a terminal. – kaylum Aug 21 '16 at 23:05
  • @John1024 yes, I could use tee, but I'd rather fix the redirect problem directly. – Sod Almighty Aug 21 '16 at 23:41
  • @paulequilibrio I'm calling the command programmatically. I can issue commands, but I don't have a terminal. – Sod Almighty Aug 21 '16 at 23:41
  • @kaylum That'd be great if `-h` was a standard parameter to `sudo` across all platforms. On a Mac, for example, there is no such option. You were right that my command was wrong: I've edited the question to fix it. – Sod Almighty Aug 21 '16 at 23:42
  • @John1024 It means your `tee` solution doesn't use redirects. I want to get redirection working so I can use `cat`, or any other program in the future that requires redirection. Please see my amended commandline. – Sod Almighty Aug 21 '16 at 23:44
  • @SodAlmighty Might be a good idea to tag or mention your system then. Macs don't run Linux (which this question is tagged with). – kaylum Aug 21 '16 at 23:47
  • @John1024 Nope, that doesn't work. `ssh user@host sudo bash -c 'ls >/test'` results in "bash: /test.: Permission denied". And of course, if `ls` redirection fails, obviously `cat` redirection will fail also. – Sod Almighty Aug 21 '16 at 23:50
  • @kaylum The server is running Linux, and my question concerned the arguments to `ssh`, and subsequently to `sudo` *on the target system*. My question didn't ask for `ssh` to be omitted entirely and `sudo` to be used directly; therefore my client system is irrelevant to the question. – Sod Almighty Aug 21 '16 at 23:52
  • @SodAlmighty I see that you have now completely changed the question. – John1024 Aug 21 '16 at 23:53
  • No, I've clarified it, by moving a paragraph up, and correcting my commandline. I *did* say I was using SSH. Are you *trying* to be argumentative? Sure, I made a mistake in my quoted commandline, but aside from that I have not "changed my question". – Sod Almighty Aug 21 '16 at 23:54
  • Similar question: http://stackoverflow.com/q/82256/5201578 – Paulo Alexandre Aug 22 '16 at 00:43

1 Answers1

6

The normal pattern to do this is:

ssh user@host 'cat | sudo tee the-file'

tee redirects output to a file and can be run with sudo.

If you want to mimic >> where the file is appended-to, use sudo tee -a.

You'll also need to be sure to quote your command as above, so that the pipe isn't interpreted by the local shell.

Edit

The tee command is part of POSIX, so you can rely on it existing.

To redirect Standard Error as well:

ssh user@host 'some_command 2>&1 | sudo tee output-file'
Will
  • 24,082
  • 14
  • 97
  • 108
  • So redirection *cannot* be used in a `sudo` command? For example, if `tee` (and I suppose `dd`) didn't exist on the system, I'd be boned? Or if I wanted to redirect stderr as well, I'd be boned then also? – Sod Almighty Aug 22 '16 at 00:02
  • See my edit :) Yeah, I mean, you could probably use `ssh 'sudo sh -c "cat >the-file"'`, but you'll quickly get into "two many levels of quoting" problems and I'm unsure if redirection will work properly through `sh -c` over ssh :) – Will Aug 22 '16 at 01:17
  • 1
    You can use redirection with any command you like, but the redirection takes place before the command runs. (In fact, your question is wrong; the redirection applies to the entire `ssh` command line, not to `sudo`.) You could so something like `sudo sh -c 'cat >output-file'` to get privileged redirection, but `sudo tee` is much simpler. With `sudo`, you should always strive to restrict the privileged operation to the simplest possible command. – tripleee Aug 22 '16 at 05:48
  • `tee` obviously also prints to standard output; you can use `tee file >/dev/null` to avoid having the results display on the console. – tripleee Aug 22 '16 at 05:50