ubuntu,ssh,sudo,restore,dump – How to Prevent \r Insertion Before \n When Retrieving Binary Files Over SSH

dumprestoresshsudoUbuntu

I have inherited a Ubuntu 14.04 production server which needs to be upgraded to 20.04, and I would like a sandboxed version to experiment with first, hence I want to dump and restore the filesystems over the network from either a MacOS or another 14.04 virtualbox instance. An earlier version of this question is at https://askubuntu.com/q/1314747/963.

The server cannot "see" my machines so I cannot easily run dump and push the result remotely to my machine, but need to invoke ssh from my machine to run dump.

ssh -t me@there "echo MYPASSWORD | sudo -S dump -y -f - /boot 2>/dev/null " > boot.dump 

Problem is that I've found that running this command inserts a lot of \r characters in front of \n characters which ruins the dump file so restore cannot use it. I understand that this is probably due to a driver translating linefeeds to the characters needed for printing, but I do not see where this is triggered.

How should I do this to get the correct binary dump file?

Best Answer

It's the ONLCR .c_oflag termios setting which is causing the newline (\n) to be turned into carriage-return/newline (\r\n) by the pseudo-terminal allocated by ssh on the remote machine (because of ssh's -t option).

Turn it off with stty -onlcr:

ssh -t me@there 'stty -onlcr; ...' > output
Related Question