66

I can copy a file to a remote Linux machine with no problem with

scp file user@host: /pathtowrite_file

However, I'm having difficulty writing to a file from one linux machine to another. Here is what I attempted:

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

The notice that I get is

stdin: is not a tty

At any rate, the file on the remote machine does not reflect the text sent 'Some Text'.

8 Answers 8

102

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.

9
  • but, I want to write to the existing file on the remote machine.
    – suffa
    Mar 14, 2012 at 17:36
  • 2
    the "cat > /remotefile.txt" runs on the remote machine and if you want to append to a existing file you replace ">" by ">>"
    – Aragorn
    Mar 14, 2012 at 17:42
  • @suffa: Are you trying to append to an existing file? Or do you want to overwrite the file but fail if it doesn't already exist? Or what? Mar 14, 2012 at 17:52
  • @ David Schwartz - either. I would like to overwrite, and fail if it doesn't exit ... but, I would take just appending at this point.
    – suffa
    Mar 14, 2012 at 19:06
  • 2
    ` echo 'Some Text'|ssh user@remotehost "tee -a /remotefile.txt"` worked for me as well. Oct 13, 2014 at 20:20
18

A bit shorter than the other answer:

ssh user@remotehost "echo Some Text > /remotefile.txt"
7
  • 5
    True; this will work — if writing a single line of text to the remote file is really all the OP wants to do.  The command in the question looks like a proof-of-concept test.  The question says, "writing to a file from one Linux machine to another."  If the user wants to run an arbitrary command (or sequence of commands) — and not just an echo — locally, then your answer doesn't help, and the accepted answer is the way to do it. Nov 17, 2015 at 21:44
  • @Scott Multiple commands are also possible using this approach. Like most things Linux, TMTOWTDI: ssh localhost "echo 'hi'; echo 'hello'; echo 'well, hello there!'
    – user4043
    Nov 18, 2015 at 1:01
  • 2
    OK, you want more criticism?  (1) Why in the world are you saying ssh localhost?  That's just muddying the water, not making it clearer.  (2) By not showing redirection in your modified example, you raise the question of whether you know how to group commands and send all the output to one file.  … (Cont’d) Nov 18, 2015 at 23:44
  • 2
    (Cont’d) …  (3) You’ve missed my point, by focusing on the phrase “a single line of text”.  My point was that the OP might want to do something more sophisticated that writing known text to the remote file — he might want to run a local command and send the output to the remote file, as in command (localhost) > file (remotehost).  If you can adapt your answer to address that general case, please do so. Nov 18, 2015 at 23:46
  • Excellent answer. +1 The converse is just as easy. -- I have a stable full of systems that use RSA between each other, so tend to use this to verify their hosts files to make sure the latest updates are there.. ssh remote "cat /etc/hosts" - then can append a line if that is sufficient to fix it.
    – SDsolar
    May 23, 2018 at 20:56
7

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)

0
4

This will take the contents of your clipboard on a Mac and append it to the end of a file remotely:

pbpaste | ssh [email protected] 'cat >> ~/.ssh/authorized_keys'

This allows you to write (append) to the end of a file on a remote host:

echo "Append string to file" | ssh [email protected] 'cat >> ~/.ssh/authorized_keys'
1

If has to be used multiple times might be easier to use this code. With "sshpass" tool, ssh won't prompt you for a password for each invocation of the script. (unless you need to keep it secret, then better off not use it)

For more info about sshpass : https://stackoverflow.com/questions/12202587/automatically-enter-ssh-password-with-script

#!/bin/bash

SCRIPT="echo 'nameserver 8.8.8.8' > /etc/resolv.conf"        

if [ "$#" -ne 1 ]; then
        echo "Wrong number of arguments. usage: prog dest_machine"
else
        sshpass -p "root" ssh -o StrictHostKeyChecking=no root@"$1" "${SCRIPT}"
fi
3
  • More intuitive than what?  This is essentially the same as g33kz0r’s answer (from almost two years ago), plus some bells and whistle that are not particularly clear.  At least explain the stuff you have added. Oct 1, 2017 at 10:52
  • If your script is invoked with too many arguments, it issues an error message saying that there are too few arguments. Oct 1, 2017 at 10:57
  • Yes it is almost the same you are right. The one thing it adds is that it is a bit more easy to use (if needed multiple times). And yes the error message is not good, I will fix it.
    – manish ma
    Oct 1, 2017 at 11:36
1

You can simply use vi or nano or pico editor:

# Just replace "vi" with "nano" or "pico" if you want to use them.
vi remotefile.txt

However, you will then have to write Some Text into it in the editor by yourself, so this process is not really batch-compatible.

0

Create a script as below:

# !/bin/bash

read -p "Enter target server IP : " server
echo "Enter root password for $server : " ; read -s password

yum install sshpass -y

sshpass -p "$password" ssh -o strictHostKeyChecking=no root@$server echo "your text goes here" >> /remotefile.txt
1
  • I see the downvote - most likely from someone who won't use a new command like yum without a very good reason. This answer shows how but not why. (Canceling the downvote for the moment - I hope you will edit this to provide more context)
    – SDsolar
    May 23, 2018 at 20:57
0

I do it like this if it's a multiliner:

cat <<EOF | ssh genom@$IP -T "cat >>./remotefile.txt"
line one
line two
line three
EOF

On my ubuntu unix it works without the T-option as well:

cat <<EOF | ssh genom@$IP "cat >>./remotefile.txt"
line one
line two
line three
EOF

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .