"Connection to localhost closed by remote host." when rsyncing over ssh - ssh

I'm trying to set up an automatic rsync backup (using cron) over an ssh tunnel but am getting an error "Connection to localhost closed by remote host.". I'm running Ubuntu 12.04. I've searched for help and tried many solutions such as adding ALL:ALL to /etc/hosts.allow, check for #MaxStartups 10:30:60 in sshd_config, setting UsePrivilegeSeparation no in sshd_config, creating /var/empty/sshd but none have fixed the problem.
I have autossh running to make sure the tunnel is always there:
autossh -M 25 -t -L 2222:destination.address.edu:22 pbeyersdorf#intermediate.address.edu -N -f
This seems to be running fine, and I've been able to use the tunnel for various rsync tasks, and in fact the first time I ran the following rsync task via cron it succeeded:
rsync -av --delete-after /tank/Documents/ peteman#10.0.1.5://Volumes/TowerBackup/tank/Documents/
with the status of each file and the output
sent 7331634 bytes received 88210 bytes 40215.96 bytes/sec
total size is 131944157313 speedup is 17782.61
Ever since that first success, every attempt gives me the following output
building file list ... Connection to localhost closed by remote host.
rsync: connection unexpectedly closed (8 bytes received so far) [sender]
rsync error: unexplained error (code 255) at io.c(605) [sender=3.0.9]
An rsync operation of a smaller subdirectory works as expected. I'd appreciate any ideas on what could be the problem.

It seems the issues is related to autossh. If I create my tunnel via ssh instead of autossh it works fine. I suspect I could tweak the environment variables that affect the autossh configuration, but for my purposes I've solved the problem by wrapping the rsycn command in a script that first opens a tunnel via ssh, executes the backup then kills the ssh tunnel, thereby eliminating the need for the always open tunnel created by autossh:
#!/bin/sh
#Start SSH tunnel
ssh -t -L 2222:destination.address.edu:22 pbeyersdorf#intermediate.address.edu -N -f
#execute backup commands
rsync -a /tank/Documents/ peteman#localhost://Volumes/TowerBackup/tank/Documents/ -e "ssh -p 2222"
#Kill SSH tunnel
pkill -f "ssh.*destination.address"

Related

scp: "Host key verification failed. lost connection" when attempting to copy files from remote server to WSL

I have a user at a remote server, let's call it remote_user#remote_server.
I also have a user on my WSL2 Ubuntu, let's call it wsl_user#<localhost>.
When I tried to use the command scp -v -o StrictKeyChecking=no remote_user#remote_server:/path/to/file.txt wsl_user#<localhost>:/path/to/directory on my host computer, it asked for the remote server's password (which successfully authenticates), but then it outputs
Host key verification failed.
lost connection
when I use localhost as <localhost>.
I have tried using both the IP address of the host computer and the IP address of the WSL2 instance, but both just hangs, and then does a Connection timed out.
P.S: I can ssh into both of them.
Well, I somehow kinda circumvented the problem using by breaking it into smaller commands, aka
scp -v remote_user#remote_server:/path/to/file.txt file.txt\
&& scp -v file.txt wsl_user#localhost:/path/to/directory \
&& rm file.txt

Runing rsync to new website gives connection refused

We have an inhouse backup server (ubuntu) the inhouse server calls numerous remote servers using rsync. In order to set this up with a new website i need to ssh into the remote server and add my key to the authorized_keys file. Once i can login to the remote site via ssh from the backup server the rsync is then ran manually to build the structure (no reason for this but to confirm and to speed the backup up).
Today however I'm trying to add our newest website to the backup but the rsync command gives a 255 error and fails to connect due to a connection refused issue.
To confirm:
The remote server is lightsail with lampstack
We have multiple sites being backed up with lightsail and we use other servers too
Yes I can ssh into the remote site from on the backup server so key is correct and matches whats used in the rsync command
The rsync is generated and copied and pasted and has worked before
The .ssh folder on remote is 0700 and the authorised_keys is 600 and owner is bitnami
The pem file is in the correct folder /var/www/.ssh on backup server
The user I'm logged in as on the remote server when i run this is www-data (for ssh and rsync)
simplified rsyn command is:
rsync -rLDvvvcs -e "ssh -i /var/www/.ssh/LightsailKey.pem -p 22 -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null" --progress --exclude-from '/path/to/exclude.txt' --delete --backup --backup-dir=/deleted_files/project-name/ --chmod=Du=rwx,Dgo=rx,Fu=rw,Fgo=r bitnami#{ip}:/home/bitnami/live/my-website/htdocs/ /mnt/incs/project-name/htdocs
Error from running this is
ssh: connect to host {ip} port 22: Connection refused
rsync: connection unexpectedly closed (0 bytes received so far) [Receiver]
rsync error: unexplained error (code 255) at io.c(235) [Receiver=3.1.2]
[Receiver] _exit_cleanup(code=12, file=io.c, line=235): about to call exit(255)
What am i missing with this?
thanks
Turned out it was a typo in the ip field in the database.

How to specify RemoteForward in the ssh config file?

I'm trying to setup a an ssh tunnel with remote port forwarding. The idea is the have a VPS act as a means to ssh into remote deployed systems (which currently incorporate a Raspberry Pi). Everything seems to work, but I run into issues when trying to move all arguments into the ~/.ssh/config file.
what does work is the setting of the HostName, User, Port and IdentityFile. However setting the RemoteForward parameter does not seem to work.
The following works:
ssh -R 5555:localhost:22 ssh-tunnel
How ever when using the following line in the config file;
Host ssh-tunnel
...
RemoteForward 5555 localhost:22
The following command returns the message "Bad remote forwarding specification 'ssh-tunnel'"
ssh -R ssh-tunnel
Obvious I found the answer almost immediately after posting the question. Using the -R flag requires you to set the remote forwarding in the command line call. However because remote forwarding is set in the config file you shouldn't add it to the command. However something confusing occurs in that aside from setting up the tunnel you also ssh into the remote server. To avoid this add the -f and the -N flag. This results in the following command:
ssh -f -N ssh-tunnel

ssh ProxyCommand fails: "forbidden char/command over SSH"

I am trying to ssh over my university's proxy server, to one of our lab's servers. The goal is to automate it with paramiko, but I am trying to first understand what's happening in the terminal level.
I tried
ssh -o ProxyCommand='ssh eran#proxy_server nc inner_server 22' eran#inner_server
And got
*** forbidden char/command over SSH: "nc inner_server 22"
This incident has been reported.
ssh_exchange_identification: Connection closed by remote host
Which I guess means the server does not allow the ProxyCommand.
Any way to achieve this in a different way?
Just to be clear, ssh to proxy_server, and then to inner_server, works fine, but doesn't produce a paramiko SSHClient instance, which is what I'm aiming for.
Do not use netcat. It is probably not allowed on the proxy server. Use -W switch:
ssh -o ProxyCommand='ssh -W %h:%p eran#proxy_server' eran#inner_server

Rsync over SSH - timeout in ssh or rsync?

I'm dealing with a crappy ISP that resets my WAN connection at random points while my script is running. I want the transfer to survive this reset and go on. I manually launch this script vs using cron / launchd currently.
I have a fairly basic script as shown below:
rsync -rltv --progress --partial -e "ssh -i <key> -o ConnectTimeout=300" <remotedir> <localdir>
Am I better off putting the timeout in the rsync section instead?
For example:
rsync -rltv --progress--partial --timeout=300 -e "ssh -i <key>" <remotedir> <localdir>
Thanks!
ConnectTimeout only applies when SSH is trying to establish the connection with the server, it doesn't have anything to do with timeouts during the data transfer. So you need to use the --timeout option to do what you want.
Try re-running the rsync. Also try without the ssh option. The job failed probably due to losing your network connection. I have an rsync job copying files between datacenters running every 2 hours via cron and it will fail about once per day.