How to kill SSH session that was started with the -f option (run in background)

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











up vote
31
down vote

favorite
20












I'm pretty lost on this. From the man page:



 -f Requests ssh to go to background just before command execution.


After starting SSH with the -f option, I have a working tunnel. But after I finish using it I don't know how to further interaction with it. For example, I cannot close the SSH tunnel when I finish with it.



The usual methods I know don't work. For example, jobs returns nothing. The ~ command is not recognized (and I don't know exactly how to use it, anyway).



However, pgrep tells me that the SSH tunnel is still running (after I have closed the terminal, etc.). How do I interact with it? How do I close it?










share|improve this question



























    up vote
    31
    down vote

    favorite
    20












    I'm pretty lost on this. From the man page:



     -f Requests ssh to go to background just before command execution.


    After starting SSH with the -f option, I have a working tunnel. But after I finish using it I don't know how to further interaction with it. For example, I cannot close the SSH tunnel when I finish with it.



    The usual methods I know don't work. For example, jobs returns nothing. The ~ command is not recognized (and I don't know exactly how to use it, anyway).



    However, pgrep tells me that the SSH tunnel is still running (after I have closed the terminal, etc.). How do I interact with it? How do I close it?










    share|improve this question

























      up vote
      31
      down vote

      favorite
      20









      up vote
      31
      down vote

      favorite
      20






      20





      I'm pretty lost on this. From the man page:



       -f Requests ssh to go to background just before command execution.


      After starting SSH with the -f option, I have a working tunnel. But after I finish using it I don't know how to further interaction with it. For example, I cannot close the SSH tunnel when I finish with it.



      The usual methods I know don't work. For example, jobs returns nothing. The ~ command is not recognized (and I don't know exactly how to use it, anyway).



      However, pgrep tells me that the SSH tunnel is still running (after I have closed the terminal, etc.). How do I interact with it? How do I close it?










      share|improve this question















      I'm pretty lost on this. From the man page:



       -f Requests ssh to go to background just before command execution.


      After starting SSH with the -f option, I have a working tunnel. But after I finish using it I don't know how to further interaction with it. For example, I cannot close the SSH tunnel when I finish with it.



      The usual methods I know don't work. For example, jobs returns nothing. The ~ command is not recognized (and I don't know exactly how to use it, anyway).



      However, pgrep tells me that the SSH tunnel is still running (after I have closed the terminal, etc.). How do I interact with it? How do I close it?







      ssh ssh-tunneling background-process






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 20 '17 at 20:42









      David Resnick

      1034




      1034










      asked Jul 21 '13 at 4:03









      MountainX

      4,6572369119




      4,6572369119




















          4 Answers
          4






          active

          oldest

          votes

















          up vote
          44
          down vote













          An especially good solution for scripting is to use master mode, with a socket for control commands:



          ssh -f -N -M -S <path-to-socket> -L <port>:<host>:<port> <server>


          To close it again:



          ssh -S <path-to-socket> -O exit <server>


          This avoids both grepping for process ids and any timing issues that might be associated with other approaches.






          share|improve this answer


















          • 6




            Just to expand a little bit for the uninitiated, the <path-to-socket> is a path to a file the master instance of ssh client will create for inter-process communication with other ssh client instances that want to share master instance's connection. The created file will actually represent a unix domain socket. It can be named and located whatever and wherever you want it, e.g. /tmp/session1 (though it is recommended to name it using % patterns -- see ControlPath description in man ssh_config)
            – golem
            May 29 '15 at 20:37







          • 1




            Also it seems that the <server> part of the ssh -S <path-to-socket> -O exit <server> command can be any string, but it must be present. That's kind of clumsy.
            – golem
            May 29 '15 at 21:57






          • 1




            This answer and question are somewhat old, but i wanted to endorse this as probably the most elegant and 'correct' way of handling this problem that I've seen. Thank you, sir!
            – zentechinc
            Sep 17 '17 at 4:42


















          up vote
          23
          down vote



          accepted










          I found the solution here: http://www.g-loaded.eu/2006/11/24/auto-closing-ssh-tunnels/



          The best way – Tunnels that auto-close



          As it has been mentioned previously, instead of using the -f -N switch combination, we can just use -f alone, but also execute a command on the remote machine. But, which command should be executed, since we only need to initialize a tunnel?



          This is when sleep can be the most useful command of all! In this particular situation, sleep has two advantages:



          • it does nothing, so no resources are consumed

          • the user can specify for how long it will be executed

          How these help in auto-closing the ssh tunnel is explained below.



          We start the ssh session in the background, while executing the sleep command for 10 seconds on the remote machine. The number of seconds is not crucial. At the same time, we execute vncviewer exactly as before:



          [me@local]$ ssh -f -L 25901:127.0.0.1:5901 me@remote.example.org sleep 10; 
          vncviewer 127.0.0.1:25901:1


          In this case, the ssh client is instructed to fork the ssh session to the background (-f), create the tunnel (-L 25901:127.0.0.1:5901) and execute the sleep command on the remote server for 10 seconds (sleep 10).



          The difference between this method and the previous one (-N switch), basically, is that in this case the ssh client’s primary goal is not to create the tunnel, but rather to execute the sleep command for 10 seconds. The creation of the tunnel is some kind of side-effect, a secondary goal. If vncviewer was not used, the ssh client would exit after the 10 sec period, as it would have no more jobs to do, destroying the tunnel at the same time.



          During the execution of the sleep command, if another process, vncviewer in this case, starts using that tunnel and keeps it occupied beyond the 10 sec period, then, even if the ssh client finishes its remote job (execution of sleep), it cannot exit because another process occupies the tunnel. In other words, the ssh client cannot destroy the tunnel because it would have to kill vncviewer as well. When vncviewer stops using the tunnel, then the ssh client exits too, as it has already accomplished its goal.



          This way, no ssh processes are left running in the background.






          share|improve this answer
















          • 2




            A short remark/correction: As far as I know you have to specify vncviewer 127.0.0.1::25091 as you are using the port syntax and not the display syntax.
            – Christian Wolf
            Sep 26 '13 at 10:07

















          up vote
          5
          down vote













          To kill the tunnel, use ps -C ssh or ps | grep ssh or any other variant to determine which ssh process is running your tunnel. Then kill it.



          Alternatively, you can look for the process by determining which one has this port open:



          netstat -lnpt | awk '$4 ~ /:1234$/ sub(//.*/, "", $7); print $7'


          If you want to kill all ssh clients running on your machine (as your user), pkill ssh will do it.






          share|improve this answer



























            up vote
            4
            down vote













            As answered by others here, pkill ssh kills it.



            To create a tunnel that could be brought back, I start it in screen without the -f option, and then detach the screen with Ctrl-A D. To bring back the tunnel, call screen -r.






            share|improve this answer




















            • Brute force, but effective
              – mafrosis
              Feb 14 at 23:19










            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%2f83806%2fhow-to-kill-ssh-session-that-was-started-with-the-f-option-run-in-background%23new-answer', 'question_page');

            );

            Post as a guest






























            4 Answers
            4






            active

            oldest

            votes








            4 Answers
            4






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            44
            down vote













            An especially good solution for scripting is to use master mode, with a socket for control commands:



            ssh -f -N -M -S <path-to-socket> -L <port>:<host>:<port> <server>


            To close it again:



            ssh -S <path-to-socket> -O exit <server>


            This avoids both grepping for process ids and any timing issues that might be associated with other approaches.






            share|improve this answer


















            • 6




              Just to expand a little bit for the uninitiated, the <path-to-socket> is a path to a file the master instance of ssh client will create for inter-process communication with other ssh client instances that want to share master instance's connection. The created file will actually represent a unix domain socket. It can be named and located whatever and wherever you want it, e.g. /tmp/session1 (though it is recommended to name it using % patterns -- see ControlPath description in man ssh_config)
              – golem
              May 29 '15 at 20:37







            • 1




              Also it seems that the <server> part of the ssh -S <path-to-socket> -O exit <server> command can be any string, but it must be present. That's kind of clumsy.
              – golem
              May 29 '15 at 21:57






            • 1




              This answer and question are somewhat old, but i wanted to endorse this as probably the most elegant and 'correct' way of handling this problem that I've seen. Thank you, sir!
              – zentechinc
              Sep 17 '17 at 4:42















            up vote
            44
            down vote













            An especially good solution for scripting is to use master mode, with a socket for control commands:



            ssh -f -N -M -S <path-to-socket> -L <port>:<host>:<port> <server>


            To close it again:



            ssh -S <path-to-socket> -O exit <server>


            This avoids both grepping for process ids and any timing issues that might be associated with other approaches.






            share|improve this answer


















            • 6




              Just to expand a little bit for the uninitiated, the <path-to-socket> is a path to a file the master instance of ssh client will create for inter-process communication with other ssh client instances that want to share master instance's connection. The created file will actually represent a unix domain socket. It can be named and located whatever and wherever you want it, e.g. /tmp/session1 (though it is recommended to name it using % patterns -- see ControlPath description in man ssh_config)
              – golem
              May 29 '15 at 20:37







            • 1




              Also it seems that the <server> part of the ssh -S <path-to-socket> -O exit <server> command can be any string, but it must be present. That's kind of clumsy.
              – golem
              May 29 '15 at 21:57






            • 1




              This answer and question are somewhat old, but i wanted to endorse this as probably the most elegant and 'correct' way of handling this problem that I've seen. Thank you, sir!
              – zentechinc
              Sep 17 '17 at 4:42













            up vote
            44
            down vote










            up vote
            44
            down vote









            An especially good solution for scripting is to use master mode, with a socket for control commands:



            ssh -f -N -M -S <path-to-socket> -L <port>:<host>:<port> <server>


            To close it again:



            ssh -S <path-to-socket> -O exit <server>


            This avoids both grepping for process ids and any timing issues that might be associated with other approaches.






            share|improve this answer














            An especially good solution for scripting is to use master mode, with a socket for control commands:



            ssh -f -N -M -S <path-to-socket> -L <port>:<host>:<port> <server>


            To close it again:



            ssh -S <path-to-socket> -O exit <server>


            This avoids both grepping for process ids and any timing issues that might be associated with other approaches.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Sep 13 at 8:59









            Mr Shunz

            2,65811720




            2,65811720










            answered Oct 28 '14 at 18:17









            Brian H

            541143




            541143







            • 6




              Just to expand a little bit for the uninitiated, the <path-to-socket> is a path to a file the master instance of ssh client will create for inter-process communication with other ssh client instances that want to share master instance's connection. The created file will actually represent a unix domain socket. It can be named and located whatever and wherever you want it, e.g. /tmp/session1 (though it is recommended to name it using % patterns -- see ControlPath description in man ssh_config)
              – golem
              May 29 '15 at 20:37







            • 1




              Also it seems that the <server> part of the ssh -S <path-to-socket> -O exit <server> command can be any string, but it must be present. That's kind of clumsy.
              – golem
              May 29 '15 at 21:57






            • 1




              This answer and question are somewhat old, but i wanted to endorse this as probably the most elegant and 'correct' way of handling this problem that I've seen. Thank you, sir!
              – zentechinc
              Sep 17 '17 at 4:42













            • 6




              Just to expand a little bit for the uninitiated, the <path-to-socket> is a path to a file the master instance of ssh client will create for inter-process communication with other ssh client instances that want to share master instance's connection. The created file will actually represent a unix domain socket. It can be named and located whatever and wherever you want it, e.g. /tmp/session1 (though it is recommended to name it using % patterns -- see ControlPath description in man ssh_config)
              – golem
              May 29 '15 at 20:37







            • 1




              Also it seems that the <server> part of the ssh -S <path-to-socket> -O exit <server> command can be any string, but it must be present. That's kind of clumsy.
              – golem
              May 29 '15 at 21:57






            • 1




              This answer and question are somewhat old, but i wanted to endorse this as probably the most elegant and 'correct' way of handling this problem that I've seen. Thank you, sir!
              – zentechinc
              Sep 17 '17 at 4:42








            6




            6




            Just to expand a little bit for the uninitiated, the <path-to-socket> is a path to a file the master instance of ssh client will create for inter-process communication with other ssh client instances that want to share master instance's connection. The created file will actually represent a unix domain socket. It can be named and located whatever and wherever you want it, e.g. /tmp/session1 (though it is recommended to name it using % patterns -- see ControlPath description in man ssh_config)
            – golem
            May 29 '15 at 20:37





            Just to expand a little bit for the uninitiated, the <path-to-socket> is a path to a file the master instance of ssh client will create for inter-process communication with other ssh client instances that want to share master instance's connection. The created file will actually represent a unix domain socket. It can be named and located whatever and wherever you want it, e.g. /tmp/session1 (though it is recommended to name it using % patterns -- see ControlPath description in man ssh_config)
            – golem
            May 29 '15 at 20:37





            1




            1




            Also it seems that the <server> part of the ssh -S <path-to-socket> -O exit <server> command can be any string, but it must be present. That's kind of clumsy.
            – golem
            May 29 '15 at 21:57




            Also it seems that the <server> part of the ssh -S <path-to-socket> -O exit <server> command can be any string, but it must be present. That's kind of clumsy.
            – golem
            May 29 '15 at 21:57




            1




            1




            This answer and question are somewhat old, but i wanted to endorse this as probably the most elegant and 'correct' way of handling this problem that I've seen. Thank you, sir!
            – zentechinc
            Sep 17 '17 at 4:42





            This answer and question are somewhat old, but i wanted to endorse this as probably the most elegant and 'correct' way of handling this problem that I've seen. Thank you, sir!
            – zentechinc
            Sep 17 '17 at 4:42













            up vote
            23
            down vote



            accepted










            I found the solution here: http://www.g-loaded.eu/2006/11/24/auto-closing-ssh-tunnels/



            The best way – Tunnels that auto-close



            As it has been mentioned previously, instead of using the -f -N switch combination, we can just use -f alone, but also execute a command on the remote machine. But, which command should be executed, since we only need to initialize a tunnel?



            This is when sleep can be the most useful command of all! In this particular situation, sleep has two advantages:



            • it does nothing, so no resources are consumed

            • the user can specify for how long it will be executed

            How these help in auto-closing the ssh tunnel is explained below.



            We start the ssh session in the background, while executing the sleep command for 10 seconds on the remote machine. The number of seconds is not crucial. At the same time, we execute vncviewer exactly as before:



            [me@local]$ ssh -f -L 25901:127.0.0.1:5901 me@remote.example.org sleep 10; 
            vncviewer 127.0.0.1:25901:1


            In this case, the ssh client is instructed to fork the ssh session to the background (-f), create the tunnel (-L 25901:127.0.0.1:5901) and execute the sleep command on the remote server for 10 seconds (sleep 10).



            The difference between this method and the previous one (-N switch), basically, is that in this case the ssh client’s primary goal is not to create the tunnel, but rather to execute the sleep command for 10 seconds. The creation of the tunnel is some kind of side-effect, a secondary goal. If vncviewer was not used, the ssh client would exit after the 10 sec period, as it would have no more jobs to do, destroying the tunnel at the same time.



            During the execution of the sleep command, if another process, vncviewer in this case, starts using that tunnel and keeps it occupied beyond the 10 sec period, then, even if the ssh client finishes its remote job (execution of sleep), it cannot exit because another process occupies the tunnel. In other words, the ssh client cannot destroy the tunnel because it would have to kill vncviewer as well. When vncviewer stops using the tunnel, then the ssh client exits too, as it has already accomplished its goal.



            This way, no ssh processes are left running in the background.






            share|improve this answer
















            • 2




              A short remark/correction: As far as I know you have to specify vncviewer 127.0.0.1::25091 as you are using the port syntax and not the display syntax.
              – Christian Wolf
              Sep 26 '13 at 10:07














            up vote
            23
            down vote



            accepted










            I found the solution here: http://www.g-loaded.eu/2006/11/24/auto-closing-ssh-tunnels/



            The best way – Tunnels that auto-close



            As it has been mentioned previously, instead of using the -f -N switch combination, we can just use -f alone, but also execute a command on the remote machine. But, which command should be executed, since we only need to initialize a tunnel?



            This is when sleep can be the most useful command of all! In this particular situation, sleep has two advantages:



            • it does nothing, so no resources are consumed

            • the user can specify for how long it will be executed

            How these help in auto-closing the ssh tunnel is explained below.



            We start the ssh session in the background, while executing the sleep command for 10 seconds on the remote machine. The number of seconds is not crucial. At the same time, we execute vncviewer exactly as before:



            [me@local]$ ssh -f -L 25901:127.0.0.1:5901 me@remote.example.org sleep 10; 
            vncviewer 127.0.0.1:25901:1


            In this case, the ssh client is instructed to fork the ssh session to the background (-f), create the tunnel (-L 25901:127.0.0.1:5901) and execute the sleep command on the remote server for 10 seconds (sleep 10).



            The difference between this method and the previous one (-N switch), basically, is that in this case the ssh client’s primary goal is not to create the tunnel, but rather to execute the sleep command for 10 seconds. The creation of the tunnel is some kind of side-effect, a secondary goal. If vncviewer was not used, the ssh client would exit after the 10 sec period, as it would have no more jobs to do, destroying the tunnel at the same time.



            During the execution of the sleep command, if another process, vncviewer in this case, starts using that tunnel and keeps it occupied beyond the 10 sec period, then, even if the ssh client finishes its remote job (execution of sleep), it cannot exit because another process occupies the tunnel. In other words, the ssh client cannot destroy the tunnel because it would have to kill vncviewer as well. When vncviewer stops using the tunnel, then the ssh client exits too, as it has already accomplished its goal.



            This way, no ssh processes are left running in the background.






            share|improve this answer
















            • 2




              A short remark/correction: As far as I know you have to specify vncviewer 127.0.0.1::25091 as you are using the port syntax and not the display syntax.
              – Christian Wolf
              Sep 26 '13 at 10:07












            up vote
            23
            down vote



            accepted







            up vote
            23
            down vote



            accepted






            I found the solution here: http://www.g-loaded.eu/2006/11/24/auto-closing-ssh-tunnels/



            The best way – Tunnels that auto-close



            As it has been mentioned previously, instead of using the -f -N switch combination, we can just use -f alone, but also execute a command on the remote machine. But, which command should be executed, since we only need to initialize a tunnel?



            This is when sleep can be the most useful command of all! In this particular situation, sleep has two advantages:



            • it does nothing, so no resources are consumed

            • the user can specify for how long it will be executed

            How these help in auto-closing the ssh tunnel is explained below.



            We start the ssh session in the background, while executing the sleep command for 10 seconds on the remote machine. The number of seconds is not crucial. At the same time, we execute vncviewer exactly as before:



            [me@local]$ ssh -f -L 25901:127.0.0.1:5901 me@remote.example.org sleep 10; 
            vncviewer 127.0.0.1:25901:1


            In this case, the ssh client is instructed to fork the ssh session to the background (-f), create the tunnel (-L 25901:127.0.0.1:5901) and execute the sleep command on the remote server for 10 seconds (sleep 10).



            The difference between this method and the previous one (-N switch), basically, is that in this case the ssh client’s primary goal is not to create the tunnel, but rather to execute the sleep command for 10 seconds. The creation of the tunnel is some kind of side-effect, a secondary goal. If vncviewer was not used, the ssh client would exit after the 10 sec period, as it would have no more jobs to do, destroying the tunnel at the same time.



            During the execution of the sleep command, if another process, vncviewer in this case, starts using that tunnel and keeps it occupied beyond the 10 sec period, then, even if the ssh client finishes its remote job (execution of sleep), it cannot exit because another process occupies the tunnel. In other words, the ssh client cannot destroy the tunnel because it would have to kill vncviewer as well. When vncviewer stops using the tunnel, then the ssh client exits too, as it has already accomplished its goal.



            This way, no ssh processes are left running in the background.






            share|improve this answer












            I found the solution here: http://www.g-loaded.eu/2006/11/24/auto-closing-ssh-tunnels/



            The best way – Tunnels that auto-close



            As it has been mentioned previously, instead of using the -f -N switch combination, we can just use -f alone, but also execute a command on the remote machine. But, which command should be executed, since we only need to initialize a tunnel?



            This is when sleep can be the most useful command of all! In this particular situation, sleep has two advantages:



            • it does nothing, so no resources are consumed

            • the user can specify for how long it will be executed

            How these help in auto-closing the ssh tunnel is explained below.



            We start the ssh session in the background, while executing the sleep command for 10 seconds on the remote machine. The number of seconds is not crucial. At the same time, we execute vncviewer exactly as before:



            [me@local]$ ssh -f -L 25901:127.0.0.1:5901 me@remote.example.org sleep 10; 
            vncviewer 127.0.0.1:25901:1


            In this case, the ssh client is instructed to fork the ssh session to the background (-f), create the tunnel (-L 25901:127.0.0.1:5901) and execute the sleep command on the remote server for 10 seconds (sleep 10).



            The difference between this method and the previous one (-N switch), basically, is that in this case the ssh client’s primary goal is not to create the tunnel, but rather to execute the sleep command for 10 seconds. The creation of the tunnel is some kind of side-effect, a secondary goal. If vncviewer was not used, the ssh client would exit after the 10 sec period, as it would have no more jobs to do, destroying the tunnel at the same time.



            During the execution of the sleep command, if another process, vncviewer in this case, starts using that tunnel and keeps it occupied beyond the 10 sec period, then, even if the ssh client finishes its remote job (execution of sleep), it cannot exit because another process occupies the tunnel. In other words, the ssh client cannot destroy the tunnel because it would have to kill vncviewer as well. When vncviewer stops using the tunnel, then the ssh client exits too, as it has already accomplished its goal.



            This way, no ssh processes are left running in the background.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Jul 21 '13 at 4:59









            MountainX

            4,6572369119




            4,6572369119







            • 2




              A short remark/correction: As far as I know you have to specify vncviewer 127.0.0.1::25091 as you are using the port syntax and not the display syntax.
              – Christian Wolf
              Sep 26 '13 at 10:07












            • 2




              A short remark/correction: As far as I know you have to specify vncviewer 127.0.0.1::25091 as you are using the port syntax and not the display syntax.
              – Christian Wolf
              Sep 26 '13 at 10:07







            2




            2




            A short remark/correction: As far as I know you have to specify vncviewer 127.0.0.1::25091 as you are using the port syntax and not the display syntax.
            – Christian Wolf
            Sep 26 '13 at 10:07




            A short remark/correction: As far as I know you have to specify vncviewer 127.0.0.1::25091 as you are using the port syntax and not the display syntax.
            – Christian Wolf
            Sep 26 '13 at 10:07










            up vote
            5
            down vote













            To kill the tunnel, use ps -C ssh or ps | grep ssh or any other variant to determine which ssh process is running your tunnel. Then kill it.



            Alternatively, you can look for the process by determining which one has this port open:



            netstat -lnpt | awk '$4 ~ /:1234$/ sub(//.*/, "", $7); print $7'


            If you want to kill all ssh clients running on your machine (as your user), pkill ssh will do it.






            share|improve this answer
























              up vote
              5
              down vote













              To kill the tunnel, use ps -C ssh or ps | grep ssh or any other variant to determine which ssh process is running your tunnel. Then kill it.



              Alternatively, you can look for the process by determining which one has this port open:



              netstat -lnpt | awk '$4 ~ /:1234$/ sub(//.*/, "", $7); print $7'


              If you want to kill all ssh clients running on your machine (as your user), pkill ssh will do it.






              share|improve this answer






















                up vote
                5
                down vote










                up vote
                5
                down vote









                To kill the tunnel, use ps -C ssh or ps | grep ssh or any other variant to determine which ssh process is running your tunnel. Then kill it.



                Alternatively, you can look for the process by determining which one has this port open:



                netstat -lnpt | awk '$4 ~ /:1234$/ sub(//.*/, "", $7); print $7'


                If you want to kill all ssh clients running on your machine (as your user), pkill ssh will do it.






                share|improve this answer












                To kill the tunnel, use ps -C ssh or ps | grep ssh or any other variant to determine which ssh process is running your tunnel. Then kill it.



                Alternatively, you can look for the process by determining which one has this port open:



                netstat -lnpt | awk '$4 ~ /:1234$/ sub(//.*/, "", $7); print $7'


                If you want to kill all ssh clients running on your machine (as your user), pkill ssh will do it.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Jul 21 '13 at 23:49









                Gilles

                511k12010141544




                511k12010141544




















                    up vote
                    4
                    down vote













                    As answered by others here, pkill ssh kills it.



                    To create a tunnel that could be brought back, I start it in screen without the -f option, and then detach the screen with Ctrl-A D. To bring back the tunnel, call screen -r.






                    share|improve this answer




















                    • Brute force, but effective
                      – mafrosis
                      Feb 14 at 23:19














                    up vote
                    4
                    down vote













                    As answered by others here, pkill ssh kills it.



                    To create a tunnel that could be brought back, I start it in screen without the -f option, and then detach the screen with Ctrl-A D. To bring back the tunnel, call screen -r.






                    share|improve this answer




















                    • Brute force, but effective
                      – mafrosis
                      Feb 14 at 23:19












                    up vote
                    4
                    down vote










                    up vote
                    4
                    down vote









                    As answered by others here, pkill ssh kills it.



                    To create a tunnel that could be brought back, I start it in screen without the -f option, and then detach the screen with Ctrl-A D. To bring back the tunnel, call screen -r.






                    share|improve this answer












                    As answered by others here, pkill ssh kills it.



                    To create a tunnel that could be brought back, I start it in screen without the -f option, and then detach the screen with Ctrl-A D. To bring back the tunnel, call screen -r.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 5 '17 at 6:22









                    Haotian Yang

                    411




                    411











                    • Brute force, but effective
                      – mafrosis
                      Feb 14 at 23:19
















                    • Brute force, but effective
                      – mafrosis
                      Feb 14 at 23:19















                    Brute force, but effective
                    – mafrosis
                    Feb 14 at 23:19




                    Brute force, but effective
                    – mafrosis
                    Feb 14 at 23:19

















                     

                    draft saved


                    draft discarded















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f83806%2fhow-to-kill-ssh-session-that-was-started-with-the-f-option-run-in-background%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?

                    Displaying single band from multi-band raster using QGIS

                    How many registers does an x86_64 CPU actually have?