LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 09-14-2016, 09:57 AM   #1
tonj
Member
 
Registered: Sep 2008
Posts: 546

Rep: Reputation: 37
error: ssh_exchange_identification: Connection closed by remote host


I have a remote ssh connection problem I need help with.
I have machine A which is a vps running centos 6 on the internet with public ip address.
I have two centos6 machines at home on my LAN, machine B and machine C.
Machine B ssh access is on the default port 22
Machine C ssh access is on a different port - 1234.
From machine B I can run:
# sshpass -p <password> ssh -t -t -R8023:localhost:22 <user>@<ip-addrr-machine-A)
and then I can log in to machine B remotely from machine A using this command:
# ssh -p8023 localhost
However if I try the same thing from machine C (ssh port 1234) machine A throws error:
Code:
ssh_exchange_identification: Connection closed by remote host
The command I run on machine C is:
# sshpass -p <password> ssh -t -t -R8025:localhost:22 <user>@<ip-addrr-machine-A)
and on machine A I then run:
# ssh -p8025 localhost
and that's when I get the ssh_exchange... error.
The problem is machine C is talking on ssh port 1234 and machine A is coming back on port 22. I've checked this by changing machine C temporarily back to the default port 22 and the remote connection from machine A then works fine.
So to connect to machine C how can I make machine A come back on port 1234?

Last edited by tonj; 09-14-2016 at 11:23 AM.
 
Old 09-15-2016, 04:28 AM   #2
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,328
Blog Entries: 3

Rep: Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726
Quote:
Originally Posted by tonj View Post
However if I try the same thing from machine C (ssh port 1234) machine A throws error:
Code:
ssh_exchange_identification: Connection closed by remote host
The command I run on machine C is:
# sshpass -p <password> ssh -t -t -R8025:localhost:22 <user>@<ip-addrr-machine-A)
and on machine A I then run:
# ssh -p8025 localhost
and that's when I get the ssh_exchange... error.
The problem is machine C is talking on ssh port 1234
Shouldn't you make the reverse tunnel that originates from C use port 1234 instead of 22?

Code:
ssh -t -t -R8025:localhost:1234 <user>@<ip-addrr-machine-A>
Rather than "sshpass", I would recommend using keys for the connection instead. You can make an extra key just for the purpose of tunneling, give it a dummy ForceCommand and then connect using -f to make sure that the connection goes into the background.
 
Old 09-15-2016, 04:51 AM   #3
tonj
Member
 
Registered: Sep 2008
Posts: 546

Original Poster
Rep: Reputation: 37
thanks for your response but making the reverse tunnel that originates from C use port 1234 instead of 22 doesn't work because no matter what port number I use machine A always comes back on port 22. Unfortunately I don't know how to use the keys method you mentioned.
 
Old 09-15-2016, 05:00 AM   #4
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,328
Blog Entries: 3

Rep: Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726
Ok. We'll deal with the keys later.

Just for clarification, is this what you have?

Machine A is on the open Internet and listening for SSH on port 22
Machine B is on the LAN and listening for SSH on port 22
Machine C is on the LAN and listening for SSH on port 1234

You have a reverse tunnel from B to A.
You have a reverse tunnel from C to A.

And you are trying to connect via A to either B or C using said tunnels?
 
Old 09-15-2016, 05:15 AM   #5
tonj
Member
 
Registered: Sep 2008
Posts: 546

Original Poster
Rep: Reputation: 37
Code:
Machine A is on the open Internet and listening for SSH on port 22
Machine B is on the LAN and listening for SSH on port 22
Machine C is on the LAN and listening for SSH on port 1234
yes all correct
Code:
You have a reverse tunnel from B to A.
yes and it works
Code:
You have a reverse tunnel from C to A.
yes that tunnel connects but A can't come back to C because of the port number.
Code:
And you are trying to connect via A to either B or C using said tunnels?
yes correct, A --> B works but A --> C fails with "ssh_exchange_identification: Connection closed by remote host"

Last edited by tonj; 09-15-2016 at 05:19 AM.
 
