Skip to main content
  1. Posts/

How to Automatically Start or Reattach to a tmux Session with SSH

·144 words·1 min· loading · loading ·
Rafael Sadowski
Author
Rafael Sadowski
Shut up and hack

How to Automatically Start or Reattach to a tmux Session with SSH
#

When working on remote servers, maintaining persistent workflows can save a lot of time and effort. By combining SSH with tmux, you can ensure that a session starts automatically upon connection—or reattaches to an existing one. This code snipped demonstrates how to configure your .ssh/config file to achieve this.

# .ssh/config
Host hack
    Hostname hack.sizeofvoid.org
    User KawwptnBlaubaer
    IdentityFile  ~/.ssh/id_ed25519_sk
    RequestTTY yes
    RemoteCommand tmux new -A -s hackon

Key points:

  • RequestTTY: Ensures a pseudo-terminal is allocated. This is required for interactive applications like tmux.

  • RemoteCommand: This is the magic command that handles tmux sessions:

  • tmux new: Starts a new session.

  • -A: Attach to an existing session if one with the same name already exists.

  • -s hackon: Specifies the session name as hackon. You can replace this with a name of your choice.