I am going to connect to a VPN using openconnect on CEntOS 7 terminal. I only have one terminal because I am on a SSH session. I need to connect to the VPN using openconnect. I do so like this:

openconnect -u username us.myprovider.net

I need to run the VPN in the background and then do other things in the foreground. Currently, I start the VPN, I press Ctrl + Z and then press bg to send it to the background. But, this seems to close the VPN connection. How can I do that?


According to the Openconnect documentation, the option you would want to try would be:


Continue in background after startup

Yes. -b is the option. But that just leads to the next issue. A clean disconnect then is not possible anymore. I mean, the only way to exit openconnect when it is in background is to kill the process. But that causes a name service problem. All dns requests are still sent to the name servers that were learned during the vpn establishment process, but they are not reachable anymore after disconnect. Only workaround is to shutdown the whole interface and take it up again.

  • Can you expand on "shutdown the whole interface and take it up again." I did not realize what this problem was for months and have usually be rebooting the machine when I am finished with a VPN connection. – Kevin May 7 at 21:06
  • My hack to fix the DNS problem on Ubuntu is to sudo sed -i --follow-symlinks '/10\.25\./d' /etc/resolv.conf && sudo service systemd-resolved restart after killing openconnect, where 10.25... should match your DNS servers, of course – Miguel May 11 at 17:36

To connect:

sudo openconnect --background <...>

To disconnect:

sudo killall -SIGINT openconnect

Using SIGINT gives the background process time to clean up the DNS configuration and exit gracefully.


Finally, I came up with this solution: Run openvpn command to connect to server. Then, press CTRL+Z to stop it and then type bg to make the stopped program continue in the background. To continue running the program in foreground, you have to type fg and then you can disconnect VPN by pressing CTRL+C.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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