SSH – Can’t Connect to iPhone: Connection Closed by Remote Host

iphonenetworkingopensshsshwireless-networking

I'm trying to establish ssh connection from my laptop(macbookpro) to my iphone.
when trying to copy keys from laptop to iphone getting:

$ ssh-copy-id -i ~/.ssh/id_rsa.pub mobile@iphone.local
/usr/local/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed

/usr/local/bin/ssh-copy-id: ERROR: ssh_exchange_identification: Connection closed by remote host

When trying to ssh just with password authentication getting basically the same:

$ ssh -vvv mobile@iphone.local
OpenSSH_6.2p2, OSSLShim 0.9.8r 8 Dec 2011
debug1: Reading configuration data /etc/ssh_config
debug1: /etc/ssh_config line 20: Applying options for *
debug1: /etc/ssh_config line 102: Applying options for *
debug2: ssh_connect: needpriv 0
debug1: Connecting to iphone.local [192.168.1.53] port 22.
debug1: Connection established.
debug3: Incorrect RSA1 identifier
debug3: Could not load "/Users/Andryuwka/.ssh/id_rsa" as a RSA1 public key
debug1: identity file /Users/Andryuwka/.ssh/id_rsa type 1
debug1: identity file /Users/Andryuwka/.ssh/id_rsa-cert type -1
debug1: identity file /Users/Andryuwka/.ssh/id_dsa type -1
debug1: identity file /Users/Andryuwka/.ssh/id_dsa-cert type -1
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_6.2
ssh_exchange_identification: Connection closed by remote host

scanning of port 22 of my iphone gives this:

$ sudo nmap -sS -sV -p 22 iphone.local
Password:

Starting Nmap 6.47 ( http://nmap.org ) at 2015-08-09 14:43 PDT
Nmap scan report for iphone.local (192.168.1.53)
Host is up (0.096s latency).
PORT   STATE SERVICE    VERSION
22/tcp open  tcpwrapped
MAC Address: D8:BB:2C:83:F8:84 (Unknown)

Service detection performed. Please report any incorrect results at http://nmap.org/submit/ .
Nmap done: 1 IP address (1 host up) scanned in 0.45 seconds

"tcpwrapped" state of port instead of OpenSSH version is really strange and confusing… But I believe it shows that something wrong with server config.

Keys are fine, because I can use them to connect to another host successfully. Trying to generate another keys – did not make any changes.

iphone /etc/ssh/sshd_config:

SyslogFacility AUTHPRIV
#LogLevel INFO

PermitRootLogin yes
#PermitRootLogin without-password
#StrictModes yes
#MaxAuthTries 6
#MaxSessions 10

#RSAAuthentication yes
#PubkeyAuthentication yes

# The default is to check both .ssh/authorized_keys and .ssh/authorized_keys2
# but this is overridden so installations will only check .ssh/authorized_keys
AuthorizedKeysFile  .ssh/authorized_keys

# Kerberos options
KerberosAuthentication yes
KerberosOrLocalPasswd yes
KerberosTicketCleanup yes

# GSSAPI options
GSSAPIAuthentication yes
GSSAPICleanupCredentials yes
GSSAPIStrictAcceptorCheck yes
GSSAPIKeyExchange no

UsePrivilegeSeparation sandbox      # Default for new installations.

AcceptEnv LANG LC_*

Subsystem   sftp    /usr/libexec/sftp-server

What's the problem? Can anybody point me to the right direction?

OpenSSH version on iphone:

$ ssh -V
OpenSSH 6.7p1, OpenSSL 0.9.8zg 11 Jun 2015

I believe, i launched the process correctly with

# launchctl load /Library/LaunchDaemons/com.openssh.sshd.plist

I've already tried to remove known_hosts and all /etc/ssh/ssh_host_ on iphone… Run out of ideas.((

UPDATE

Tried this:

$ telnet iphone.local 22
Trying 192.168.1.53...
Connected to iphone.local.
Escape character is '^]'.
Connection closed by foreign host.

==> No sshd banner.

EDIT

I've checked if the server runs on my iphone:

ps aux | grep [s]shd

I should have got a line such as:

root 749 0.0 0.0 55164 5428 ? Ss Aug09 0:00 /usr/sbin/sshd -D

but i didn't. Seems like it was my problem. Now I have to find out how to start the process on iOS.

Best Answer

Ok, we work our way down:

Your ssh connection attempt output mentions this error:

debug3: Incorrect RSA1 identifier
debug3: Could not load "/Users/Andryuwka/.ssh/id_rsa" as a RSA1 public key
  1. Does this file exist?
  2. Can this identity be used to login to other hosts (= is not corrupted)
  3. If you move this identity, and generate a new one with ssh-keygen, do you still get the same error?

Concerning the nmap output:

Do you know about https://secwiki.org/w/FAQ_tcpwrapped : "Specifically, it means that a full TCP handshake was completed, but the remote host closed the connection without receiving any data."

==> Do you get an SSH server banner when you:

telnet iphone.local 22

? Should look something like:

# telnet localhost 22
Trying ::1...
Connected to localhost.
Escape character is '^]'.
SSH-2.0-OpenSSH_<SomeVersionNumer>

==> Can you connect with an SSH-Client on the iphone to the SSH-Server on the iphone?

==> If not, can you confirm, the process is running on the iphone? e.g.:

ps aux | grep [s]shd

You should get a line such as:

root 749 0.0 0.0 55164 5428 ? Ss Aug09 0:00 /usr/sbin/sshd -D

If not, there is your problem: There is no sshd running

Related Question