First of all, if I do a simple ssh admin@example.com "cat backup.tar" > backup.tar this is not happening! So, there is something else converting it which I don't know.
I have the requirement to provide a backup of the current state of the system without giving access to the server. What I have done is the following:
- Create a new user. Let's call him
username - Allow only access via ssh-key
- Put the keys into the
authorized-keys - Change the command / shell in
/etc/passwdto a custom script forusername - Custom script gets the data by doing a database dump and copy some files and eventually creates a tar archive to the
stdout - Make sure that there is no additional output to
stdout.
To get the data you need to do: ssh username@example.com > backup.tar.
Now, nearly everything is working fine except that the line ending is changed. The tar gets bigger and I can see that every line has the dos line ending with a ^M symbol. If I force dos2unix on the binary tar file the file matches the file on the server.
Why is this happening and what is changing the line ending?
I even simplified it to the following: The script on the server for username linked in /etc/passwd just make a cat .viminfo and the line endings are still transferred wrong when using the above ssh command to login as username and redirect the output to a file on my local machine.
ssh username@example.com whateverwill run the "shell" (the script used instead of a shell) with command line arguments-candwhatever. It totally depends on the script how (if) the script changes its behavior because of them. In general any program one wants to use as a shell should accept-c code. The program may ignore-cbut it shall not be surprised nor misled by it. If your script recognizes-cthen keep in mindsshusers logging in asusernameare able to use it. An edge case is when-cenables some functionality you don't want these users to have access to. – Kamil Maciorowski Apr 14 '21 at 16:14