18 ms·
SSH tunnelling for fun and profit: AutoSSH
- jacquesm 11y agoNice tool but not quite as reliable as I'd like it to be, especially on the reconnection-in-the-face-of-unreliable-transport front. I tried using it for my mailtunnel but eventually switched to a VPN over UDP.
- vidoc 11y agoyou might take a look at zerotier.com. They provide tools/services to ease tun/tap much like docker eases lxc/containers.
- jacquesm 11y agoFor now openvpn seems to fill my needs but thank you for the reference, if I can't get this to work reliably over the longer term I will look at zerotier.
- emmelaich 11y agoIf you're running systemd you can use this: [Unit] Description=SSH remote forwarding to here After=network-online.target Requires=network-online.target [Service] User=autossh ExecStart=/bin/ssh -N -o PasswordAuthentication=no -o "ServerAliveInterval 60" -o "ServerAliveCountMax 3" -R 2222:localhost:22 -o TCPKeepAlive=yes -p 9196 -i /home/autossh/.ssh/autossh autossh@example.com Restart=on-failure RestartSec=1200 [Install] WantedBy=multi-user.target
- wscott 11y agoI use sshuttle (https://github.com/apenwarr/sshuttle https://github.com/apenwarr/sshuttle). It is super flexible and works as a poor man's VPN. It also has much better performance when used over a flaky connection because it doesn't do TCP-over-TCP. It doesn't really create a persistent tunnel like this thingy, but it seems worth mentioning to people who might find autossh interesting.
- njs12345 11y agoSshuttle is awesome. Comparing the ease of use to openvpn, it's night and day..
- pbhjpbhj 11y agoIndeed I was looking to create a VPN and found it complicated - saw sshuttle and found it awesomely easy (blog post about it http://alicious.com/cheap-vpn-using-sshuttle/ http://alicious.com/cheap-vpn-using-sshuttle/ [has aff links in post]). My commandline was: sshuttle –dns -rv root@128.199.xxx.xxx 0/0 –exclude 192.168.0.0/9 # start sshuttle -dns routes dns requests over the link -rv remote server details (v for verbose output) -exclude to not send traffic from local addresses over the link (not sure if this is required)
- deanclatworthy 11y agoI've found performance (bandwidth) on sshuttle to be poorer than OpenVPN.
- LawnGnome 11y agoIt's definitely below what you get with a true VPN, but it's a great option when combined with an SSH server listening on port 443 to get any kind of "VPN" on a locked down network (like most airport WiFi, for example).
- scandox 11y agoI wrote a guide on using Softener VPN which I found much easier to use than other VPN software: http://www.selectedintelligence.com/post/128701492804/softether http://www.selectedintelligence.com/post/128701492804/softet... Our prototype used autossh actually but later we needed something a bit easier to manage. Disclaimer: I am not a networking or security expert.
- sneak 11y agoAlternately, "ssh -D 1080 user@host" will give you a SOCKS proxy on 127.0.0.1:1080 that forwards all connections out of the remote host. No download required.
- daurnimator 11y agoThis tool makes me afraid. Anyone that can successfully use AutoSSH is not monitoring their private key usage. You should always need to approve each time your SSH key is used; whether via typing in your password, swiping your smartcard, or pressing the button on your yubikey.
- epimenov 11y agoI have a key generated for AutoSSH only. Also you can limit to which hosts/ports it can connect with: no-agent-forwarding,no-X11-forwarding,command="read a; exit",permitopen="host:port" ssh-ed25519 AAAA Pretty nifty.
- sleepychu 11y agoOn the server side, right?
- creshal 11y agoYes, in the authorized_keys
- tomkinstinch 11y agoI've gone so far as to create a separate autossh user with separate key, with no shell access (by setting the user's shell to /bin/false). It still permits tunneling (call ssh with -N), but does not allow shell sessions. Perfect for when you want to access remote systems via a remote tunnel but do not want to give shell access on the relaying machine to the tunnel origin. It trusts that the machine originating the tunnel will not initiate a reflection attack (by making a local forward to the remote port itself on the originating machine and causing some log messages to appear until all file descriptors are in use)[1], but that's not so unreasonable. 1. http://www.semicomplete.com/articles/ssh-security/ http://www.semicomplete.com/articles/ssh-security/
- epimenov 11y agoSame here actually, the user has /bin/false as shell.
- kardos 11y agoI understand the fun part but somehow missed the profit aspect
- Piskvorrr 11y agoCan't remember how many times a persistent autossh reverse tunnel to a jump host has saved me from several hours of a field trip. A few man-months, I'd guess - that's quite enough profit for me ;)
- tomkinstinch 11y agoA challenge I've had with Autossh (and OpenSSH for that matter) is in getting the listen port during remote (reverse) tunnels when it is dynamically assigned via '-R 0:localhost:$SOMEPORT' where $SOMEPORT is on the machine originating the reverse tunnel, and 0 is a dynamically assigned open port on the relay/remote machine. It doesn't look like there is a good way[1] to get the listen port from the originating side (remotehost:$DYNPORT) without inspecting ssh stdout (complicated when using Autossh via systemd). Any ideas? On the remote side it is easy to find the listen port provided you have sudo access, but seemingly impossible without [2]. On the local side I probably just need to pipe stdout to a regex to find the right string and then redirect the captured port number to a file or named pipe, but it seems like it ought to be easier to get dynamic port assigned on the remote side from the local end of the tunnel. 1. http://www.nico.schottelius.org/blog/openssh-6.2-add-callback-functionality-using-dynamic-remote-port-forwarding/ http://www.nico.schottelius.org/blog/openssh-6.2-add-callbac... 2. https://gist.github.com/tomkinsc/c2932097796155f4fd94 https://gist.github.com/tomkinsc/c2932097796155f4fd94
- anfogoat 11y agoAssuming autossh is going just rerun whatever binary ssh points to, then you likely wont need a separate binary for this. Just run while true; do ...; done and be done with it. The author does say that ... Some scripts use a while loop, others encourage you to run a remote command (such as tail) to make sure you don’t run into timeout and various others. But actually, you don’t want to re-invent the wheel and stick to bullet-proof already existing solutions. but the only case where that makes sense, and where a simple while loop will fail you, is if the ssh command hangs. Only then will this start making sense.
- Piskvorrr 11y ago1. SSH hanging is not altogether uncommon, esp. on repeated reconnect over flaky connections and/or crappy-but-stateful captive portals, experienced both; also, not every SSH server is OpenSSH, esp. various embedded dropbears have been imbued with nasty device-specific quirks. 2. a simple loop might fail you unless you've remembered to use -oBatchMode=true; also it would work iff every SSH connection is terminated cleanly; also also about 20 other minor warts. Very quickly, the "simple loop" starts being decidedly unsimple, and you'll find yourself reinventing a square wheel. Been there, done that. Then I found that someone else has already been through the same issues and made a comprehensive tool for it: autossh.
- x0 11y agoI'd put a `sleep 5s` or so in there as well, in case your wifi is off or something, and it's looping "no route to host" over and over.
- wiz21c 11y agoI used autossh for 2 years, several times a week, and it is absolutely robust (like never failed, ever) when it comes to restarting a broken ssh connection
- yanokwa 11y agoFor a persistent connection to a server, I like mosh (https://mosh.mit.edu https://mosh.mit.edu). It works really well for roaming across networks and for intermittent or laggy connectivity.
- Carrok 11y agoYea this looks like a terribly convoluted solution for something mosh has solved quite elegantly.
- Piskvorrr 11y ago1. Mosh is about a decade younger than this ("why would anyone write this, when there would be a better solution in ten years?!" - Mosh was a response to the shortcomings of available programs, namely the combination of autossh+screen), 2. Mosh solves a slightly different problem (persistent terminal emulation across variable and unstable connection X persistent SSH connection - one has intelligent local echo, the other has tunnels)
- brightball 11y agoThis is pretty slick.
- Aissen 11y agoNice tool, but doesn't play well with ssh_config ControlPersist, which is essential to my use cases :-(
- everythingcli 11y agoyou could still append this option: `autossh -oControlPersist=yes|no"`
- Aissen 11y agoAnd then losing the advantage of ControlPersist; the ideal would be to have an always-on ControlMaster, but that would also be restarted once ServerAliveInterval*ServerAliveCountMax expires.
- NelsonMinar 11y agoOne thing the article glossed over is the SSH key. I set this up for remote access to a computer behind a NAT router that does not allow any incoming connections at all. For that to work robustly in case of a reboot, the private ssh key used by that computer must have no passphrase. That always makes me nervous, I ended up setting up the public computer side of this with a special user account that was as locked down as I could make it. All the ssh tunnel will do is forward port 2200 on the public machine to my computer's ssh port. Works great!
- Piskvorrr 11y agoOr you could use a SSH agent (Pageant, ssh-agent): you enter your key passphrase once when starting the agent, and it handles the key auth on your behalf.
- e40 11y agoWhy is this getting downvoted? It's clearly the right thing to do.
- NelsonMinar 11y agoHow do I enter the passphrase when the computer behind the firewall reboots?
- Piskvorrr 11y agoAh. Now that's a problem I have no solution for - only the same workaround as you describe (you have taken one more step than I did by having a dedicated host for the SSH connection, instead of just an account - with today's virtualization, this is easier than ever :)).
- peterwwillis 11y agoThis seems like pretty simple code. Could we not roll this into the official ssh client as a patch, but instead of port forwarding, use the existing keep-alive code and ssh signaling channel to check the connection and restart the session? Not only would this be cleaner (no need for wrappers), but we could use it on servers where port forwarding is disabled.
- matt_wulfeck 11y agoIn the example of the author creating a reverse tunnel for mysql, I can't decide if I love or hate these types of tunnels. On the one hand it's nice to allow SSH to do all of the encryption heavy lifting, and to punch a single hole through the firewall. It's also nice that somebody sniffing around from the outside doesn't really have any idea what services are running on the box. On the other handle it comes off as much too hacky for something I'd trust my production services with. It also involves two levels of authentication (PKI + whatever is built-in to the system).