SSH – SSH Shell via SSH Proxy

sshssh-tunneling

Yes, I'm aware that I wrote "SSH shell" in the title.

TL;DR: The first paragraph, the one with the link, and the one with the error message are most important.

I have my Raspberry Pi at home which I can access over the internet but only via IPv6. I'm currently in a location where I don't have IPv6.

I can execute commands on it by first logging in to a server which has both IPv4 and IPv6 and then logging into my pi from there. However, I use SSH on it for more than executing commands on it:

  • git
  • backups (Deja Dup)
  • accessing files (SFTP)
  • VNC (I tunnel through SSH and can then connect to localhost via VNC)

These are in decreasing order of importance. I want to access my git repos.

A few more details:

  • I can't simply make my Pi accessible via IPv4. The modem it's behind has an IPv4 address and an IPv6 subnet but I have to use hardware I didn't choose running software I can't change. That software is not only buggy and I can't even take a look at it, but furthermore, it doesn't allow IPv4 port forwarding it all.
  • I don't control the server with both IPv4 and IPv6 on it. I only have a normal user account on it and can't – for example – install new software if more than standard user rights are required for it.

Googling for a solution brought up this rather promising page, and it actually works for git. I set up new remotes for the repos I'm using, simply replacing the pi's domain name by localhost:3333.

But it looks much more promising than that. It looks like the solution for all of the above. And it kind of started to work out!

SFTP works and I can't really determine whether backups via Deja Dup work, yet, because my connection is too slow, but it hasn't failed yet, and something's causing network traffic, so that's good and promising.

But why can't I just do ssh localhost:3333 to connect to my laptop to get a shell on my pi? The command results in this error message:

ssh: Could not resolve hostname localhost:3333: Name or service not known

I'm mainly interested in why I can't get a shell the way I'd expect it to work.

Best Answer

You might want to look into ssh's ProxyCommand configuration, which allows for this to work more seamlessly, and will work for shells, SFTP, tunnels, and anything else you might want to proxy via ssh.

Let's say you have the following three hosts:

  • workstation.example.com - This is the machine you're physically working on
  • proxy.example.com - This is the machine you're routing your SSH traffic through
  • endpoint.example.com - This is where you want the traffic to ultimately end up

In ~/.ssh/config on workstation, add the following:

Host endpoint
    User EndpointUser # set this to the username on the destination host
    HostName endpoint.example.com
    ProxyCommand ssh username@proxy.example.com nc %h %p 2> /dev/null

On the proxy host, make sure nc (netcat) is installed.

Then, on workstation, you can ssh endpoint or sftp endpoint and you will be transparently proxied to the machine by way of your proxy host.