Don't nest tmux in a remote ssh shell

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
0
down vote

favorite












I've set up the .zshrc of my user account on all of the hosts to which I connect to automatically start tmux on login as long as it's not already running.



if [ -z "$TMUX" ]; then
tmux attach -d || tmux new
fi



This works well until I ssh (or mosh) into my own account on a remote host from within a tmux session on the local host. Since the $TMUX macro isn't passed from the local host to the remote, tmux launches on the remote host and I now have two nested tmux sessions.



Is there a way to avoid this while keeping the auto-launching behaviour? Ideally I'd like the remote shell to know that it's being launched from within a tmux session on the host that is connecting and to not launch a second tmux instance.



I've already tried checking $TERM in the remote shell but it is always xterm-256color regardless of whether it is running within a tmux session on the local machine.







share|improve this question
















  • 3




    Related: How to pass environment variables to a new ssh-initiated login session
    – Mark Plotnick
    Jan 30 at 17:28














up vote
0
down vote

favorite












I've set up the .zshrc of my user account on all of the hosts to which I connect to automatically start tmux on login as long as it's not already running.



if [ -z "$TMUX" ]; then
tmux attach -d || tmux new
fi



This works well until I ssh (or mosh) into my own account on a remote host from within a tmux session on the local host. Since the $TMUX macro isn't passed from the local host to the remote, tmux launches on the remote host and I now have two nested tmux sessions.



Is there a way to avoid this while keeping the auto-launching behaviour? Ideally I'd like the remote shell to know that it's being launched from within a tmux session on the host that is connecting and to not launch a second tmux instance.



I've already tried checking $TERM in the remote shell but it is always xterm-256color regardless of whether it is running within a tmux session on the local machine.







share|improve this question
















  • 3




    Related: How to pass environment variables to a new ssh-initiated login session
    – Mark Plotnick
    Jan 30 at 17:28












up vote
0
down vote

favorite









up vote
0
down vote

favorite











I've set up the .zshrc of my user account on all of the hosts to which I connect to automatically start tmux on login as long as it's not already running.



if [ -z "$TMUX" ]; then
tmux attach -d || tmux new
fi



This works well until I ssh (or mosh) into my own account on a remote host from within a tmux session on the local host. Since the $TMUX macro isn't passed from the local host to the remote, tmux launches on the remote host and I now have two nested tmux sessions.



Is there a way to avoid this while keeping the auto-launching behaviour? Ideally I'd like the remote shell to know that it's being launched from within a tmux session on the host that is connecting and to not launch a second tmux instance.



I've already tried checking $TERM in the remote shell but it is always xterm-256color regardless of whether it is running within a tmux session on the local machine.







share|improve this question












I've set up the .zshrc of my user account on all of the hosts to which I connect to automatically start tmux on login as long as it's not already running.



if [ -z "$TMUX" ]; then
tmux attach -d || tmux new
fi



This works well until I ssh (or mosh) into my own account on a remote host from within a tmux session on the local host. Since the $TMUX macro isn't passed from the local host to the remote, tmux launches on the remote host and I now have two nested tmux sessions.



Is there a way to avoid this while keeping the auto-launching behaviour? Ideally I'd like the remote shell to know that it's being launched from within a tmux session on the host that is connecting and to not launch a second tmux instance.



I've already tried checking $TERM in the remote shell but it is always xterm-256color regardless of whether it is running within a tmux session on the local machine.









share|improve this question











share|improve this question




share|improve this question










asked Jan 30 at 17:23









Gene Goykhman

1112




1112







  • 3




    Related: How to pass environment variables to a new ssh-initiated login session
    – Mark Plotnick
    Jan 30 at 17:28












  • 3




    Related: How to pass environment variables to a new ssh-initiated login session
    – Mark Plotnick
    Jan 30 at 17:28







3




3




Related: How to pass environment variables to a new ssh-initiated login session
– Mark Plotnick
Jan 30 at 17:28




Related: How to pass environment variables to a new ssh-initiated login session
– Mark Plotnick
Jan 30 at 17:28










1 Answer
1






active

oldest

votes

















up vote
1
down vote













Thanks to @mark-plotnick 's comment I think I have solution that works for me.



On the remote host, add to /etc/ssh/sshd_config:



AcceptEnv TMUX



On the local host, add to ~/.ssh/config:



Host *
SendEnv TMUX



Now the value of the $TMUX env variable is sent to the remote host, and tmux no longer launches when the value is non-blank.



I don't mind making the server-side change but I wish I didn't have to edit every client ~/.ssh/config so I'm open to additional suggestions.






share|improve this answer




















    Your Answer







    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "106"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );








     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f420733%2fdont-nest-tmux-in-a-remote-ssh-shell%23new-answer', 'question_page');

    );

    Post as a guest






























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote













    Thanks to @mark-plotnick 's comment I think I have solution that works for me.



    On the remote host, add to /etc/ssh/sshd_config:



    AcceptEnv TMUX



    On the local host, add to ~/.ssh/config:



    Host *
    SendEnv TMUX



    Now the value of the $TMUX env variable is sent to the remote host, and tmux no longer launches when the value is non-blank.



    I don't mind making the server-side change but I wish I didn't have to edit every client ~/.ssh/config so I'm open to additional suggestions.






    share|improve this answer
























      up vote
      1
      down vote













      Thanks to @mark-plotnick 's comment I think I have solution that works for me.



      On the remote host, add to /etc/ssh/sshd_config:



      AcceptEnv TMUX



      On the local host, add to ~/.ssh/config:



      Host *
      SendEnv TMUX



      Now the value of the $TMUX env variable is sent to the remote host, and tmux no longer launches when the value is non-blank.



      I don't mind making the server-side change but I wish I didn't have to edit every client ~/.ssh/config so I'm open to additional suggestions.






      share|improve this answer






















        up vote
        1
        down vote










        up vote
        1
        down vote









        Thanks to @mark-plotnick 's comment I think I have solution that works for me.



        On the remote host, add to /etc/ssh/sshd_config:



        AcceptEnv TMUX



        On the local host, add to ~/.ssh/config:



        Host *
        SendEnv TMUX



        Now the value of the $TMUX env variable is sent to the remote host, and tmux no longer launches when the value is non-blank.



        I don't mind making the server-side change but I wish I didn't have to edit every client ~/.ssh/config so I'm open to additional suggestions.






        share|improve this answer












        Thanks to @mark-plotnick 's comment I think I have solution that works for me.



        On the remote host, add to /etc/ssh/sshd_config:



        AcceptEnv TMUX



        On the local host, add to ~/.ssh/config:



        Host *
        SendEnv TMUX



        Now the value of the $TMUX env variable is sent to the remote host, and tmux no longer launches when the value is non-blank.



        I don't mind making the server-side change but I wish I didn't have to edit every client ~/.ssh/config so I'm open to additional suggestions.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 30 at 19:01









        Gene Goykhman

        1112




        1112






















             

            draft saved


            draft discarded


























             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f420733%2fdont-nest-tmux-in-a-remote-ssh-shell%23new-answer', 'question_page');

            );

            Post as a guest













































































            Popular posts from this blog

            How to check contact read email or not when send email to Individual?

            Bahrain

            Postfix configuration issue with fips on centos 7; mailgun relay