I would like tmux to create a new-window when I ssh onto a machine from an existing tmux session. However, I do not want a tmux session started on the new machine!

I have the following in my .bashrc, so that tmux automatically starts up:

if [[ "$TERM" != "screen" ]]
   # try to attach to existing session, or start a new one
   tmux attach-session -t "$USER" || tmux -2 new-session -s "$USER"

I also have an ssh function:

alias ssh='ssh_func'
ssh_func (){
    if [[ "$TERM" == "screen" ]]; then
        tmux new-window -n "$1" "ssh $@";
        /usr/bin/ssh "$@";

This works ok, but I do not want a tmux session started on the machine I ssh to, because this gives me 2 sessions in the same terminal window. Is there anything I can put in my .bashrc so that tmux does not start up on a machine if the ssh command has been invoked from a tmux session?

I am using PuTTY and tmux 1.5.

  • I don't understand your requirements. If you run ssh from within tmux, you'll have $TERM = screen, so you won't be trying to attach to a tmux window. How does that differ from what you want? Aug 19, 2011 at 16:21
  • If I ssh from tmux I don't get TERM=screen. I get TERM=linux. It doesn't seem to get exported.
    – dogbane
    Aug 19, 2011 at 18:12
  • 2
    If you get a different value of TERM after ssh'ing, there's something wrong with your setup. As far as I can tell the code you posted should do what you want on a normal setup. You need to investigate what is mangling $TERM. Do you have any code that sets it in ~/.ssh/* or in ~/.* or in /etc/*? Aug 19, 2011 at 19:07
  • @Gilles you're right. I was setting TERM in one of my dotfiles. Removing it has worked. Please add this as an answer. Thanks!
    – dogbane
    Aug 20, 2011 at 11:23

2 Answers 2


Given the code you've posted, if you run ssh from within tmux, you'll have $TERM = screen, so you won't be trying to attach to a tmux window. In other words, the code you already have should work as desired. There's something fishy going on. Make sure your dot files don't mess up the TERM variable (if you need to modify TERM, which is very rare, make sure to do it only in very specific circumstances; in particular don't change it if it's screen).


You can test for presence of the SSH_CONNECTION environment variable: if that variable is defined then the shell has been started from SSH. For example:

if [ -n "$SSH_CONNECTION" ]; then
  # running within SSH session, do not start tmux
  # logged in from console/terminal, start tmux
  tmux ...

The variable contains four space-separated values: client IP address, client port number, server IP address, and server port number. You can use this for more complex matching, e.g.:

   # client is client.ip.v4.addr != this host's addr
   echo "you're connecting from a remote host, starting tmux"
   tmux ...
   # if server.ip.v4.addr == this hosts' address, then SSH_CONNECTION
   # is not inherited from another connection
   echo "SSH_CNNECTION is not stale"
   # no SSH_CONNECTION at all

More details on the SSH_CONNECTION environment variable in the "ENVIRONMENT" section of the ssh(1) manpage.

  • This won't work for me because I always use ssh to log on to an initial machine from PuTTY, so my SSH_CONNECTION is always set. I need to somehow detect if the ssh was invoked from within a tmux session.
    – dogbane
    Aug 19, 2011 at 13:08
  • You can then match on the actual contents of SSH_CONNECTION, not just its presence. I've edited the answer with an example. Aug 19, 2011 at 13:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .