89

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, 2013 at 13:59
  • Which one would it be? eth0, lo, tun2, or wlan0
    – Adam
    May 23, 2013 at 14:13
  • simply do > sudo /etc/init.d/network-manager restart <
    – Qasim
    May 23, 2013 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, 2014 at 12:49

25 Answers 25

83

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

sudo killall openvpn
7
  • 1
    Might require sudo apt-get install psmisc on some builds
    – geotheory
    Oct 29, 2015 at 22:51
  • I had to kill -9 it on Ubuntu 16.04 (yes I know...)
    – Gregor
    Apr 5, 2019 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, 2020 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: community.openvpn.net/openvpn/wiki/OpenVPN3Linux
    – Spencer D
    Dec 14, 2020 at 5: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 7, 2022 at 12:04
46

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
  • 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, 2021 at 10:28
  • 2
    This is indeed the correct answer, and thanks for writing those bash files - makes life much easier!
    – jonsedar
    Nov 17, 2021 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.
5
  • 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, 2015 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, 2018 at 19:44
  • Just for reference: "9" ist SIGKILL and "15" is SIGTERM - see kill -L
    – Gerd
    Jan 23, 2020 at 11:10
  • how do I selectively close certain vpn connections? Jan 23, 2020 at 12:45
  • not tested, but you can use kill -9 $(pidof <processname>)
    – funder7
    Mar 31, 2022 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 
1
  • 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, 2022 at 14:55
10

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

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, 2015 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, 2015 at 0:04
  • 1
    What if teh terminal was closed accidentally. ?
    – varun
    Apr 21, 2016 at 11:43
  • 1
    @Dennis ifconfig tun0 down helps in this case.
    – gnysek
    Sep 20, 2018 at 16:04
7

Try this

killall -SIGINT openvpn

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

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, 2013 at 13:57
  • 1
    sudo killall openvpn in a new terminal worked for me.
    – Adam
    May 24, 2013 at 18:09
  • @Adam: it's SIGINT, not SICINT Jul 24, 2017 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, 2018 at 19:47
6

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

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

0
5

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
3

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-)
2

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

sudo ifconfig tun0 down
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
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
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

1

Quick one-liner:

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

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.
1

an a comment to the answer from @allgamer :

for openvpn3,

the command is:

$ sudo killall openvpn3-servic

tested on Ubuntu 20.04

2
  • 1
    Then this is not another answer it is a comment to someone else answer.
    – David
    Sep 15, 2022 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. Sep 30, 2022 at 7: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.

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.
1

I am slightly late here but to improve previous answers, I have added following as an allias

alias ovpn-up='sudo openvpn3 session-start --config /etc/openvpn/openvpn.ovpn'
alias ovpn-ls='sudo openvpn3 sessions-list'
alias ovpn-down='sudo openvpn3 session-manage --session-path $(sudo openvpn3 sessions-list | grep Path | cut -d':' -f2) --disconnect'

so I can quickly connect/disconnect and list the sessions.

0

For beginner don't go for a complicated process, just use Task Manager, search for openvpn and terminate it directly from there

given image to see

0

Improving on @nat-naydenova's answer, it happened that just a grep on openvpn was not enough as the shell script itself had openvpn in the name.

Also, for some reason, using kill -9 also killed my virtual internet connection in Debian. Yet, kill -15 worked just fine.

So here is the proposal:

echo "Looking for PID of openvpn..."
#1. looking for /usr/sbin/openvpn to avoid confusion with other files
#2. extracting PID with sed, since we get all the info (as opposed to lsof -i)
#3. removing extra spaces with tr

pid=$(lsof -w | grep /usr/sbin/openvpn | sed -n 's/openvpn\(.*\)root.*/\1/p' | tr -d " ")


echo "Found $pid"
echo "Attempting to kill PID: $pid"

#Uncomment if you think this is really required. I don't think so
##Stopping openvpn service
#service openvpn stop

#Killing softly
kill -15 $pid

#Uncomment if you think this is really required. I don't think so
##Restarting openvpn service
#service openvpn start

echo "Done killing openvpn."

Acknowledgements:

  • Thanks ChatGPT for the help with the command chaining with grep + sed + tr.
  • Thanks to people writing those f***ng manuals ;)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .