Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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" ]]
then
   # try to attach to existing session, or start a new one
   tmux attach-session -t "$USER" || tmux -2 new-session -s "$USER"
   exit
fi

I also have an ssh function:

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

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.

share|improve this question
    
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? –  Gilles Aug 19 '11 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 '11 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/*? –  Gilles Aug 19 '11 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 '11 at 11:23
add comment

2 Answers

up vote 1 down vote accepted

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).

share|improve this answer
add comment

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
  ...
else
  # logged in from console/terminal, start tmux
  tmux ...
fi

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.:

case "$SSH_CONNECTION" in
  client.ip.v4.addr*)
   # client is client.ip.v4.addr != this host's addr
   echo "you're connecting from a remote host, starting tmux"
   tmux ...
   ;;
  *server.ip.v4.addr)
   # 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
   ;;
esac

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

share|improve this answer
    
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 '11 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. –  Riccardo Murri Aug 19 '11 at 13:28
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.