How to Use SSH Pipes on Linux

by Joana Maria
17 minutes read

How to Use SSH Pipes on Linux

A photo of a male working in front of his computer system.

UNIX tubes are a monumental reaction in the modern technology of UNIX and UNIX-favor operating tools. It enabled borrowers to perform intricate scheming job-related by tying together the input and result of typical powers. This brief post extends on that by showing you how to consumption a UNIX tube in Linux over a network via the SSH protocol.

Web content
  • Knowing the Unix Pipe
  • Immediately Transfer Compressed Brochures
  • Emboldening and Retrieving Documents from Remote Hosts
  • Stabilize Up and Repossessing Drives Remotely
  • Redirecting Audio Input to a Remote Fabricator
  • Flowing Reside Video clip from a Remote Web digital camera
  • Printing Message on a Remote Console
  • Piping Remote Information to a Hamlet Clipboard

Pointer: If you call for a refresher, learn how tube and redirection job-related in Linux.

Knowing the Unix Pipe

Pipes on Unix (and by expansion, Linux) are brought out to chain powers together and gain them crew up. For example, utilising cat, you can depict the ingredients of a documents, but if you brought out a tube (|), you could chain the cat command to the more command to gain the documents less complex to read using.

cat file1 | more

The typical referral listed below is this: program1 fileX | program2. It’s not merely banned to one documents and two powers, but. Piping can gain consumption around as builded as you call for it to be via as multiple modifiers as you can image.

Listed below are some strategies to gain nice consumption of tube (|) in SSH situations.

On a side note: learn some of the extravagant pivots to resolve your SSH server.

Immediately Transfer Compressed Brochures

One of the the majority of usual strategies of utilising UNIX tubes is for keeping a regiment’s result to a documents someplace in the regional system. For example, dashing echo "Hello, MakeTechEasier!" | tee Hello will sprinted the regiment echo while in a tantamount means keeping the string “Hello there, MakeTechEasier!” within the documents “Hello there.”

A terminal showing a typical UNIX tube redirection in a regional contraption.

That asserted, you can consumption this referral to transmit brochures across two Linux hosts. To take on that, read the folder that you pine to send utilising tar, then tube that to your SSH daemon:

tar czf - "~/Documents/myfolder" | ssh ramces@remote.host "tar xzf - -C ~/Documents/"

This command will scheme upward your folder right into a tar archive and send it to the command’s typical result. The UNIX tube will then read that documents and send it to your confidential Linux host utilising SSH.

You can in a tantamount means overturn this command to gain consumption your records out of confidential hosts:

ssh ramces@remote.host "tar czf - ~/Documents/myfolder" | tar xzf - -C "~/Documents/"

Extensive to detect: learn how you can unleash encrypted cloud earlier-ups utilising rclone.

Emboldening and Retrieving Documents from Remote Hosts

You can in a tantamount means consumption tubes and SSH to send borrower records over the network. This jobs by utilising cat as a means to agglomeration the ingredients of a documents and send that using SSH:

cat < my.local.file | ssh ramces@remote.host "cat > my.remote.file"

The confidential host will purchase the result stream from the regional pet cat procedure and reconstruct the documents as is.

To purchase a documents from a confidential host, you should overturn the ordinance of the command and equip the training course for your confidential documents:

ssh ramces@remote.host "cat < my.remote.file" | cat > my.local.file
A terminal showing borrower documents transmit utilising an SSH tube in Linux.

Stabilize Up and Repossessing Drives Remotely

Comparable to sending records and brochures, it is you can perhaps imagine to earlier upward entire drives in Linux from another place via a UNIX tube and SSH. This can be convenient if you pine to unleash rapid offsite earlier-ups and you wear’t have a spare physical drive at the moment.

To replacement an entire drive, sprinted dd via its “if=” variable kit to the drive that you pine to replacement, then tube that to your SSH daemon:

sudo dd if=/dev/sda | ssh ramces@remote.host "dd of=sda.img"

Repealing this command in a tantamount means enables you to bring a disk image from a confidential contraption to a physical disk:

ssh ramces@remote.host "dd if=sda.img" | sudo dd of=/dev/sda

Further, this SSH tube phrase attractiveness will in a tantamount means job-related via distinctive disk dividings. For example, if your system has a /abode partition in “/dev/sda4” you can sprinted the subsequent command to unleash a replacement of it:

