How to attach to a tmux session?

Quick Answer

To attach to a tmux session, use tmux attach or tmux a. To attach to a specific session, use tmux attach -t session-name.

$ tmux attach -t mysession

Detailed Explanation

The tmux attach command connects you to an existing tmux session. This is one of tmux's core features - the ability to detach from a session and reattach to it later, even from a different computer or terminal.

Basic attach commands:

  • tmux attach or tmux a - Attach to the most recently used session
  • tmux attach -t session-name - Attach to a specific session by name
  • tmux attach -t 0 - Attach to a session by number

Handling multiple sessions:

If you have multiple sessions, first list them to see which one you want to attach to:

$ tmux ls
0: 1 windows (created Fri May 31 12:34:56 2025) [80x24]
dev: 2 windows (created Fri May 31 14:10:34 2025) [80x24]

Then attach to your desired session:

$ tmux attach -t dev

Additional attach options:

  • tmux attach -d - Attach and detach all other clients (useful for screen sharing)
  • tmux attach -r - Attach in read-only mode
  • tmux attach -c /path - Set the working directory for the session

Pro Tip

If the session doesn't exist, you'll get an error. Use tmux new-session -A -s name to create the session if it doesn't exist or attach if it does.

SSH Connection

When connecting to a remote server, you can automatically attach to a tmux session with: ssh user@host -t tmux attach