Auto reconnect to VPN on disconnect?












6














I'm an Ubuntu GNOME user and I was wondering if there is a way to be able to automatically reconnect to a VPN on disconnection. I'm using the OpenVPN protocol.



I've checked Network Manager thoroughly but could not find such an option, only to connect to the VPN when connected to a specific WiFi.










share|improve this question



























    6














    I'm an Ubuntu GNOME user and I was wondering if there is a way to be able to automatically reconnect to a VPN on disconnection. I'm using the OpenVPN protocol.



    I've checked Network Manager thoroughly but could not find such an option, only to connect to the VPN when connected to a specific WiFi.










    share|improve this question

























      6












      6








      6


      5





      I'm an Ubuntu GNOME user and I was wondering if there is a way to be able to automatically reconnect to a VPN on disconnection. I'm using the OpenVPN protocol.



      I've checked Network Manager thoroughly but could not find such an option, only to connect to the VPN when connected to a specific WiFi.










      share|improve this question













      I'm an Ubuntu GNOME user and I was wondering if there is a way to be able to automatically reconnect to a VPN on disconnection. I'm using the OpenVPN protocol.



      I've checked Network Manager thoroughly but could not find such an option, only to connect to the VPN when connected to a specific WiFi.







      vpn ubuntu-gnome






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Sep 27 '15 at 17:24









      Inoki

      3232516




      3232516






















          6 Answers
          6






          active

          oldest

          votes


















          11














          OpenVPN has a build-in mechanism to automatically detect dead links and reconnect. In Network Manager go to "Edit Connections", select your VPN connection and choose "Edit". In the "VPN" tab click on "Advanced..." and go to the "General" Tab. There you have two relevant options:



          "Specify ping interval" tell OpenVPN how frequently to check if the link is still alive. "Specify exit or restart ping" tells it how long to wait until it takes action and which action to take.



          Example: My setting are "30 / ping-restart / 300". This means OpenVPN checks every 30 seconds if the link is still active. If the link is down for 300 seconds it initiates a restart.



          This way there is no need for external scripts...






          share|improve this answer





















          • This should be the accepted answer.
            – necbot
            Sep 12 '17 at 1:50






          • 3




            This is not true for Ubuntu 18.04 at least. I have ping interval and ping-restart but it will disable itself anyway.
            – Sarge Borsch
            Aug 13 at 9:42



















          1














          After a bit of digging I found this answer, tested it (on Ubuntu GNOME 15.04) and so far it seems working.



          The only thing I might add is that once the script file is created it doesn't necessarily need to be saved to your /home folder. You can save it anywhere, make it executable and add it to the list of startup programs.






          share|improve this answer































            1














            As of 18.10 (cannot check in earlier versions) VPN connections in NetworkManager have a setting vpn.persistent which does just that: reconnects to a VPN on connection loss until you disconnect manually. It is set to "no" by default and unfortunately isn't exposed neither in Gnome Network Settings nor in nm-connection-editor.



            But you can set it through a CLI like this:



            nmcli connection modify <Your VPN connection name> vpn.persistent yes



            The connection must exist before you do that, of course






            share|improve this answer





























              0














              After claiming it was a SMOP (Simple Matter of Programming), I wrote a bash script that monitors for "Link Down", then executes a user script. Less CPU usage, more responsive than the while true....sleep 30 method. See my answer at here. It's about "rotating WiFi connections", but will probably work for you, too






              share|improve this answer































                0














                this script will work on 16.04 where nmcli con status no longer works:



                #!/bin/bash
                CON="purple"
                STATUS=`nmcli con show --active | grep purple | cut -f1 -d " "`
                if [ -z "$STATUS" ]; then
                nmcli con up $CON
                fi





                share|improve this answer























                • A bit shorter STATUS="$(nmcli con show -f name | grep purple)". Or you can simply check if the actual connection is active by doing nmcli con show --active id 'purple'
                  – smac89
                  May 29 '17 at 2:38





















                0














                I think the complete answer based on other answers goes as follow :



                #!/bin/bash +x
                while [ "true" ]
                do
                CON="Your-VPN-Name"
                STATUS=`nmcli con show --active | grep $CON | cut -f1 -d " "`
                if [ -z "$STATUS" ]; then
                echo "Disconnected, trying to reconnect..."
                (sleep 1s && nmcli con up $CON)
                else
                echo "Already connected !"
                fi
                sleep 30
                done





                share|improve this answer























                  Your Answer








                  StackExchange.ready(function() {
                  var channelOptions = {
                  tags: "".split(" "),
                  id: "89"
                  };
                  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',
                  autoActivateHeartbeat: false,
                  convertImagesToLinks: true,
                  noModals: true,
                  showLowRepImageUploadWarning: true,
                  reputationToPostImages: 10,
                  bindNavPrevention: true,
                  postfix: "",
                  imageUploader: {
                  brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                  contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                  allowUrls: true
                  },
                  onDemand: true,
                  discardSelector: ".discard-answer"
                  ,immediatelyShowMarkdownHelp:true
                  });


                  }
                  });














                  draft saved

                  draft discarded


















                  StackExchange.ready(
                  function () {
                  StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f679059%2fauto-reconnect-to-vpn-on-disconnect%23new-answer', 'question_page');
                  }
                  );

                  Post as a guest















                  Required, but never shown

























                  6 Answers
                  6






                  active

                  oldest

                  votes








                  6 Answers
                  6






                  active

                  oldest

                  votes









                  active

                  oldest

                  votes






                  active

                  oldest

                  votes









                  11














                  OpenVPN has a build-in mechanism to automatically detect dead links and reconnect. In Network Manager go to "Edit Connections", select your VPN connection and choose "Edit". In the "VPN" tab click on "Advanced..." and go to the "General" Tab. There you have two relevant options:



                  "Specify ping interval" tell OpenVPN how frequently to check if the link is still alive. "Specify exit or restart ping" tells it how long to wait until it takes action and which action to take.



                  Example: My setting are "30 / ping-restart / 300". This means OpenVPN checks every 30 seconds if the link is still active. If the link is down for 300 seconds it initiates a restart.



                  This way there is no need for external scripts...






                  share|improve this answer





















                  • This should be the accepted answer.
                    – necbot
                    Sep 12 '17 at 1:50






                  • 3




                    This is not true for Ubuntu 18.04 at least. I have ping interval and ping-restart but it will disable itself anyway.
                    – Sarge Borsch
                    Aug 13 at 9:42
















                  11














                  OpenVPN has a build-in mechanism to automatically detect dead links and reconnect. In Network Manager go to "Edit Connections", select your VPN connection and choose "Edit". In the "VPN" tab click on "Advanced..." and go to the "General" Tab. There you have two relevant options:



                  "Specify ping interval" tell OpenVPN how frequently to check if the link is still alive. "Specify exit or restart ping" tells it how long to wait until it takes action and which action to take.



                  Example: My setting are "30 / ping-restart / 300". This means OpenVPN checks every 30 seconds if the link is still active. If the link is down for 300 seconds it initiates a restart.



                  This way there is no need for external scripts...






                  share|improve this answer





















                  • This should be the accepted answer.
                    – necbot
                    Sep 12 '17 at 1:50






                  • 3




                    This is not true for Ubuntu 18.04 at least. I have ping interval and ping-restart but it will disable itself anyway.
                    – Sarge Borsch
                    Aug 13 at 9:42














                  11












                  11








                  11






                  OpenVPN has a build-in mechanism to automatically detect dead links and reconnect. In Network Manager go to "Edit Connections", select your VPN connection and choose "Edit". In the "VPN" tab click on "Advanced..." and go to the "General" Tab. There you have two relevant options:



                  "Specify ping interval" tell OpenVPN how frequently to check if the link is still alive. "Specify exit or restart ping" tells it how long to wait until it takes action and which action to take.



                  Example: My setting are "30 / ping-restart / 300". This means OpenVPN checks every 30 seconds if the link is still active. If the link is down for 300 seconds it initiates a restart.



                  This way there is no need for external scripts...






                  share|improve this answer












                  OpenVPN has a build-in mechanism to automatically detect dead links and reconnect. In Network Manager go to "Edit Connections", select your VPN connection and choose "Edit". In the "VPN" tab click on "Advanced..." and go to the "General" Tab. There you have two relevant options:



                  "Specify ping interval" tell OpenVPN how frequently to check if the link is still alive. "Specify exit or restart ping" tells it how long to wait until it takes action and which action to take.



                  Example: My setting are "30 / ping-restart / 300". This means OpenVPN checks every 30 seconds if the link is still active. If the link is down for 300 seconds it initiates a restart.



                  This way there is no need for external scripts...







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered May 30 '16 at 8:43









                  OttoEisen

                  2921314




                  2921314












                  • This should be the accepted answer.
                    – necbot
                    Sep 12 '17 at 1:50






                  • 3




                    This is not true for Ubuntu 18.04 at least. I have ping interval and ping-restart but it will disable itself anyway.
                    – Sarge Borsch
                    Aug 13 at 9:42


















                  • This should be the accepted answer.
                    – necbot
                    Sep 12 '17 at 1:50






                  • 3




                    This is not true for Ubuntu 18.04 at least. I have ping interval and ping-restart but it will disable itself anyway.
                    – Sarge Borsch
                    Aug 13 at 9:42
















                  This should be the accepted answer.
                  – necbot
                  Sep 12 '17 at 1:50




                  This should be the accepted answer.
                  – necbot
                  Sep 12 '17 at 1:50




                  3




                  3




                  This is not true for Ubuntu 18.04 at least. I have ping interval and ping-restart but it will disable itself anyway.
                  – Sarge Borsch
                  Aug 13 at 9:42




                  This is not true for Ubuntu 18.04 at least. I have ping interval and ping-restart but it will disable itself anyway.
                  – Sarge Borsch
                  Aug 13 at 9:42













                  1














                  After a bit of digging I found this answer, tested it (on Ubuntu GNOME 15.04) and so far it seems working.



                  The only thing I might add is that once the script file is created it doesn't necessarily need to be saved to your /home folder. You can save it anywhere, make it executable and add it to the list of startup programs.






                  share|improve this answer




























                    1














                    After a bit of digging I found this answer, tested it (on Ubuntu GNOME 15.04) and so far it seems working.



                    The only thing I might add is that once the script file is created it doesn't necessarily need to be saved to your /home folder. You can save it anywhere, make it executable and add it to the list of startup programs.






                    share|improve this answer


























                      1












                      1








                      1






                      After a bit of digging I found this answer, tested it (on Ubuntu GNOME 15.04) and so far it seems working.



                      The only thing I might add is that once the script file is created it doesn't necessarily need to be saved to your /home folder. You can save it anywhere, make it executable and add it to the list of startup programs.






                      share|improve this answer














                      After a bit of digging I found this answer, tested it (on Ubuntu GNOME 15.04) and so far it seems working.



                      The only thing I might add is that once the script file is created it doesn't necessarily need to be saved to your /home folder. You can save it anywhere, make it executable and add it to the list of startup programs.







                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      answered Sep 27 '15 at 17:40


























                      community wiki





                      Inoki
























                          1














                          As of 18.10 (cannot check in earlier versions) VPN connections in NetworkManager have a setting vpn.persistent which does just that: reconnects to a VPN on connection loss until you disconnect manually. It is set to "no" by default and unfortunately isn't exposed neither in Gnome Network Settings nor in nm-connection-editor.



                          But you can set it through a CLI like this:



                          nmcli connection modify <Your VPN connection name> vpn.persistent yes



                          The connection must exist before you do that, of course






                          share|improve this answer


























                            1














                            As of 18.10 (cannot check in earlier versions) VPN connections in NetworkManager have a setting vpn.persistent which does just that: reconnects to a VPN on connection loss until you disconnect manually. It is set to "no" by default and unfortunately isn't exposed neither in Gnome Network Settings nor in nm-connection-editor.



                            But you can set it through a CLI like this:



                            nmcli connection modify <Your VPN connection name> vpn.persistent yes



                            The connection must exist before you do that, of course






                            share|improve this answer
























                              1












                              1








                              1






                              As of 18.10 (cannot check in earlier versions) VPN connections in NetworkManager have a setting vpn.persistent which does just that: reconnects to a VPN on connection loss until you disconnect manually. It is set to "no" by default and unfortunately isn't exposed neither in Gnome Network Settings nor in nm-connection-editor.



                              But you can set it through a CLI like this:



                              nmcli connection modify <Your VPN connection name> vpn.persistent yes



                              The connection must exist before you do that, of course






                              share|improve this answer












                              As of 18.10 (cannot check in earlier versions) VPN connections in NetworkManager have a setting vpn.persistent which does just that: reconnects to a VPN on connection loss until you disconnect manually. It is set to "no" by default and unfortunately isn't exposed neither in Gnome Network Settings nor in nm-connection-editor.



                              But you can set it through a CLI like this:



                              nmcli connection modify <Your VPN connection name> vpn.persistent yes



                              The connection must exist before you do that, of course







                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Dec 20 at 10:37









                              m0NKey bR4in

                              312




                              312























                                  0














                                  After claiming it was a SMOP (Simple Matter of Programming), I wrote a bash script that monitors for "Link Down", then executes a user script. Less CPU usage, more responsive than the while true....sleep 30 method. See my answer at here. It's about "rotating WiFi connections", but will probably work for you, too






                                  share|improve this answer




























                                    0














                                    After claiming it was a SMOP (Simple Matter of Programming), I wrote a bash script that monitors for "Link Down", then executes a user script. Less CPU usage, more responsive than the while true....sleep 30 method. See my answer at here. It's about "rotating WiFi connections", but will probably work for you, too






                                    share|improve this answer


























                                      0












                                      0








                                      0






                                      After claiming it was a SMOP (Simple Matter of Programming), I wrote a bash script that monitors for "Link Down", then executes a user script. Less CPU usage, more responsive than the while true....sleep 30 method. See my answer at here. It's about "rotating WiFi connections", but will probably work for you, too






                                      share|improve this answer














                                      After claiming it was a SMOP (Simple Matter of Programming), I wrote a bash script that monitors for "Link Down", then executes a user script. Less CPU usage, more responsive than the while true....sleep 30 method. See my answer at here. It's about "rotating WiFi connections", but will probably work for you, too







                                      share|improve this answer














                                      share|improve this answer



                                      share|improve this answer








                                      edited Apr 13 '17 at 12:24









                                      Community

                                      1




                                      1










                                      answered Sep 28 '15 at 22:48









                                      waltinator

                                      21.9k74169




                                      21.9k74169























                                          0














                                          this script will work on 16.04 where nmcli con status no longer works:



                                          #!/bin/bash
                                          CON="purple"
                                          STATUS=`nmcli con show --active | grep purple | cut -f1 -d " "`
                                          if [ -z "$STATUS" ]; then
                                          nmcli con up $CON
                                          fi





                                          share|improve this answer























                                          • A bit shorter STATUS="$(nmcli con show -f name | grep purple)". Or you can simply check if the actual connection is active by doing nmcli con show --active id 'purple'
                                            – smac89
                                            May 29 '17 at 2:38


















                                          0














                                          this script will work on 16.04 where nmcli con status no longer works:



                                          #!/bin/bash
                                          CON="purple"
                                          STATUS=`nmcli con show --active | grep purple | cut -f1 -d " "`
                                          if [ -z "$STATUS" ]; then
                                          nmcli con up $CON
                                          fi





                                          share|improve this answer























                                          • A bit shorter STATUS="$(nmcli con show -f name | grep purple)". Or you can simply check if the actual connection is active by doing nmcli con show --active id 'purple'
                                            – smac89
                                            May 29 '17 at 2:38
















                                          0












                                          0








                                          0






                                          this script will work on 16.04 where nmcli con status no longer works:



                                          #!/bin/bash
                                          CON="purple"
                                          STATUS=`nmcli con show --active | grep purple | cut -f1 -d " "`
                                          if [ -z "$STATUS" ]; then
                                          nmcli con up $CON
                                          fi





                                          share|improve this answer














                                          this script will work on 16.04 where nmcli con status no longer works:



                                          #!/bin/bash
                                          CON="purple"
                                          STATUS=`nmcli con show --active | grep purple | cut -f1 -d " "`
                                          if [ -z "$STATUS" ]; then
                                          nmcli con up $CON
                                          fi






                                          share|improve this answer














                                          share|improve this answer



                                          share|improve this answer








                                          edited Nov 24 '17 at 8:16









                                          derHugo

                                          2,28021429




                                          2,28021429










                                          answered Mar 15 '17 at 6:04









                                          george

                                          1




                                          1












                                          • A bit shorter STATUS="$(nmcli con show -f name | grep purple)". Or you can simply check if the actual connection is active by doing nmcli con show --active id 'purple'
                                            – smac89
                                            May 29 '17 at 2:38




















                                          • A bit shorter STATUS="$(nmcli con show -f name | grep purple)". Or you can simply check if the actual connection is active by doing nmcli con show --active id 'purple'
                                            – smac89
                                            May 29 '17 at 2:38


















                                          A bit shorter STATUS="$(nmcli con show -f name | grep purple)". Or you can simply check if the actual connection is active by doing nmcli con show --active id 'purple'
                                          – smac89
                                          May 29 '17 at 2:38






                                          A bit shorter STATUS="$(nmcli con show -f name | grep purple)". Or you can simply check if the actual connection is active by doing nmcli con show --active id 'purple'
                                          – smac89
                                          May 29 '17 at 2:38













                                          0














                                          I think the complete answer based on other answers goes as follow :



                                          #!/bin/bash +x
                                          while [ "true" ]
                                          do
                                          CON="Your-VPN-Name"
                                          STATUS=`nmcli con show --active | grep $CON | cut -f1 -d " "`
                                          if [ -z "$STATUS" ]; then
                                          echo "Disconnected, trying to reconnect..."
                                          (sleep 1s && nmcli con up $CON)
                                          else
                                          echo "Already connected !"
                                          fi
                                          sleep 30
                                          done





                                          share|improve this answer




























                                            0














                                            I think the complete answer based on other answers goes as follow :



                                            #!/bin/bash +x
                                            while [ "true" ]
                                            do
                                            CON="Your-VPN-Name"
                                            STATUS=`nmcli con show --active | grep $CON | cut -f1 -d " "`
                                            if [ -z "$STATUS" ]; then
                                            echo "Disconnected, trying to reconnect..."
                                            (sleep 1s && nmcli con up $CON)
                                            else
                                            echo "Already connected !"
                                            fi
                                            sleep 30
                                            done





                                            share|improve this answer


























                                              0












                                              0








                                              0






                                              I think the complete answer based on other answers goes as follow :



                                              #!/bin/bash +x
                                              while [ "true" ]
                                              do
                                              CON="Your-VPN-Name"
                                              STATUS=`nmcli con show --active | grep $CON | cut -f1 -d " "`
                                              if [ -z "$STATUS" ]; then
                                              echo "Disconnected, trying to reconnect..."
                                              (sleep 1s && nmcli con up $CON)
                                              else
                                              echo "Already connected !"
                                              fi
                                              sleep 30
                                              done





                                              share|improve this answer














                                              I think the complete answer based on other answers goes as follow :



                                              #!/bin/bash +x
                                              while [ "true" ]
                                              do
                                              CON="Your-VPN-Name"
                                              STATUS=`nmcli con show --active | grep $CON | cut -f1 -d " "`
                                              if [ -z "$STATUS" ]; then
                                              echo "Disconnected, trying to reconnect..."
                                              (sleep 1s && nmcli con up $CON)
                                              else
                                              echo "Already connected !"
                                              fi
                                              sleep 30
                                              done






                                              share|improve this answer














                                              share|improve this answer



                                              share|improve this answer








                                              edited May 13 at 15:50

























                                              answered May 13 at 15:38









                                              ali mohebbi

                                              11




                                              11






























                                                  draft saved

                                                  draft discarded




















































                                                  Thanks for contributing an answer to Ask Ubuntu!


                                                  • Please be sure to answer the question. Provide details and share your research!

                                                  But avoid



                                                  • Asking for help, clarification, or responding to other answers.

                                                  • Making statements based on opinion; back them up with references or personal experience.


                                                  To learn more, see our tips on writing great answers.





                                                  Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                                  Please pay close attention to the following guidance:


                                                  • Please be sure to answer the question. Provide details and share your research!

                                                  But avoid



                                                  • Asking for help, clarification, or responding to other answers.

                                                  • Making statements based on opinion; back them up with references or personal experience.


                                                  To learn more, see our tips on writing great answers.




                                                  draft saved


                                                  draft discarded














                                                  StackExchange.ready(
                                                  function () {
                                                  StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f679059%2fauto-reconnect-to-vpn-on-disconnect%23new-answer', 'question_page');
                                                  }
                                                  );

                                                  Post as a guest















                                                  Required, but never shown





















































                                                  Required, but never shown














                                                  Required, but never shown












                                                  Required, but never shown







                                                  Required, but never shown

































                                                  Required, but never shown














                                                  Required, but never shown












                                                  Required, but never shown







                                                  Required, but never shown







                                                  Popular posts from this blog

                                                  How did Captain America manage to do this?

                                                  迪纳利

                                                  南乌拉尔铁路局