11

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?

14

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.

2
  • 1
    According to man page, signals SIGINT, SIGTERM are interpreted the same way. So plain killall openconnect should do the same as killall -SIGINT openconnect.
    – ks1322
    Apr 6 '20 at 10:58
  • 1
    @ks1322 it appears the behaviour changed at some point. The man page you link to does describe SIGINT and SIGTERM as being the same, but the man page on Ubuntu 18.04 describes SIGINT as "performs a clean shutdown by logging the session off, disconnecting from the gateway, and running the vpnc-script to restore the network configuration." and SIGTERM as "exits immediately without logging off or running vpnc-script"
    – bain
    Aug 7 '20 at 9:14
10

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

-b,--background

Continue in background after startup
7

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 shut down the whole interface and take it up again.

3
  • 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 '19 at 21:06
  • 2
    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 '19 at 17:36
  • 1
    Use killall -SIGINT openconnect. From man openconnect: "SIGINT performs a clean shutdown by logging the session off, disconnecting from the gateway, and running the vpnc-script to restore the network configuration."
    – bain
    Aug 7 '20 at 9:04
1

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.

0

You can do this with screen or any other terminal multiplexer. With screen you can:

  1. start a new session with screen command
  2. connect to the VPN inside this new session openconnect -u username us.myprovider.net
  3. detach from this session with Ctrl+a d command and do other things in foreground

When you need to read openconnect messages or close VPN connection you can resume detached session with screen -r command.

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.