Skip to content

Claude Persistent Sessions with tmux

This guide explains how to run Claude Code in persistent tmux sessions on the Hetzner server, allowing you to maintain long-running Claude sessions that survive disconnections and can be accessed from anywhere.

tmux (terminal multiplexer) is a tool that allows you to:

  • Create persistent terminal sessions that continue running even after you disconnect
  • Switch between multiple sessions easily
  • Detach from a session and reattach later
  • Keep processes running in the background

Running Claude Code in tmux provides several advantages:

  • Persistence: Your Claude session continues running even if your SSH connection drops
  • Accessibility: Reconnect to your Claude session from any device
  • Multi-tasking: Run Claude in one session while working in another terminal
  • Reliability: Never lose your conversation history or context due to network issues
  • Convenience: Quick commands to attach/detach without interrupting Claude’s work

Your .bashrc has been configured with convenient aliases for tmux management:

CommandDescriptionExample
tclaudeQuickly attach to the Claude sessiontclaude
tlsList all tmux sessionstls
ta <name>Attach to a specific sessionta work
tn <name>Create a new named sessiontn myproject
tk <name>Kill a specific sessiontk old-session
tworkCreate a separate work session (won’t auto-attach to Claude)twork
freetmStart bash shell without auto-tmuxfreetm

Step 1: Create Your Persistent Claude Session

Section titled “Step 1: Create Your Persistent Claude Session”
Terminal window
# SSH into the Hetzner server
ssh root@your-server.com
# Create a new tmux session named "claude"
tmux new-session -s claude
# Start Claude Code
claude

You now have Claude running in a persistent session!

To leave Claude running in the background while you do other work:

Press: Ctrl+b, then press d

This detaches you from the tmux session. Claude continues running in the background.

To return to your Claude session from anywhere:

Terminal window
tclaude

That’s it! You’re back in your Claude session exactly where you left off.

Scenario 1: Working with Claude While Monitoring Logs

Section titled “Scenario 1: Working with Claude While Monitoring Logs”
Terminal window
# Terminal 1: Attach to Claude
tclaude
# Detach with Ctrl+b, d
# Terminal 2: Create a work session for logs
twork
tail -f /var/log/nginx/access.log
# Switch back to Claude anytime with tclaude
Terminal window
# Create session for project A
tn project-a
claude
# Detach: Ctrl+b, d
# Create session for project B
tn project-b
claude
# List all sessions
tls
# Switch between projects
ta project-a
ta project-b

Scenario 3: Recovering After Network Disconnection

Section titled “Scenario 3: Recovering After Network Disconnection”
Terminal window
# If your SSH connection drops, simply:
ssh root@your-server.com
tclaude
# You're back in your Claude session!
Keyboard ShortcutAction
Ctrl+b, dDetach from current session
Ctrl+b, $Rename current session
Ctrl+b, sList all sessions (interactive)

Window Management (Multiple Windows in One Session)

Section titled “Window Management (Multiple Windows in One Session)”
Keyboard ShortcutAction
Ctrl+b, cCreate new window
Ctrl+b, nNext window
Ctrl+b, pPrevious window
Ctrl+b, 0-9Switch to window number
Ctrl+b, ,Rename current window
Ctrl+b, &Kill current window
Keyboard ShortcutAction
Ctrl+b, %Split pane vertically
Ctrl+b, "Split pane horizontally
Ctrl+b, Arrow KeysNavigate between panes
Ctrl+b, xKill current pane
Terminal window
# Good
tn claude-thrivesend
tn claude-mgslg
tn monitoring
# Avoid
tn session1
tn temp
Terminal window
# List all sessions
tls
# Kill sessions you no longer need
tk old-session-name

3. Use Multiple Windows Instead of Multiple Sessions

Section titled “3. Use Multiple Windows Instead of Multiple Sessions”

For related work, use windows within the same session:

Terminal window
# Attach to claude session
tclaude
# Create new window: Ctrl+b, c
# Now you have Claude in window 0, and can work in window 1

If you created session “0” and want to use it for Claude:

Terminal window
# Attach to the session
ta 0
# Rename it: Ctrl+b, $
# Type: claude
# Press Enter
# Now you can use tclaude to attach to it

Issue: “no sessions” when running tclaude

Section titled “Issue: “no sessions” when running tclaude”

Solution: You haven’t created a “claude” session yet.

Terminal window
# Create the claude session
tmux new-session -s claude
claude

Solution: The session may have been killed or renamed.

Terminal window
# List all sessions to find it
tls
# If you see a session with a different name, attach to it
ta <session-name>
# Rename it to "claude": Ctrl+b, $

Issue: Accidentally killed the Claude session

Section titled “Issue: Accidentally killed the Claude session”

Solution: Just create a new one!

Terminal window
tn claude
claude

Your conversation history with Claude is stored in Claude’s cloud, so you won’t lose your context.

Solution: Make sure you’re pressing the key combination correctly:

  1. Press and hold Ctrl
  2. Press b (while holding Ctrl)
  3. Release both keys
  4. Press d

Multiple users can attach to the same tmux session simultaneously:

Terminal window
# User 1 creates session
tmux new-session -s shared-claude
claude
# User 2 (on another SSH connection) can attach to the same session
tmux attach-session -t shared-claude
# Both users see the same Claude session in real-time!

Auto-start Claude on Server Boot (Optional)

Section titled “Auto-start Claude on Server Boot (Optional)”

To automatically start Claude in tmux when the server boots:

Terminal window
# Add to crontab
crontab -e
# Add this line:
@reboot tmux new-session -d -s claude 'cd ~ && claude'

Create a ~/.tmux.conf file to customize tmux behavior:

Terminal window
# Example: Change prefix from Ctrl+b to Ctrl+a
set-option -g prefix C-a
unbind-key C-b
bind-key C-a send-prefix
# Enable mouse support
set -g mouse on
# Start window numbering at 1
set -g base-index 1
# Increase scrollback buffer
set-option -g history-limit 10000

After creating the config, reload it:

Terminal window
tmux source-file ~/.tmux.conf

Each tmux session runs with the permissions of the user who created it. On a shared server, ensure you trust all users who can access your sessions.

For security, consider configuring automatic session timeout:

Terminal window
# Add to ~/.tmux.conf
set -g lock-after-time 1800 # Lock after 30 minutes of inactivity

Be cautious about leaving Claude sessions with sensitive information visible. Consider locking your tmux session when stepping away:

Terminal window
# Lock current session: Ctrl+b, Ctrl+x
# Or from command line:
tmux lock-session

With tmux and the configured aliases, managing persistent Claude sessions is simple:

  1. Create: tn claudeclaude
  2. Detach: Ctrl+b, d
  3. Reattach: tclaude
  4. List: tls

Never lose your Claude session again! 🚀


Last updated: {{ git_revision_date_localized }}