How to remotely write to a file using SSH

44

13

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

suffa

Posted 2012-03-14T17:22:17.453

Reputation: 595

Answers

74

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.

Aragorn

Posted 2012-03-14T17:22:17.453

Reputation: 1 310

echo 'Some Text'|ssh user@remotehost "tee -a /remotefile.txt" worked for me as well. – Simply Seth – 2014-10-13T20:20:29.953

@Aragorn I tried your command but got an permission denied after it. What could be the reason? I already tried it with sudo too – Jenson – 2016-01-28T15:50:35.147

@Jenson the file "/remotefile.txt" is in the root directory and in a normal system that is restricted to the root user – Aragorn – 2016-01-29T05:58:34.600

but, I want to write to the existing file on the remote machine. – suffa – 2012-03-14T17:36:21.913

2the "cat > /remotefile.txt" runs on the remote machine and if you want to append to a existing file you replace ">" by ">>" – Aragorn – 2012-03-14T17:42:56.053

@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 – 2012-03-14T17:52:08.523

@ 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 – 2012-03-14T19:06:59.697

12

A bit shorter than the other answer:

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

g33kz0r

Posted 2012-03-14T17:22:17.453

Reputation: 284

3True; 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 – 2015-11-17T21:44:08.200

@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 – 2015-11-18T01:01:16.637

Cool dude, thanks for the downvote. I guess that's easier than actually addressing my point? PS your comment above makes no sense. – g33kz0r – 2015-11-18T19:36:44.230

1OK, 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 – 2015-11-18T23:44:25.797

1(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 – 2015-11-18T23:46:38.670

i don't think asking you to not arbitrarily downvote and adequately explain yourself is asking for criticism, but yeah this is the internet. – g33kz0r – 2015-11-19T03:42:17.613

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 – 2018-05-23T20:56:01.480

6

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)

sdl

Posted 2012-03-14T17:22:17.453

Reputation: 61

3

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'

David

Posted 2012-03-14T17:22:17.453

Reputation: 131

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

sergeyrar

Posted 2012-03-14T17:22:17.453

Reputation: 111

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 – 2017-10-01T10:52:45.803

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' – 2017-10-01T10:57:00.873

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. – sergeyrar – 2017-10-01T11:36:04.533

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.

Riptide9.7

Posted 2012-03-14T17:22:17.453

Reputation: 11

-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

Abhilash Mishra

Posted 2012-03-14T17:22:17.453

Reputation: 9

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 – 2018-05-23T20:57:53.740