this ssux

Two of the commands I use the most on a daily basis are tmux and ssh.

If you haven’t heard of tmux it is a terminal multiplexer similar to screen, but with loads of extra functionality. My normal workflow sees me mostly in one or more tmux sessions.

As a sysadmin I rely heavily on ssh. Even with moving a lot of my work to Infrastructure-as-Code with tools like Ansible, there is still many times throughout a given week when I have to ssh onto a system.

A long time I was in the habit of starting a screen when I logged onto a system so that if my connection dropped I didn’t lose what I was doing, as well as being able to open new shells within the same session.

Then I moved to installing tmux on my systems and started a tmux session when I logged on. After a while this became an function in my zsh alias file, which I use in place of ssh

ssux () {
    TERM=screen ssh -t "$@" 'tmux attach || tmux new' || ssh "$@"
}

The function opens an ssh connection to the server specified then attempts to attach an existing tmux session. If this fails it will try to start a new session. If both of those commands fail, if tmux isn’t installed for example, then it falls back to normal ssh

Running it is as simple as replacing ssh

ssux hostname

I have seen a couple of different methods of doing something similar using settings in ~/.ssh/config, but I found they didn’t suit my needs.

I was using this a lot in work and decided to add hostname autocompletion to make my life easier. You can see the completion script in my dotfiles.

Living inside a tmux session then using tmux over ssh meant I had to use two different prefix options. I didn’t like this. I use the backtick (`) as my prefix in tmux, I find it to be really convenient. I wanted to use the same backtick prefix on my remote sessions. This worked if I hit backtick twice before the command I wanted to send to the remote, or “second” session.

With a bit of internet searching I found a discussion on the tmux Github page regarding a toggle option. I also came across bnorick’s tmux.conf with a toggle for nested sessions.

I have implemented the same thing in my tmux.conf

# see: toggle on/off all keybindings · Issue #237 · tmux/tmux - https://github.com/tmux/tmux/issues/237
# Also, change some visual styles when window keys are off
bind-key -T root F12  \
    set-option -g prefix None \;\
    setw -g window-status-current-format "#[bg=colour5,fg=colour0] #I #[bg=colour5,fg=colour0]#W #[bg=colour0,fg=colour5]▓░" \;\
    refresh-client -S

bind-key -T root F11 \
    set-option -g prefix ` \;\
    bind ` send-prefix \;\
    setw -g window-status-current-format "#[bg=colour14,fg=colour0] #I #[bg=colour14,fg=colour0]#W #[bg=colour0,fg=colour14]▓░" \;\
    refresh-client -S

When I hit F12 the prefix in my local session is set to None, then I change the colour of the active window tab in the status bar so I remember it is disabled. I can now use the backtick prefix freely in my remote session. To enable the local session again I hit F11.