Velvet Star Monitor

Standout celebrity highlights with iconic style.

general

How to disconnect from OpenVPN?

Writer Matthew Martinez

I'm connecting using VPNBook servers and it works fine with this command:

 sudo openvpn --config /etc/openvpn/vpnbook-udp-53.ovpn --auth-user-pass /etc/openvpn/password.txt

but I just can't seem to figure out how to stop it without a reboot.

I've tried service openvpn stop and /etc/init.d/vpnbook stop, but that doesn't seem to affect it.

4

18 Answers

This command definitely works for me, and it should work for you too.

sudo killall openvpn
7

I had same problem with disconnecting from openvpn3

I end up creating this small repo that helps manage the openvpn3 sessions

To disconnect the session, you have know the session's Path

openvpn3 session-manage --session-path $OPENVPN3_SESSION_PATH --disconnect

the session path could be found via

openvpn3 sessions-list
> -----------------------------------------------------------------------------
> Path: /net/openvpn/v3/sessions/7a42f37asc8d9s424c8b534sd331d6dd56e8
> Created: Tue Dec 8 10:44:57 2020 PID: 9495
> Owner: shmalex Device: tun0
> Config name: client.ovpn (Config not available)
> Session name: ***.***.***.***
> Status: Connection, Client connected
> -----------------------------------------------------------------------------
OPENVPN3_SESSION_PATH=/net/openvpn/v3/sessions/7a42f37asc8d9s424c8b534sd331d6dd56e8
openvpn3 session-manage --session-path $OPENVPN3_SESSION_PATH --disconnect

You can use my repo to perform same actions with help of bash files.

2

The successful steps in my case were:

# stop the service
$ sudo /etc/init.d/openvpn stop
# find the process if it is still running for some reason
$ lsof -i | grep openvpn
# kill the proccess(s) by its PID
$ kill -9 <PID>
# if necessary restart the service again
$ sudo /etc/init.d/openvpn start

For some reason killall -SIGINT openvpn did not work for me, but the steps above did. 5

In case sudo killall openvpn does not finish the job (I experienced it a few times) then a sharp and fatal solution would be:

pgrep openvpn | xargs sudo kill -9
1

Just hit CTRL+C in the terminal you just started OpenVPN.

4

I stumbled upon having 2 open sessions with the same config path. So I could not use

openvpn3 session-manage --disconect --config <config_path>

session-manage: ** ERROR ** More than one session with the given configuration profile name was found.

So I made a script to loop through sessions (session ids are not always the same as the config paths)

ACTIVE_SESSIONS=$(openvpn3 sessions-list | grep -i 'path' | awk '{p=index($0, ":");print $2}')
echo $ACTIVE_SESSIONS
for instance in $ACTIVE_SESSIONS; do openvpn3 session-manage --disconnect --session-path ${instance}
done 
1

Try this

killall -SIGINT openvpn

You can get more info on the different signals you can send here.

4

after running sudo killall openvpn or service openvpn stop the virtual interface "tun0" would remain opened and referenced in route table, so actually related connections would be lost since openvpn service is killed.

the solution is to delete this virtual connection after killing openvpn service, as it is created everytime when openvpn service gets connected.

so you need to run below commands for disconnecting openvpn:

$ sudo killall openvpn
$ sudo ip link delete tun0

sudo openvpn3 session-manage --disconnect --config $'client'.ovpn

Replace client with the corresponding name.
This will shutdown the session.

0

Use the following command, where 0 is the tunnel number:

sudo ifconfig tun0 down

Use the following command:

 $openvpn3 session-manage --session-path /net/openvpn/v3/sessions/..... --disconnect

you may get the path using command below:

openvpn3 sessions-list

You can use the following script to disconnect all vpn sessions or a specific vpn session

vpnd.sh [session path]

#!/bin/bash
set -e
session=$1
if [ "$1" = "--help" ]; then echo "Usage : ./vpnd.sh [session path]" echo "E.g. disconnect specific session" echo "vpnd.sh /net/openvpn/v3/sessions/b7a35c15s95ffs4cd9sa867sc473a37d77a0" echo "E.g. disconnect all sessions" echo "vpnd.sh" exit 1
fi
if [ ! -z "$session" ]; then openvpn3 session-manage --disconnect --session-path "${session}" exit 0
fi
readarray -t vpn_sessions < <(openvpn3 sessions-list | sed -nE 's/^\s*Path:\s+(\S*)$/\1/p')
for session in ${vpn_sessions[@]} ; do if [ ! -z "${session}" ]; then echo "Closing session ${session}..." openvpn3 session-manage --disconnect --session-path "${session}" fi
done
openvpn3 session-manage --cleanup
openvpn3 sessions-list

Note that you can get a list of active session paths via

openvpn3 sessions-list

Quick one-liner:

sudo openvpn3 sessions-list | grep -ioP '/net/openvpn/v3/sessions/\w+' | xargs -I{} sudo openvpn3 session-manage --path {} --disconnect

openvpn3 sessions-list

It will print a PID number

sudo kill -9 {PID} without the curly braces of course.

For me works this:

your@prompt: openvpn3 session-manage --disconnect --path </PATH/PROVIDED/IN/SESSION/START>

This worked for me.

When you login to ovpn. It will give a session file with full path. Put that full path after --session-path in below command

openvpn3 session-manage --session-path /net/openvpn/v3/sessions/<session file> --disconnect

Here's my one-liner that easily gets the session-path using grep and cut:

openvpn3 session-manage --disconnect --session-path $(openvpn3 sessions-list | grep Path | cut -b 15-)
sudo update-rc.d openvpn disable

Or edit the config file in /etc/default/openvpn with

sudo nano /etc/default/openvpn

And uncomment the line:

#AUTOSTART="none"

So it looks like:

AUTOSTART="none"

Then you'll have to run:

  • sudo service openvpn start <vpn-name>to manually start the VPN.

  • sudo service openvpn stop <vpn-name>to manually stop the VPN.

Where <vpn-name> is the config file name located in /etc/openvpn without the .conf extension and without the < >

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

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