sudo dd if=/dev/sda4 | ssh ramces@remote.host "dd of=home.img"

FYI: learn more around the history and you can consumption the dd command to unleash disk pics.

Redirecting Audio Input to a Remote Fabricator

One of the paybacks of SSH tubes is that they make it possible for you to share via confidential machines as if they are a regional resource. This involves the capacity to tap right into contraption records such as the audio input of a system.

To take on this, sprinted a confidential ALSA subshell utilising SSH and send its result to your regional ALSA daemon:

ssh ramces@remote.host "arecord -f cd" | aplay

This will listen to the default audio input contraption on the confidential contraption and play what it is hearing on your system. That asserted, turning the commands around will send your regional contraption’s audio input to your confidential host’s audio result:

A terminal showing an energetic audio stream utilising SSH tubes.
arecord -f cd | ssh ramces@remote.host "aplay"

The ALSA SSH tube will in a tantamount means job-related once you fuse it via other audio playback tools. For instance, you can send the arecord result from an SSH tube to ffmpeg:

ssh ramces@remote.host "arecord -f cd" | ffplay -nodisp -

Flowing Reside Video clip from a Remote Web digital camera

Another diligent consumption of SSH tubes in Linux is by spurting live video clip webcam feeds. Just favor via audio, this enables you to use a confidential host’s contraption and render its result on your regional contraption.

To stream from a confidential host’s webcam, sprinted SSH via an ffmpeg subshell then tube it to a video clip playback target on your regional contraption:

ssh ramces@remote.host "ffmpeg  -r 14 -s 640x480 -f video4linux2 -i /dev/video0 -f matroska -" | mpv --demuxer=mkv /dev/stdin

This command will stream the raw video clip result from the initially webcam on your confidential contraption.

A screenshot showing an energetic webcam video clip feed going using an SSH tube in Linux.

It is in a tantamount means you can perhaps imagine to record the footage from your confidential webcam to a detect documents. You can take on this by sending the documents from the SSH tube to tee before rerouting it to your video clip player:

ssh ramces@remote.host "ffmpeg  -r 14 -s 640x480 -f video4linux2 -i /dev/video0 -f matroska -" | tee my_recording.mkv | mpv --demuxer=mkv /dev/stdin

Unconcerned from audio and video clip, you can in a tantamount means consumption SSH tubes to send raw message on a confidential contraption’s TTY. This is priceless if you pine to send stature articles to a system via no GUI.

To boosting, unleash a FIFO tube on your regional contraption:

mkfifo my-fifo

Rushed a paying attention tail command via your FIFO and send its result to an SSH daemon:

tail -f my-fifo | ssh root@remote.host "cat > /dev/tty0"

Examination whether your brand name-modern FIFO tube is working over the network by sending message documents utilising the resemble command:

echo "Hello, MakeTechEasier!" > my-fifo
A terminal showing approximate message in a contraption's TTY console.

Tab: Sending out message to your contraption’s TTY will single job-related if you are logged in as that contraption’s origin account.

Piping Remote Information to a Hamlet Clipboard

The best drawback of a system clipboard is that it single jobs via the regional contraption. This is a top priority if you’re working via multiple computers and you pine to transmit documents without inflicting brief records.

One means to heal this is to unleash an SSH tube that can read and send a confidential documents right to your regional system clipboard:

ssh ramces@remote.host "cat < ~/ramces.txt" | xclip -sel clipboard

This command will fasten to your confidential contraption, sprinted the pet cat utility then boosting reading the “ramces.txt” documents. When caught on out, it will send the confidential documents earlier to your regional contraption and reroute that to your system clipboard.

A terminal showing the clipboard of the regional contraption after an SSH tube.

You can in a tantamount means press your system’s current clipboard as a documents on your confidential contraption by utilising the subsequent command:

xclip -sel clipboard -o | ssh ramces@remote.host "cat > ~/clip.txt"

Spotting out how to send documents across a network utilising UNIX tubes and SSH is merely the initially reaction in realising how computer system networks job-related. Uncover out more around your network by security wherein your packages go utilising Traceroute.

Image credit: Mikhail Fesenko using Unsplash. All fluctuations and screenshots by Ramces Red.

Related Posts