How to remotely write to a file using SSH

You can use the "cat" command to create the remote file.

echo 'Some Text' | ssh user@remotehost -T "cat > /remotefile.txt"

The -T disables pseudo-terminal allocation and stops you from getting the message,

Pseudo-terminal will not be allocated because stdin is not a terminal.


A bit shorter than the other answer:

ssh user@remotehost "echo Some Text > /remotefile.txt"

It's also possible to use dd to append to a file. Maybe a bit obscure but useful if output redirection on the remote host isn't possible.

cat ~/.ssh/id_rsa.pub | ssh [email protected] 'dd of=.ssh/authorized_keys oflag=append conv=notrunc'

This example appends your public key to the authorized_keys file on the remote host.

(Source: http://www.rsync.net/resources/howto/ssh_keys.html)

Tags:

Bash

Ssh