Old 09-15-2016, 05:20 AM   #6
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,328
Blog Entries: 3

Rep: Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726
Ok. Then I am puzzled. If you create a tunnel from C to A:

Code:
# on machine C
ssh -t -t -R8025:localhost:1234 <user>@<ip-addrr-machine-A>
Then you should be able to connect from A to C with port 8025.

Code:
# on machine A
ssh -p 8025 localhost
Or is there something listening already on port 1234 on machine C?

Code:
# on machine C
sudo netstat -ntlp | grep 1234
 
Old 09-15-2016, 07:21 AM   #7
tonj
Member
 
Registered: Sep 2008
Posts: 546

Original Poster
Rep: Reputation: 37
1) yes you're right, I should be able to connect from A to C with port 8025 but it won't let me unless machine C is on port 22, it won't do it while it's on port 1234.
2) there's nothing on machine C already listening on 8025, I've verified this by running # fuser 8025/tcp, it comes back with nothing.
3) just for more info: I've watched the log on machine C, it verifies the connection to A with this line:
"Last login: Thu Sep 15 13:02:12 2016 from <my public ip addres>"
And on machine A /var/log/secure also verifies the connection with:
Sep 15 13:14:13 vps sshd[21145]: pam_unix(sshd:session): session opened for user <user> by (uid=0)
Sep 15 13:15:03 vps sshd[21201]: Accepted password for <user> from <my-public-ip-address> port 49791 ssh2
However when I run #ssh -p8025 localhost on machine A the log on machine C shows:
"[<user>@<machine A>]$ connect_to localhost port 22: failed."
Machine A is coming back on port 22.

Last edited by tonj; 09-15-2016 at 07:28 AM.
 
Old 09-15-2016, 08:01 AM   #8
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,328
Blog Entries: 3

Rep: Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726
Quote:
Originally Posted by tonj View Post
2) there's nothing on machine C already listening on 8025, I've verified this by running # fuser 8025/tcp, it comes back with nothing.
With the setup you have described, 8025 is used on machine A. It is machine C that would be using port 1234, according to the plan above. On machine C, check port 1234 with fuser 1234/tcp It should say that it being used. You should be able to see more clearly using the "netstat" options in the earlier post that it is use by "sshd".

Edit: Just to be sure, what are you typing on Machine C?

Code:
# on machine C, right
ssh -t -t -R8025:localhost:1234 <user>@<ip-addrr-machine-A>

# on machine C, wrong
ssh -t -t -R8025:localhost:22 <user>@<ip-addrr-machine-A>

Last edited by Turbocapitalist; 09-15-2016 at 08:12 AM.
 
Old 09-15-2016, 10:12 PM   #9
tonj
Member
 
Registered: Sep 2008
Posts: 546

Original Poster
Rep: Reputation: 37
ah yes that works!
ssh -t -t -R8025:localhost:1234 <user>@<ip-addrr-machine-A>
silly me I was initiating the original tunnel on port 22. I can now remotely log in to machine C from machine A. Thanks very much for your help on this turbocapitalist.
 
Old 09-15-2016, 10:36 PM   #10
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,328
Blog Entries: 3

Rep: Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726
You're welcome. I'd recommend arranging for key-based authentication next. And then once you have that, it's simple once you know how, some single purpose keys that do nothing but make a tunnel. Password authentication is almost universally frowned upon these days.
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
ssh_exchange_identification: Connection closed by remote host verWire3619 Linux - Networking 1 09-06-2011 10:47 AM
ssh_exchange_identification: Connection closed by remote host 2kau Linux - Networking 2 05-10-2011 02:43 PM
ssh_exchange_identification: Connection closed by remote host sailu_mvn Linux - Networking 5 07-09-2008 09:47 AM
Error : ssh_exchange_identification: Connection closed by remote host sachinh Linux - Networking 2 04-03-2008 03:26 AM
ssh_exchange_identification: Connection closed by remote host ctav01 Linux - Networking 1 08-22-2003 07:14 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 08:06 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration