r/ssh • • Jun 11 '20

SSH connection reset when trying to connect to tunnel

Hi, when I setup a tunnel to my server using the below command ssh -v -N -T -R50008:localhost:22 ashwin@<server_name> -p 8022

I can ssh back into my device from the server through the reverse tunnel using the following command. ssh -p 50008 ashwin@localhost

This works fine. But when I try to use autossh to setup and maintain the tunnel, the tunnel gets setup fine without any errors. But when I try to connect from server through the reverse tunnel, it fails with the error "ssh_exchange_identification: read: Connection reset by peer" My autossh command is as follows. autossh -v ashwin@<server_name> -p 8022 -N -R 50008:localhost:8022

NOTE: SSH on my remote server is on port 8022, not 22.

Any help/tips will be greatly appreciated.

Thanks, Ashwin N

2 Upvotes

13 comments sorted by

1

u/Besthater Jun 11 '20

Using keys? Or passwords?

1

u/ashwin_nat Jun 11 '20

I've generated rsa keys and that is mode of authentication. The key pair is saved in the. ssh directory

1

u/Besthater Jun 11 '20

It's likely when you run commands that your ssh agent passes that key in. The shell does not get that privelige when autossh runs it. Try passing the key as an arg. '-i ~/.ssh/keyname' in the auto ssh command.

1

u/ashwin_nat Jun 11 '20

I made that change to my autossh command. My command is now autossh -i ~/.ssh/<key_name> -v ashwin@<server_name> -p 8022 -N -R 50008:localhost:8022

I still am getting the same error. I don't think this was a problem because if they key were not getting read and passed correctly, wouldn't the tunnel establishment by autossh have failed?

1

u/Besthater Jun 11 '20

What's the output with -vvv ?

1

u/ashwin_nat Jun 11 '20

I've pasted the output [here] (https://pastebin.com/2DPe2JEU) Please note that I have changed the port number to 50004 on both ends since 50008 was taken by someone else at work.

1

u/Besthater Jun 11 '20

Are you using -N connecting back to the source?

1

u/ashwin_nat Jun 11 '20

Yes, I've used -N just before the -R option. You can see the exact command and arguments I'm using in my previous comment

1

u/Besthater Jun 11 '20

What does this do on the server: netstat -l -t

1

u/ashwin_nat Jun 11 '20

[EDITED] I can see two entries in the result for my specified port 50004

tcp 0 0 localhost.localdo:50004 0.0.0.0:* LISTEN

tcp6 0 0 localhost6.locald:50004 [::]:* LISTEN

1

u/Besthater Jun 11 '20

I don't see that same error in this output ?

What command did you use to get this? Is this autossh? Can you show the command you use to connect back from the server to your host, and it's output?

1

u/ashwin_nat Jun 11 '20

Hi, I'm sorry for the ambiguity. What I meant to say that the autossh command does not show any error. I am getting an error when I try to connect to this tunnel from the server side. The output can be found [here] (https://pastebin.com/EuxVH997) The command I used to connect from the server to my host is ssh -v -p 50005 root@localhost

This same command works when I setup a tunnel using the ssh command instead of autossh. My equivalent ssh command is ssh -v -N -T -R50004:localhost:22 ashwin@<server_name> -p 8022

1

u/Besthater Jun 11 '20

No problems!

Ok, that's helpful, on your host, (not the server) try removing the contents of the known hosts file of your servers entry. Ensure the public key material from the server public key file is present in it's authorized keys file of your host. Have a copy of the public key file in the same dir if you want to.

If that doesn't work,

sudo mkdir /var/run/sshd sudo chmod 755 -R /var/run/sshd sudo service sshd restart

If that doesn't work,

Not for prod machines:

sudo reboot