How to stop vpnc IPSec VPNs and close the tunnel interface

Problem:

vpnc_start.sh
$ sudo vpnc my-vpn.conf
VPNC started in background (pid: 21763)...

but you can’t find any information on how to stop vpnc i.e. terminating the VPN connection.

Solution

Preferred method: Use vpnc-disconnect

Run

example.sh
sudo vpnc-disconnect

This will, for example, print

vpnc_terminating.txt
Terminating vpnc daemon (pid: 21763)

vpnc-disconnect is the official method of stopping vpnc and will terminate the vpnc instance whose PID is written in /var/run/vpnc.pid. In other words, it will not work properly if you have multiple vpnc instances running at the same time, or if you have specified an alternate PID file for vpnc (e.g. using vpnc --pid-file /var/run/my-vpnc.pid my-vpn.conf).

Alternate method 1: Stop all vpnc instances on the current machine

You can kill all vpnc instances on the current machine using

example.sh
sudo killall vpnc
vpnc_disconnect.sh
sudo vpnc-disconnect

vpnc tells you its process ID when starting it. In our example above:

example.txt
VPNC started in background (pid: 21763)...

the PID is 21763 so we can kill the process using

example.sh
sudo kill 21763

This will cleanly stop vpnc and remove the tunnel interface.

Alternate method 3: Kill a specific vpnc (if you don’t know it’s PID)

Show all running vpnc instances using

example.sh
pgrep -a vpnc

This will show you, for example,

example.txt
21763 vpnc my-vpn.conf
30792 vpnc other-vpn.conf

In that list, find the line with the vpnc instance you want to kill (you can identify it by the config file name, e.g. my-vpn.conf - in this example, it would be the first line).

The number at the beginning of the line is the PID of that vpnc process. Copy it and run

pgrep_vpnc_example.txt
21763 vpnc my-vpn.conf
30792 vpnc other-vpn.conf

e.g.

example.sh
sudo kill 21763

just like in Alternate method 2. This will only stop that specific vpnc instance and leave all others running.


Check out similar posts by category: Networking