September 17th, 2012 by depesz | Tags: , , , , , , | 1 comment »
Did it help? If yes - maybe you can help me?

For those of you that don't know – Tmux is program similar to screen, but written from scratch, with different functionality, approach, and capabilities.

I would like to show one particular case where I found that Tmux does something that screen doesn't.

You might be familiar with ssh-agent. I use it daily. And Tmux apparently supports it in a way that saves me some (or a lot depending on how you look at it) trouble.

First, let's see how screen behaves.

On my local machine, I have ssh-agent configured, with loaded key:

11:29:12 depesz@h3po4 ~
=$ ssh-add -l
4096 51:81:d8:bf:78:95:b6:6b:e3:d8:29:1d:ee:ea:fe:60 /home/depesz/.ssh/id_rsa (RSA)

Now, I log to another machine, with agent-forwarding enabled:

11:29:17 depesz@h3po4 ~
=$ ssh blob
...
 
11:29:40 depesz@blob ~
=$ ssh-add -l
4096 51:81:d8:bf:78:95:b6:6b:e3:d8:29:1d:ee:ea:fe:60 /home/depesz/.ssh/id_rsa (RSA)

SO far everything OK. Now, let's start a screen on blob (the remote machine):

11:30:20 depesz@blob ~
=$ screen
...
11:30:23 depesz@blob ~
=$ ssh-add -l
4096 51:81:d8:bf:78:95:b6:6b:e3:d8:29:1d:ee:ea:fe:60 /home/depesz/.ssh/id_rsa (RSA)

this last command is inside screen.

Now, I will detach screen (ctrl-a d), and logout from blob. Logout is important.

Then, I re log to blob, and attach to previously detached screen:

11:32:06 depesz@h3po4 ~
=$ ssh blob
...
 
11:32:06 depesz@blob ~
=$ screen -r

And within this screen, I do:

11:30:26 depesz@blob ~
=$ ssh-add -l
Could not open a connection to your authentication agent.

This is (more or less) obvious. Connection to agent is in env variable SSH_AUTH_SOCK, and this particular socket does not exist:

11:32:22 depesz@blob ~
=$ echo $SSH_AUTH_SOCK
/tmp/ssh-oEAIMouZcO/agent.3423
 
11:33:18 depesz@blob ~
=$ ls -l $SSH_AUTH_SOCK
ls: cannot access /tmp/ssh-oEAIMouZcO/agent.3423: No such file or directory

Why it doesn't exist? Because logging off from ssh closed connection to agent. This is all find and dandy – I don't expect any terminal program to modify env of running programs (shell in this example).

But what about new program/shell?

I started new session within the screen, and in it:

11:32:54 depesz@blob ~
=$ ssh-add -l
Could not open a connection to your authentication agent.

It also has the wrong, old, not existing path to agent socket.

So, how can Tmux improve over this?

Let's repeat the same exercise, but this time with tmux:

11:36:07 depesz@h3po4 ~
=$ ssh blob
...
 
11:36:10 depesz@blob ~
=$ tmux
 
 
11:36:12 depesz@blob ~
=$ ssh-add -l
4096 51:81:d8:bf:78:95:b6:6b:e3:d8:29:1d:ee:ea:fe:60 /home/depesz/.ssh/id_rsa (RSA)
 
11:36:15 depesz@blob ~
=$ echo $SSH_AUTH_SOCK
/tmp/ssh-1npHFai9H0/agent.3859

After this, I detached from Tmux (ctrl-b d), and logged out of blob. And then I logged back in, and attached back to tmux:

11:37:31 depesz@h3po4 ~
=$ ssh blob
...
 
11:37:30 depesz@blob ~
=$ tmux a

Within the tmux, in pre-existing shell:

11:38:06 depesz@blob ~
=$ ssh-add -l
Could not open a connection to your authentication agent.
 
11:38:10 depesz@blob ~
=$ echo $SSH_AUTH_SOCK
/tmp/ssh-1npHFai9H0/agent.3859

But, when, in the same tmux, I open new window with new shell, I get:

11:38:32 depesz@blob ~
=$ ssh-add -l
4096 51:81:d8:bf:78:95:b6:6b:e3:d8:29:1d:ee:ea:fe:60 /home/depesz/.ssh/id_rsa (RSA)
 
11:38:34 depesz@blob ~
=$ echo $SSH_AUTH_SOCK
/tmp/ssh-IeanToYwkS/agent.4140

Note different, updated, path to agent socket.

As you can see, it just works. And since I tend to have some long-running programs in background, I definitely prefer tmux, so I can always start new window/screen and do productive work with my ssh working correctly.

You can also view the whole example as an ascii screen cast.

  1. One comment

  2. Sep 23, 2012

    Great point and one of my favorite features of tmux as well. Did you know that tmux actually updates the underlying env variables when reconnecting? Try “tmux showenv” once reconnected to see what I mean. With a little “sed” magic, you can inject those updated values into each session. I keep a script in my dotfiles for doing just that. Check it out:

    http://github.com/al-the-x/Bash_Profiles/tree/master/bin/ssh_fix

Leave a comment