How to detach other clients from tmux sessions?

Quick Answer

To detach other clients from a tmux session, use Ctrl+b : followed by detach-client -a to detach all other clients except yours. You can also specify a target client with detach-client -t /dev/pts/X. Use list-clients to see all connected clients before detaching.

detach-client -a

Detailed Explanation

When multiple clients (terminal windows or users) are connected to the same tmux session, you might want to detach some or all of them. This is useful for managing shared sessions, freeing resources, or dealing with "stuck" connections.

Listing Current Clients

Before detaching clients, you may want to see which clients are currently connected:

# List all clients connected to the current session
Ctrl+b :list-clients

# Or from the shell
tmux list-clients -t session_name

This will show output similar to:

/dev/pts/0: session0 [80x24 xterm-256color] (utf8)
/dev/pts/2: session0 [100x30 xterm-256color] (utf8)

Each line represents a client connection with its terminal path, session name, terminal size, and type.

Detaching All Other Clients

To detach all clients except your current one:

# From within tmux
Ctrl+b :detach-client -a

# From the shell (detaches all clients from session_name except the calling client)
tmux detach-client -a -t session_name

The -a flag stands for "all except the current client."

Detaching a Specific Client

To detach a specific client, you need to specify its terminal path:

# From within tmux
Ctrl+b :detach-client -t /dev/pts/2

# From the shell
tmux detach-client -t /dev/pts/2

Replace /dev/pts/2 with the actual terminal path shown in the list-clients output.

Detaching Clients from a Specific Session

If you have multiple sessions and want to detach clients from a specific one:

# From within tmux
Ctrl+b :detach-client -a -s session_name

# From the shell
tmux detach-client -a -s session_name

This detaches all clients connected to the specified session, even if you're not currently attached to it.

Pro Tip

Create a key binding for quickly detaching other clients:

# Add to your ~/.tmux.conf
bind D detach-client -a

Now you can press Ctrl+b D (capital D) to quickly detach all other clients.

Detaching with Switching

You can detach other clients while simultaneously switching to a different session:

# Detach other clients and switch to session_name
Ctrl+b :attach-session -d -t session_name

The -d flag detaches other clients, while the -t specifies the target session to attach to. This is useful for taking control of a session that's being viewed by others.