stdout on one CentOS server needs to be piped to stdin on another CentOS server. Is this possible?


ScottPack, MikeyB and jofel all have valid answers. I awarded the answer to Scott because, even though my question didn't specify security as a requirement, it's always nice to be safe. However, the other two fellows' suggestions will also work.

  • 1
    It's worth noting that the (only) major advantage of the non-ssh approach is throughput speed; if you're on a fast network and security is unnecessary this may be worth the extra inconvenience of typing two commands into two windows. – Random832 Mar 16 '12 at 12:53

This is an unabashed yes.

When one uses ssh to execute a command on a remote server it performs some kind of fancy internal input/output redirection. In fact, I find this to be one of the subtly nicer features of OpenSSH. Specifically, if you use ssh to execute an arbitrary command on a remote system, then ssh will map STDIN and STDOUT to that of the command being executed.

For the purposes of an example, let's assume you want to create a backup tarball, but don't want to, or can't, store it locally. Let's have a gander at this syntax:

$ tar -cf - /path/to/backup/dir | ssh remotehost "cat - > backupfile.tar"

We're creating a tarball, and writing it to STDOUT, normal stuff. Since we're using ssh to execute a remote command, STDIN gets mapped to the STDIN of cat. Which we then redirect to a file.

  • 11
    That isn't any sort of "fancy internal input/output redirection" - just the plain, boring regular stuff. ssh reads from STDIN, just like any other tool, and passes it to the remote process. :) – Daniel Pittman Mar 15 '12 at 20:06
  • 6
    @DanielPittman: But it's just so much more fun to call it "fancy internal" garbage. – Scott Pack Mar 15 '12 at 20:07
  • 7
    Similarly, netcat on both ends makes for a great simple, easy communication channel. tar cf - /path/to/dir | nc 5000 on one server, nc -l -p 5000 > backupfile.tar on the other. – MikeyB Mar 15 '12 at 20:14
  • 1
    @MikeyB: Good point. Netcat is a clear-text protocol so be careful with sensitive data. I tend to use netcat for more specific things like network drive acquisitions (ala dd) over a local network and port scanning. – Scott Pack Mar 15 '12 at 20:23
  • 2
    @MikeyB: You people what with your flying and your pants seats! – Scott Pack Mar 15 '12 at 20:49

A convenient way of piping data between hosts when you don't need to worry about security over the wire is using netcat on both ends on the connection.

This also lets you set them up asynchronously:

On the "receiver" (really, you'll have two-way communication, but it's easier to think of it like this), run:

nc -l -p 5000 > /path/to/backupfile.tar

And on the "sender", run:

tar cf - /path/to/dir | nc 5000
  • Very good to know. This is good if the physical connection is trusted like perhaps a backup network or if the connection is already tunneled. – Wesley Mar 15 '12 at 21:39
  • Or if the data is something that's public anyway. – Samuel Edwin Ward Mar 15 '12 at 22:24
  • 1
    +1 netcat is an invaluable tool, especially when you don't have a ssh server running. – kwarrick Mar 15 '12 at 23:31

A very powerful tool for creating uni- and bidirectional connections is socat. For a short look at the possibilities, look at the examples in its manpage.

It replaces netcat and similar tools completely and has support for ssl encrypted connections. For beginners, it might be not simple enough, but it is at least good to know that it exists.

  • 1
    @WesleyDavid: To your "Update": Just for completeness, I've added to my answer that socat has SSL support, so encryption is with socat possible, too. However, ssh is in most cases the better and easier solution, so I would have chosen ScottPack's answer,too. – jofel Mar 15 '12 at 23:46


Things only get slightly more complicated when you have a bastion server that must be used.

  1. You can pass ssh as the command to ssh like so:

    • cat local_script.sh | ssh -A usera@bastion ssh -A userb@privateserver "cat > remote_copy_of_local_script.sh; bash remote_copy_of_local_script.sh"
  2. Beware of pseudo-terminals

Note that the point of key importance here is that ssh, like most tools, just treats stdout and stdin correct by default.

However, when you start to see option like Disable pseudo-terminal allocation. and Force pseudo-terminal allocation. you may need to do a little trial and error. But, as a general rule you don't want to alter tty behavior unless you are trying to fix garbled/binary junk in a terminal emulator (what a human types in).

For example, I tend to use -At so that my workstation's ssh-agent gets forwarded, and so that running tmux remotely doesn't barf binary (like so ssh -At bastion.internal tmux -L bruno attach). And, for docker too (like so sudo docker exec -it jenkins bash).

However, those two -t flags cause some hard to track down data corruption when I try to do something like this:

# copy /etc/init from jenkins to /tmp/init in testjenkins running as a container
ssh -A bastion.internal \
ssh -A jenkins.internal \
sudo tar cf - -C /etc init | \
sudo docker exec -i testjenkins \
bash -c 'tar xvf - -C /tmp'

# note trailing slashes to make this oneliner more readable.

Try to put your ssh public key in another host just by one command

ssh root@example.com 'cat >> .ssh/authorized_keys' < .ssh/id_rsa.pub

I find this to be the easiest, after setting up no password handshaking between servers for the user you are running the command as:


tar cf - . | ssh servername "cd /path-to-dir && tar xf -"

Compression on the fly

tar czf - . | ssh servername "cd /path-to-dir && tar xzf -"
  • Using compression on the tar file is a very bad idea if your ssh is already configured for compression. – Anthon Feb 19 '14 at 19:10
  • @Anthon Why so bad, and how would one check if ssh compression is already enabled? – Tom Hale Nov 1 '16 at 9:11
  • 1
    @TomHale Depending on your systems speed, this might slow down the overall operation, as the second compression takes time, but is unlikely to shave of additional bytes. Compression could be set in any of the config files, a quick check for that is to see if ssh -v localhost exit 2>&1 | fgrep -i compress gives any output (AFAIK there is no option to dump the configuration as ssh has read it in). – Anthon Nov 1 '16 at 9:31
  • tar has a -C path flag that works for both the c and x commands. You don't have to put a separate cd command in there. (But it is good to note that you can run more than a single command.) – Bruno Bronosky Feb 21 '17 at 16:07
  • @Bruno, -C is a GNU extension (though now also supported by bsdtar and schily tar) – Stéphane Chazelas Feb 21 '17 at 16:34

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.