53

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
  • How exactly with ifconfig?
    – Adam
    May 23 '13 at 13:59
  • Which one would it be? eth0, lo, tun2, or wlan0
    – Adam
    May 23 '13 at 14:13
  • simply do > sudo /etc/init.d/network-manager restart <
    – Qasim
    May 23 '13 at 15:06
  • I am curious, when a VPN is started this way does it not show up in the network manager. I have only always used the network manager connect and disconnect and don't currently have a VPN to test but wondered.
    – Dennis
    Feb 19 '14 at 12:49

14 Answers 14

63

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

sudo killall openvpn
5
  • 1
    Might require sudo apt-get install psmisc on some builds
    – geotheory
    Oct 29 '15 at 22:51
  • I had to kill -9 it on Ubuntu 16.04 (yes I know...)
    – Gregor
    Apr 5 '19 at 8:09
  • 1
    this just kills every openvpn process. I have several and want to close just some. Is there a better way? Jan 23 '20 at 12:44
  • 1
    @johannes_lalala, you probably already figured this out, but this worked on my side: openvpn3 session-manage --config "$CONFIGURATION_PROFILE_NAME" --disconnect Hopefully that will help someone out. BTW, I got the command from this wiki page: community.openvpn.net/openvpn/wiki/OpenVPN3Linux
    – Spencer D
    Dec 14 '20 at 5:13
  • The problem is that you will kill all processes and all openvpn connections.
    – Shmalex
    Jul 28 at 4:08
15

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.
4
  • Well, this is the best answer in my opinion. Killing process is the weird method, but requesting the service to stop should do things as it must. Jul 27 '15 at 22:21
  • In general, you shouldn't "kill -9" things until you've tried an interrupt or otherwise cleanly existing it first. Programs can catch an interrupt and do cleanup, but can't catch signal nine (term). Particularly in the case of openvpn, killing it with -9 does not allow the post scripts to run, and very likely will leave now-invalid routes laying around. Ideally, you'd kill -SIGINT, then wait a few seconds for the pid to end, and only go with -SIGTERM / -9 if it didn't exit before that.
    – dannysauer
    Aug 16 '18 at 19:44
  • Just for reference: "9" ist SIGKILL and "15" is SIGTERM - see kill -L
    – Gerd
    Jan 23 '20 at 11:10
  • how do I selectively close certain vpn connections? Jan 23 '20 at 12:45
11

I had same problem with disconnecting from openvpn3

I end up creating this small repo https://github.com/shmalex/openvpn3_manager 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.

1
  • 1
    This is THE CORRECT answer, and should be the accepted one too. I get, however, why the answers with kill command are so upvoted, I wouldn't blame them or the voters, I wish the disconnect command was really practically 'a' command.
    – 0xc0de
    Jul 27 at 10:28
5

Try this

killall -SIGINT openvpn

You can get more info on different signals you can send here: http://openvpn.net/index.php/open-source/documentation/howto.html#control

4
  • killall -SIGINT openvpn openvpn(15360): Operation not permitted openvpn: no process found sudo killall -SICINT openvpn SICINT: unknown signal; killall -l lists signals.
    – Adam
    May 23 '13 at 13:57
  • 1
    sudo killall openvpn in a new terminal worked for me.
    – Adam
    May 24 '13 at 18:09
  • @Adam: it's SIGINT, not SICINT Jul 24 '17 at 10:23
  • The kill and killall commands send SIGTERM by default, which the documentation says has the same effect as SIGINT. So, either would work equivalently - if spelled properly. ;)
    – dannysauer
    Aug 16 '18 at 19:47
5

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

4
  • 2
    What if it was started w/the -daemon (background) flag? It's not possible in this case.
    – mr-sk
    Sep 25 '15 at 22:40
  • 1
    Also this leaves 'tun0' as an interface, so it's not possible to restart without rebooting or doing some system config file editing while running.
    – Dennis
    Oct 17 '15 at 0:04
  • 1
    What if teh terminal was closed accidentally. ?
    – varun
    Apr 21 '16 at 11:43
  • 1
    @Dennis ifconfig tun0 down helps in this case.
    – gnysek
    Sep 20 '18 at 16:04
4

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
  • 1
    sudo pkill openvpn whould do the job as well. May 8 '20 at 6:53
3

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
2

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

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

1
  • 1
    +1, works like a charm for me.
    – Piotr L
    Oct 28 '20 at 20:11
2

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 
0

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

sudo ifconfig tun0 down
0

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
0

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

0

Quick one-liner:

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

sudo update-rc.d openvpn disable

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

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.

< vpn-name > is the config file name without .conf extension

located in /etc/openvpn and without the < >

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.