84

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.

Adam
  • 1,305
  • 2
  • 15
  • 22

22 Answers22

80

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

sudo killall openvpn
AllGamer
  • 1,167
  • 7
  • 9
  • 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 05 '19 at 08:09
  • 1
    this just kills every openvpn process. I have several and want to close just some. Is there a better way? – johannes_lalala Jan 23 '20 at 12:44
  • 4
    @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: https://community.openvpn.net/openvpn/wiki/OpenVPN3Linux – Spencer D Dec 14 '20 at 05:13
  • @SpencerD I tried it and got `If 'openvpn3' is not a typo you can use command-not-found to lookup the package that contains it, like this: cnf openvpn3`. I also tried `openvpn session-manage --config "$CONFIGURATION_PROFILE_NAME" --disconnect` (without the 3) instead, but the parameters seem not fitting: `Options error: I'm trying to parse "session-manage" as an --option parameter but I don't see a leading '--' Use --help for more information. ` – Cadoiz Mar 07 '22 at 12:04
  • @johannes_lalala You can use `ps` or `lsof -i` to find all processes and `grep` it for `openvn` [as suggested here](https://askubuntu.com/a/558797/830570). After you have the Process-ID, you can use `kill -9 ` to kill just one process. – Cadoiz Mar 08 '22 at 06:29
  • @Cadoiz, it sounds like you have a different version of OpenVPN installed than the version I referenced in my comment. – Spencer D Mar 26 '22 at 18:51
40

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.

Bazer Con
  • 7
  • 2
Shmalex
  • 510
  • 4
  • 7
  • 8
    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 '21 at 10:28
  • 2
    This is indeed the correct answer, and thanks for writing those bash files - makes life much easier! – jonsedar Nov 17 '21 at 10:27
21

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.
Cadoiz
  • 268
  • 1
  • 9
Nat Naydenova
  • 331
  • 3
  • 6
  • 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. – Sopalajo de Arrierez Jul 27 '15 at 22:21
  • 1
    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? – johannes_lalala Jan 23 '20 at 12:45
  • not tested, but you can use `kill -9 $(pidof )` – funder7 Mar 31 '22 at 14:31
12

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 
teliaz
  • 121
  • 1
  • 2
  • 1
    Well, this is nice! I've integrated the first command as External tool into phpstorm. Btw I would have used the script if I had more active sessions! I'll keep it, just in case .. Nice work! :-) – funder7 Mar 31 '22 at 14:55
9

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
yerlilbilgin
  • 191
  • 2
  • 3
7

Try this

killall -SIGINT openvpn

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

cocomac
  • 3,043
  • 3
  • 16
  • 49
kuchi
  • 171
  • 2
  • `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` – Dzamo Norton 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
7

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

Andrea Corbellini
  • 15,616
  • 2
  • 64
  • 81
Valkenier
  • 71
  • 1
  • 1
5

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

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

FloT
  • 2,256
  • 4
  • 13
  • 29
Ruchira
  • 51
  • 1
  • 3
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
Hamid Siaban
  • 141
  • 4
2

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

sudo ifconfig tun0 down
BeastOfCaerbannog
  • 12,964
  • 10
  • 49
  • 77
Sayed Sohan
  • 193
  • 1
  • 2
  • 8
2

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
Devendra
  • 131
  • 5
2

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
Prajna
  • 121
  • 2
2

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-)
JeanLescure
  • 196
  • 1
  • 5
1

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

Harindaka
  • 111
  • 2
1

Quick one-liner:

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

openvpn3 sessions-list

It will print a PID number

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

1

For me works this:

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

You can just send SIGINT signal to openvpn and it will stop gracefully.

https://openvpn.net/community-resources/controlling-a-running-openvpn-process/

Running on Linux/BSD/Unix

OpenVPN accepts several signals:

SIGUSR1 -- Conditional restart, designed to restart without root privileges
SIGHUP -- Hard restart
SIGUSR2 -- Output connection statistics to log file or syslog
SIGTERM, SIGINT -- Exit

Here another info from openvpn manual:

SIGNALS SIGHUP Cause OpenVPN to close all TUN/TAP and network connections, restart, re-read the configuration file (if any), and reopen TUN/TAP and network connections.

   SIGUSR1
          Like SIGHUP`, except don't re-read configuration file, and possibly don't close and reopen TUN/TAP device, re-read key files,

preserve local IP address/port, or preserve most recently authenticated remote IP address/port based on --persist-tun, --persist-key, --persist-local-ip and --persist-remote-ip options respectively (see above).

          This signal may also be internally generated by a timeout condition, governed by the --ping-restart option.

          This signal, when combined with --persist-remote-ip, may be sent when the underlying parameters of the host's network interface

change such as when the host is a DHCP client and is assigned a new IP address. See --ipchange for more information.

   SIGUSR2
          Causes OpenVPN to display its current statistics (to the syslog file if --daemon is used, or stdout otherwise).

   SIGINT, SIGTERM
          Causes OpenVPN to exit gracefully.
dyedfox
  • 333
  • 1
  • 3
  • 11
1

an a comment to the answer from @allgamer :

for openvpn3,

the command is:

$ sudo killall openvpn3-servic

tested on Ubuntu 20.04

  • 1
    Then this is not another answer it is a comment to someone else answer. – David Sep 15 '22 at 14:27
  • very well pointed out @David , unfortunately i have too little reputation to comment on stackexchange, but enough to post an answer. if you can paste a comment, i will gladly delete this answer of mine. – whenthoughsmatch Sep 30 '22 at 07:59
1

For me this works:

in the terminal,

openvpn3 sessions-list

Here you can see your active sessions, you can see its Config Name (usually its xxxxx.ovpn)

then you can type :

openvpn3 session-manage --disconnect --config ${session_config_name}

change ${session_config_name} with your config name that you listed previously. Then you'll get disconnected.

anonymous2
  • 4,268
  • 7
  • 33
  • 61
OmBrewok
  • 11
  • 1
1
openvpn3 session-manage --disconnect --interface tun0

(where tun0 is the name of the VPN tunnel you see in ip route show)


Explanation:

Just killing it (even politely with sigterm: sudo pkill openvpn3) does not fully clean up the routing table (ip route show):

 default via 192.168.0.1 dev wlp58s0 proto dhcp src 192.168.0.100 metric 600 
+<your employer's IP address> via 192.168.0.1 dev wlp58s0 
 192.168.0.0/24 dev wlp58s0 proto kernel scope link src 192.168.0.100 metric 600

What does is --disconnect.

As for what argument to identify the connection:

  • --config does not work for me:
    openvpn3 session-manage --disconnect --config ~/Downloads/Openvpn3\ Profile.ovpn 
    session-manage: ** ERROR ** No sessions started with the configuration profile name was found
    
  • --path changes each time (you could script around that).
  • --interface is stable in my case.
user2394284
  • 271
  • 1
  • 4
1
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 < >

Bazer Con
  • 7
  • 2
MasterCATZ
  • 21
  • 2