tmux "sessions should be nested with care, unset $TMUX to force"
tmux refuses to attach or start a session from a shell already inside tmux. Use switch-client or prefix then s to change session, or nest on purpose.
tmux prints this when you run tmux, tmux attach or tmux new-session -A in a shell that is already running inside tmux:
sessions should be nested with care, unset $TMUX to force
Nothing changes: the command exits and you stay where you were. tmux stops because a second tmux client inside one of its own panes would draw tmux inside tmux, and both would react to the same prefix key. Usually you did not want a nested session at all; you wanted to move to another one.
How tmux knows
tmux sets the TMUX variable in every pane it starts. Before it attaches a client, it checks two things: TMUX is set, and your terminal is one of the panes of the tmux server you are talking to. Both must be true for the message to appear. So:
- A tmux on another server socket (
tmux -L other) is not refused. That is real nesting, without a warning. - Over SSH, the remote shell does not get your local
TMUX, because ssh sends no environment variables unlessSendEnvnames them. Running tmux on the remote host from inside a local tmux works without this message, and the prefix question below applies.
Switch sessions instead
From a shell inside tmux, use switch-client. It moves the client you are using to another session; no second client starts:
tmux switch-client -t work
Other ways to move:
Ctrl+Bthens: pick a session from a list (choose-tree).Ctrl+Bthen(or): previous or next session.Ctrl+BthenL: the last session.Ctrl+Bthen:andattach -t work: at tmux's own command prompt,attach-sessionswitches the current client. That is what the manual means by "if used from inside". From the shell it gives the nested message.
To create a session and go to it, create it detached first:
tmux new-session -d -s work
tmux switch-client -t work
Or leave tmux with Ctrl+B then d (detach) and run the attach command from the plain shell.
Scripts and login files that start tmux
A line in .bashrc or .profile that runs tmux runs again in every new pane, and each time it prints this message. Test TMUX first:
if [ -z "$TMUX" ]; then
tmux new-session -A -s main
fi
A script that should work both inside and outside tmux can choose the command:
if [ -n "$TMUX" ]; then
tmux switch-client -t work
else
tmux attach-session -t work
fi
For the full login setup over SSH, see Keep your work after a disconnect with tmux, and attach automatically.
Nest on purpose
To run a tmux inside a tmux pane anyway, clear TMUX for that one command:
TMUX= tmux new-session -s inner
Keys then go to the outer tmux first. With the default prefix, Ctrl+B Ctrl+B sends one Ctrl+B through to the inner tmux (send-prefix), so Ctrl+B Ctrl+B d detaches the inner session. The same applies to tmux on a remote host that you reach from a local tmux. Many people give the inner tmux a different prefix with set -g prefix C-a in that host's ~/.tmux.conf.
Related
- Keep your work after a disconnect with tmux, and attach automatically
- Shell and tmux basics
- Check if a shell is running over SSH, inside tmux, or inside screen
- screen -r "There is no screen to be resumed"
- Scroll back and search history in tmux
- Change the tmux prefix key from Ctrl+B to Ctrl+A or another key