10 ms·
A visual guide to SSH tunnels
- brunobronosky 6y agohttps://unix.stackexchange.com/a/115906/9745 https://unix.stackexchange.com/a/115906/9745 I’ve always shared this with my team. I think OP’s post is good, but prefer the visuals of this for the `-L` and `-R` features.
- fooblat 6y agoGreat write up! One thing to add is that you can even open tunnels during an interactive session without disconnection. To do this, type the escape command sequence ~C (will not show) and it will drop you to the control prompt. You can then add tunnels. ssh> ssh> -L 8000:localhost:9000 Forwarding port.
- macintux 6y agoI’ve been using ssh for 25+ years, I wrote a (in hindsight, pretty awful) article about it for Linux Journal, and I never knew this.
- nerdponx 6y agoIs ~ a literal ~ character? Or notation for some modifier key? I only knew about ^ to mean "control".
- comradesmith 6y agoLiteral ~. Its input as a sequencenot as a modifier. Iirc the sequence ~h or ~? prints a little help page
- rzzzt 6y agoWarning, if you read comradesmith's comment even more literally and type "~." (tilde and period) in an SSH session, that will close the connection and exit the client. It is useful when a remote application hangs and does not let you exit gracefully: https://apple.stackexchange.com/questions/35524/what-can-i-do-when-my-ssh-session-is-stuck#35543 https://apple.stackexchange.com/questions/35524/what-can-i-d...
- jtvjan 6y agoYes, but you need to press enter before using it. Try typing `⏎~?' in an SSH session.
- xvf22 6y agoWell, there's something I didn't know. Thank you for the time and tab savings!
- fooblat 6y agoSince this tip is popular I'll share another related tip. When using nested ssh session, you can send escape commands to any of them by the number of tildes. For example, if you ssh into Server A and then from Server A to Server B: ~~. # drop session from Server A to Server B ~. # drop original session into Server A
- stjohnswarts 6y agoI sometimes have to hit a <return> before the ~. disconnect works. Not sure why.
- BenjiWiebe 6y agoBecause iirc, ssh only interprets the tilde if it's immediately preceded by a newline.
- IgorBog61650384 6y agoReally nice and clear. One addition: the final example uses ProxyCommand, I find ProxyJump much more useful: you can specify multiple hops clearly and even specify different private keys for each hop.
- hmsimha 6y agoProxyJump indeed seems more useful, but has less support (it's a "newer" SSH feature). Do you know if it needs to be supported by the openssh libs of intermediate hosts, or just the initiating client?
- lxgr 6y agoAs far as I remember, it‘s implemented on the client only. (It can be replicated with ProxyCommand and a few invocations of cat/nc on the proxy host, if I remember correctly.)
- shawnz 6y agoProxyCommand is useful for proxying with a non-SSH protocol. For example when combined with proxytunnel or similar you can run your SSH connections over an HTTPS proxy, which appears as regular web traffic to the network operator.
- An0mammall 6y agoReally nice guide! This stuff really helps getting into the topic!
- framecowbird 6y agoIs it just me or is this site a bit of a disaster on mobile?
- 656565656565 6y agoYes gave up it’s too awkward to read.
- mfgs 6y agoYep, but it works well for me in landscape view.
- catoc 6y agoCould read just fine on iPhone 12p after zooming out; even in portrait
- dexterdog 6y agoOn desktop firefox all of the code snippets are black-on-black so I have to select the code to read it.
- franga2000 6y agoAbsolutely brilliant! I've had to explain local/reverse/dynamic port forwarding to people enough times that I've written a text file I can just send them, but I can finally replace that with a link to this page.
- stdclass 6y agoNicely written! I once also wrote a guide to access remote IoT devices using SSH tunnels: https://phillip.dornauer.cc/unix/iot/2019/04/12/set-up-ssh-tunnel-server.html https://phillip.dornauer.cc/unix/iot/2019/04/12/set-up-ssh-t...
- dloss 6y agoDrawing some diagrams helped me understand remote port forwarding and OpenSSH's port:host:port syntax: http://dirk-loss.de/ssh-port-forwarding.htm http://dirk-loss.de/ssh-port-forwarding.htm
- tatref 6y agoI think it's way clearer!
- giuseppeciuni 6y agoThanks! I pinned your page on my evernote.. I always get confuse everytime I need to make a ssh port forward. After a while I get it but I spend time (and some bad words :P)
- ohazi 6y agoSSH tunnels are my favorite tool for NAT-busting. I always have to look up the cryptic syntax, but with one strategically placed Raspberry Pi, you can basically get from anywhere to anywhere. And it works just as well on a locked down corporate network as it does on a home network. Why people put up with garbage VPN software is beyond me.
- lxgr 6y agoSSH isn’t a replacement for VPNs. I’d argue that they complement each other. For example, SSH port forwarding is TCP only, so if you need UDP, you‘ll need a VPN.
- xioxox 6y ago[note I am wrong here - it doesn't support UDP - see below] The ssh SOCKS proxy supports UDP. I use Firefox with a SOCKS proxy via ssh (e.g. ssh -D 2222 hostname) to get around geoblocking and accessing papers via an institutional account. The tsocks program can also let you use a SOCKS proxy for an arbitrary command in linux. I find it much more convenient than a VPN, as it can be easily applied to single programs.
- cryo 6y agoAre you about this? Last time I've checked SSH SOCKS5 only supports TCP traffic, UDP isn't supported and needs extra steps through tools like socat to wrap UDP in TCP. http://lists.mindrot.org/pipermail/openssh-unix-dev/2017-January/035662.html http://lists.mindrot.org/pipermail/openssh-unix-dev/2017-Jan...
- xioxox 6y agoIt looks like I was wrong about UDP being supported by ssh. Sorry about misleading everyone. I've verified with nc that it doesn't actually forward UDP. I'm amazed that streaming media over this seems to work fine on most sites, though.
- kburman 6y agoThese are simply amazing. There's a stack overflow answer similar to this which I found very intuitive to understand https://unix.stackexchange.com/a/115906/254044 https://unix.stackexchange.com/a/115906/254044
- sgao08 6y agoThanks for this amazing guide!
- unilynx 6y agoThere’s also -w which sets up a point to point tunnel which you can route over - even more powerful than the SOCKS server
- amelius 6y agoWhy not visualize them as actual tunnels?
- manuel_w 6y agoThat comes in very handy. Bookmarked. I also found the following very nice: (Same info but presented in a different way.) https://unix.stackexchange.com/questions/115897/whats-ssh-port-forwarding-and-whats-the-difference-between-ssh-local-and-remot/115906#115906 https://unix.stackexchange.com/questions/115897/whats-ssh-po...
- magicconch 6y agoFor those interested in the topic of tunneling, I highly recommend The Cyber Plumber’s Handbook [1] to gain an even deeper understanding and examples beyond SSH. [1] https://cph.opsdisk.com/ https://cph.opsdisk.com/
- opsdisk 6y agoAppreciate the shout out magicconch! Just added a HN family discount to pick up the book, lab guide, and lab access for $10. https://gum.co/cph_book_and_lab/hackernews20210207 https://gum.co/cph_book_and_lab/hackernews20210207
- tailspin2019 6y agoThanks for the HN discount! Purchased.
- 64738 6y agoVery cool, I was unaware of this product. Thanks for the deal! :D
- pixelface 6y agovery cool, just grabbed a copy.
- opsdisk 6y agoAppreciate the support pixelface...hope it helps!
- pvitz 6y agoJust tried to grab a copy but gumroad returns a 404 after trying to pay with Paypal. Do you sell it through another shop, too? Edit: Never mind, it seems it was a problem with Firefox. Chromium worked.
- opsdisk 6y agoLet me know if it's still giving you trouble...contact info is in my profile.
- walrus01 6y agoOne common use is to secure vnc, which has no crypto built into it. On the remote host, have the vnc server listen only on localhost:5901 This assumes everyone who can ssh to the server should be allowed access. On your workstation, form an ssh tunnel to that remote host and its port, and link it your own localhost:5901 Open your vnc client GUI and set it to open 127.0.0.1:5901, voila, vnc session. More fun things: say for example you have a big remote xen dom0 with a lot of unique qemu HVM VMs running on it. Configure each of their .cfg files for xen to spawn a vnc server on localhost only and a unique port number per domU (5902,5903,5904,etc). Then use the same method to connect.
- chrisandchris 6y agoI am always amazed at what is possible with the linux command line, and it‘s something one can really fall in love with.
- labawi 6y agoNote that it still exposes the connection to all local processes, on both hosts (except network-restricted services or with non-typical host-local firewall rules), though it should be fine, if you're using semi-reasonable passwords. SSH supports unix domain socket forwarding and even cross unix/ip forwarding with a syntax like: ssh -L /path/to/sock:/path/to/sock ssh -L /path/to/sock:localhost:5901 ssh -L 5091:/path/to/sock Then you can have user-authenticated host and transit secure password-less VNC connections. Unfortunately I haven't been able to convince tigervnc viewer to connect to a unix domain socket, so I don't have a fully scripted connection, but remmina and tigervnc server work fine. Also, forwardings / connections fail if the socket already exists when the listening side starts up (often an unclosed leftover), so I rm them before starting the ssh connection / vnc server and it works reliably.
- philsnow 6y agoWow that's amazing, I've been cobbling together socat processes because I didn't know this was possible.
- m463 6y ago
- johnchristopher 6y agoGreat, I can refer people (and myself) to this page ! A lot of tunneling tutorial, medium articles and blog post mixes local and remote port forwarding and use the words interchangeably which cause a lot of confusion.
- MaxBarraclough 6y agoGreat resource. Most tutorials on SSH tunnels are strangely incomplete. This one seems to cover all the important points, and does so in an approachable way.
- labawi 6y agoWhy don't people use unix domain sockets for local connections? They are pretty much the same thing as local IP connections, except access rights apply as on files, so you're not exposing connections to pretty much all processes. Domain sockets can be forwarded via ssh with the same -L and -R arguments, including cross unix/ip forwardings.
- chaosfox 6y agolast time I tried ssh does not delete the socket file when the connection closes and complains the file already exists on a new connection.
- aidenn0 6y agoIt's a relatively recent feature in openssh (in the past decade for upstream; not sure when it first landed in an Ubuntu LTS, which for practical purposes is the earliest one might start using it regularly)
- atomicson 6y agoIn a computer networks (the Internet), if I can see you, you can see me.
- efreak 6y agoYou might want to be a bit more specific than computer networks. Monitor/ promiscuous mode is a thing, and so is dropping packets. It's not much use outside of wifi anymore, but there's still a few dumb hubs around.
- metahost 6y agoThis[1] is probably the best, easiest and most (visually) effective way to learn about SSH tunnels. [1] : https://unix.stackexchange.com/a/118650/289353 https://unix.stackexchange.com/a/118650/289353
- beermonster 6y agoNice reference. Reaching target hosts multiple jump-hosts away, and going the wrong way through multiple firewalls segregating those lans, is another use-case of tunnels I've found handy. i.e. your_remote_client_host->internet->fw1->lan1->fw2->lan2->fw3->target_host Target_host can be reached by doing tunnel-in-tunnel-in-tunnel-in-tunnel. Each tunnel gets you past one firewall. The final tunnel you can just ssh to fw3 via a local tunnel and a local port on your_remote_client now takes you straight to target_host.
- px43 6y agoThere's a really fancy new -R feature that I love (added in 2017 I think). ssh <target> -R <just a port number> This opens up a localhost port on the target that acts as a socks server which tunnels all your traffic through the source machine. This is great for machines that you can SSH into, but are otherwise completely isolated from the network, or are monitored heavily. You can jump on, and pull down everything you need from the existing SSH connection, rather than using the machine to make requests out to the internet directly. This is also good if the source machine is a web server or something on a secured network which can SSH out, but not much else, and the destination is your command and control server on the internet. Then it opens up a socks port on the C&C machine that gives full access to the internal network, impersonating the source machine. Every other -R is a point to point TCP connection, but setting up a SOCKS proxy with -R is magic. More analogous to a reverse -D than a reverse -L. Super useful.
- LukeShu 6y ago> (added in 2017 I think) Indeed: committed in September 2017, released in v7.6 in October 2017.
- nafizh 6y agoIn tmux, when opening multiple sessions to the same remote server, how do you avoid typing the password repeatedly?
- milofeynman 6y agoAdd your ssh pub key (id_rsa.pub) to authorized_ssh keys on the remote servers will automatically authenticate you. https://www.ssh.com/ssh/copy-id https://www.ssh.com/ssh/copy-id
- kps 6y agoLook up ‘shh multiplexing’. This makes subsequent sessions share the same connection. I don't know a particularly good tutorial; maybe see https://en.wikibooks.org/wiki/OpenSSH/Cookbook/Multiplexing https://en.wikibooks.org/wiki/OpenSSH/Cookbook/Multiplexing
- mugwort13 6y ago-Title claims "visual guide"- Me: *Opens it up, sees the entire thing is text* Ҩ.¬_¬.Ҩ
- zxcvbn4038 6y agoEvery once in a while I wonder how the SOCKS5 stuff works at the protocol level, I would like to be able to SSH out without running a proxy process locally, just talk to the ssh deamon on the relay server directly. The OpenSSH code is a bit dense but it looks like it creates dynamic tunnels that are torn down when the connection closes. Anyone confirm that is what happens behind the scenes?
- AtlasBarfed 6y agoI was hoping for a series of tubes.