Using tmux for persistent shell sessions#

Tmux allows you to keep a shell session active when closing your ssh connection. So, when you close your laptop to change a room, you don’t lose the current state of your command line tools. It has many more useful features. However, you need to remember the login node you used to start tmux, and X11 applications (e.g. ncview) will not survive the disconnect. emacs will survive in command line mode (module load emacs ; emacs -nw).

The following examples will assume that you have prescribed your user name and allowed (trusted) X11 forwarding for dkrz.de machines in your ~/.ssh/config

host *.dkrz.de
   user YOUR_USER_ID
   ForwardX11 yes
   ForwardX11Trusted  yes

Getting started#

To use it, start your first session as:

ssh levante2.dkrz.de # chose your preferred node here.
tmux

When you lost your ssh connection, you can reconnect by

ssh levante2.dkrz.de
tmux attach -t 0

To get a list of active tmux sessions (on your login node of choice):

tmux list-sessions

Ask your favorite search engine for tmux tutorial or tmux cheat sheet to get started.

Customizing tmux#

You probably want to rebind the control key for tmux. By default tmux will eat ctrl-b (c-b in the following) and use it for commands to tmux. Not super-handy if you are used to using it for moving the cursor backward by one character… Add

unbind C-b
set-option -g prefix C-j
bind-key C-j send-prefix

to your ~/.tmux.conf on levante to move this to c-j. Then c-j c-j will send a c-j to your shell/emacs/whatever.

Now you can use c-j d to detach from your tmux session. It will live on, but you are back in the normal shell.

Also have a look at tmux customization in your favorite search engine.

Using tmux and X11 forwarding#

As stated above, we’ll assume that you have activated X11 forwarding for your connections to levante.

Now the only issue is that the shell in the tmux will forever remember the display number you had, when you started tmux, but your ssh connections will get different display numbers on each connect.

ssh levante2.dkrz.de
echo $DISPLAY
#  localhost:38.0
tmux
echo $DISPLAY
# localhost:38.0

#  [ on a new terminal / ... ]
ssh levante2.dkrz.de
echo $DISPLAY
# localhost:46.0
tmux attach -t 0
export $DISPLAY
# localhost:38.0
# you cannot start x11 apps now.
export DISPLAY=localhost:46
# this should fix things. Take the number you got from the echo after logging in.