Managing Long-Running Ssh Sessions With Tmux

Keeping SSH Sessions Alive with Tmux

Tmux is a terminal multiplexer that enables multiple terminal sessions to be accessed and controlled from a single screen. By multiplexing the terminal, it is possible to detach and reattach terminal sessions while keeping the programs running in the background. This makes tmux ideal for managing long-running SSH connections.

SSH connections will time out after a certain period of inactivity. For example, an SSH session may disconnect after 10 minutes without any interaction. This causes disruptions when connected programs close or stop functioning properly after an SSH connection closes. Tmux avoids this issue by keeping SSH sessions alive in the background even after detaching from the terminal.

Why Keep SSH Sessions Alive?

There are several key reasons for keeping SSH sessions alive for long periods such as:

  • Avoid having to constantly reconnect SSH which can be slow and cumbersome
  • Prevent loss of work by not having to reopen terminal programs
  • Persist long-running processes like downloads/uploads over patchy connections
  • Manage sessions from multiple locations by reattaching as needed

By multiplexing SSH connections, tmux enables securely accessing a remote server continuously regardless of client connectivity issues. Keeping sessions alive with tmux avoids connectivity gaps and having to rebuild terminal environments.

Starting New Tmux Sessions

Launching a new tmux session is straightforward. Simply type tmux in a terminal window and this will start a session with a single window open:


$ tmux
  

From inside tmux it is possible to open additional windows, split windows horizontally/vertically, and switch between windows using keybindings or tmux commands. Some common commands include:

  • ctrl+b c – Create new window
  • ctrl+b & – Split window vertically
  • ctrl+b % – Split window horizontally
  • ctrl+b [0-9] – Switch to window number

Tmux has a powerful command mode triggerd with ctrl+b : allowing more advanced configuration changes to panes and windows.

Detaching and Reattaching Tmux Sessions

A key capability provided by tmux is the ability to detach from sessions while keeping all programs and processes running. This allows long-running tasks to continue without being disrupted if connectivity to the client is lost.

Detaching from a tmux session is done with the ctrl+b d keybinding. The tmux window will disappear but all child processes keep running in the background. Tmux prints out which session has been detached and the ID.

Later when ready to access the session again simply run tmux attach and the previous session will open with all windows intact. The session ID printed when detaching can be used to attach specific sessions.

 
$ tmux attach -t [sessionID]

Being able to persist running processes by detaching and reattaching tmux sessions enables maintaining long-lived SSH connections.

Configuring Tmux for Long-Running Sessions

Out of the box tmux works well for persisting sessions, but additional configurations can extend session durability further for long-term usage.

One area to tweak is SSH client timeout durations before it considers a session closed. Edit the ServerAliveInterval setting in SSH config:


# /etc/ssh/ssh_config or ~/.ssh/config

ServerAliveInterval 120
ServerAliveCountMax 30 

This keeps the SSH connection alive up to 60 minutes without client-to-server communication.

For tmux itself, updating session timeout and respawn settings also helps. Add this to ~/.tmux.conf:


# Longer terminal inactivity timeout
set -g terminal-overrides 'xterm*:smcup@:rmcup@'  

# Respawn tmux session after dying 
set-option -g respawn-window-command "tmux new-session"

With longer timeouts and session respawning, detached tmux sessions will persist for days without user interaction before hitting timeouts.

Managing Multiple Tmux Sessions

It is common to have multiple tmux sessions running on a system with different programs operating inside each session. Tmux supports naming sessions for easier identification and management.

To assign a name, use the ctrl+b $ keybinding to prompt for entering a name. Alternatively just provide a name when starting tmux:


$ tmux new -s [name]

With named sessions they can be directly attached using the -t flag:

 
$ tmux attach -t [name]

Tmux will also list all running named sessions using the tmux ls command, allowing attaching to exactly the right session.

Tmux Session Mobility

A prime benefit of using tmux for long-running SSH sessions is the ability to resume sessions from different client locations. For example, a session could be started from a desktop, detached, and later reattached from a laptop to continue work.

To enable attaching sessions across locations there are a couple requirements:

  • Consistent session naming and ~/.tmux.conf settings
  • Equivalent SSH access between locations

With matched configurations session data and programs will pickup seamlessly when attaching to a detached session from a new client system, providing excellent mobility.

Conclusion

Maintaining resilient long-term SSH connections is made straightforward with tmux. By multiplexing the terminal and detaching/reattaching sessions programs keep running persistently in the background without disruptions to connectivity.

Configuring longer timeout durations, automatic respawning of sessions after failures, naming sessions, and moving sessions between client locations provides robust tools for managing SSH connections.

Tmux transforms how servers can be accessed securely via SSH by enabling sessions to withstand network failures, client changes, and long periods without user interaction before dropping. The terminal multiplexer strongly complements SSH usage for remote server management and access.

Leave a Reply

Your email address will not be published. Required fields are marked *