53

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'.

83

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.

  • but, I want to write to the existing file on the remote machine. – suffa Mar 14 '12 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 '12 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? – David Schwartz Mar 14 '12 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 '12 at 19:06
  • 2
    ` echo 'Some Text'|ssh user@remotehost "tee -a /remotefile.txt"` worked for me as well. – Simply Seth Oct 13 '14 at 20:20
15

A bit shorter than the other answer:

ssh user@remotehost "echo Some Text > /remotefile.txt"
  • 4
    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. – Scott Nov 17 '15 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!' – g33kz0r Nov 18 '15 at 1:01
  • Cool dude, thanks for the downvote. I guess that's easier than actually addressing my point? PS your comment above makes no sense. – g33kz0r Nov 18 '15 at 19:36
  • 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) – Scott Nov 18 '15 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. – Scott Nov 18 '15 at 23:46
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 user@remote.host '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)

4

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

pbpaste | ssh root@my.machine.remote '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 root@my.machine.remote '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
  • 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. – Scott Oct 1 '17 at 10:52
  • If your script is invoked with too many arguments, it issues an error message saying that there are too few arguments. – G-Man Says 'Reinstate Monica' Oct 1 '17 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 '17 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.

-1

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
  • 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 '18 at 20:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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