Operation Bootstrap

Web Operations, Culture, Security & Startups.

Terminal Multiplexing With Ssh-agent - My Tmux Setup

| Comments

For the impatient – the setup of this is here on GitHub – read on to learn what you are actually downloading.

I spend most of my day, like many of you, in a terminal. I also spend most of my time in those terminals connecting to a variety of different hosts, sometimes for 30 seconds and sometimes for hours/days. I’ve adopted a setup over the years that lets me move my workspace (those terminals) from my desktop, to a laptop, to my phone if needed, and start/stop/continue work from any of them.

If you are familiar with screen or tmux, you know what I’m talking about. Despite knowing about these tools, many folks find them really hard to leverage because – quite frankly – the defaults suck. You can’t tell what host your are logged into – if I have 20 tmux sessions open, which one am I looking at? There is a solution.

This is a setup I must put in place on day 1, before I can start any work for any duration of time. I’ve grown so accustomed to it that I can’t live without it and if you try it, I think you’ll find the same. I’ve converted quite a few co-workers to this approach, but haven’t ever written up the how-to publicly (it exists on a few internal wiki’s).

This has all been cobbled together over time from various Internet sources I can no longer recall – to those of you who unknowingly helped, thank you. If you recognize your work – please let me know so I can update the post with appropriate credit.

First – some reasons to bother with this:

  • You can have multiple sessions to internal machines from a single “management” node even if you have to go through a bastion host to get there with minimal effort.

  • Long-running processes will not get interrupted (barring some local datacenter problem) because you disconnect or shutdown your laptop.

  • You never, ever, have to touch the mouse to move between hosts you are working on.

  • Other people will be jealous and ask you how to do this. I promise.

Here’s a quick view of what people will see and ask “What is that?”:

The only really interesting part is what you see at the bottom. This is a single tmux session – you can have multiple sessions, give them cutesy names, etc. You can also connect multiple clients to the same session and watch what’s going on simultaneously on two systems. The red square is the window I am currently viewing. You notice there are 3 windows, the one titled “bash” is a local window, the one titled “hostb” is an ssh session to a host named… can you guess?… “hostb”. Same for “hostc”. With this setup, as you ssh into new hosts you’ll get a new window with that hostname as the name – neat eh?

Enough already! How do I set it up? Easy!

I have published the files relevant to this here on GitHub for you to grab and install. There’s an automated setup.sh script, and a manual process documented in the README. I would rather maintain the instructions there than here so that anyone watching the source benefits from updates. Plus, I’m lazy, and I only want to update the one source that really matters.

If you find these useful – let me know. If you have tweaks / suggestions / improvements – either shoot me a note or send me a pull request (preferred).

Using the new setup

The biggest thing about my setup – I don’t type ‘ssh’ very much to connect to machines and that’ll probably be the single biggest adjustment for you to use this. The setup includes a script called ‘sc’ which is what I type instead of ssh. Why? Because typing ‘sc somehostname’ will do 3 things for me:

  1. Open a new tmux window named after the host I’m connecting to (to give the effect above)

  2. Setup your ssh agent variables to make sure the new tmux session has access to them

  3. SSH to the host

I think the added functionality is worth twisting your brain to think a little differently.

So, when I want to connect to my management tmux sessions from any system I have (this works from my phone, my laptop, or my mom’s computer with putty):

  1. SSH to the host running tmux

  2. Type ‘Attach’

  3. Rejoyce! My ssh-agent will work and I can ‘sc’ into any host using my public key – no additional setup required.

Happy tmuxing!