Answers:
pkill -SIGTERM -f'openvpn --daemon --conf $ OPENVPNCONFFILE'
pkill命令允许您根据名称或其他属性来发出信号
这会将SIGTERM发送到openvpn,从而使其正常退出并关闭tun接口。您可能/将需要在-f之后修改该部分,以匹配您启动openvpn连接的方式。
我在openvpn手册页的Signals部分中找到了这一点。
SIGINT, SIGTERM
Causes OpenVPN to exit gracefully.
使用以下命令确定虚拟接口ifconfig
:
tap0 Link encap:Ethernet HWaddr 32:28:a4:04:34:cc
inet addr:172.22.18.14 Bcast:172.22.18.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:100
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
并使用以下命令关闭它:
sudo ifconfig tap0 down
这是我为基于RedHat编写的初始化脚本:
#! /bin/bash
#
# openvpn-client Start/Stop the openvpn client
#
# chkconfig: 2345 90 60
# description: start openvpn client at boot
# processname: openvpn
# Source function library.
. /etc/init.d/functions
daemon="openvpn"
prog="openvpn-client"
conf_file="/vagrant/vpn/client-dept18-payment.ovpn"
start() {
echo -n $"Starting $prog: "
if [ -e /var/lock/subsys/openvpn-client ] && [ $(pgrep -fl "openvpn --config /vagrant/vpn/client-dept18-payment.ovpn" | wc -l) -gt 0 ]; then
echo_failure
echo
exit 1
fi
runuser -l root -c "$daemon --config $conf_file >/dev/null 2>&1 &" && echo_success || echo_failure
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && touch /var/lock/subsys/openvpn-client;
return $RETVAL
}
stop() {
echo -n $"Stopping $prog: "
pid=$(ps -ef | grep "[o]penvpn --config $conf_file" | awk '{ print $2 }')
kill $pid > /dev/null 2>&1 && echo_success || echo_failure
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/openvpn-client;
return $RETVAL
}
status() {
pgrep -fl "openvpn --config /vagrant/vpn/client-dept18-payment.ovpn" >/dev/null 2>&1
RETVAL=$?
if [ $RETVAL -eq 0 ]; then
pid=$(ps -ef | grep "[o]penvpn --config $conf_file" | awk '{ print $2 }')
echo $"$prog (pid $pid) is running..."
else
echo $"$prog is stopped"
fi
}
restart() {
stop
start
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
status)
status
;;
condrestart)
[ -f /var/lock/subsys/openvpn-client ] && restart || :
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart}"
exit 1
esac
那么您可以照常使用它:
# /etc/init.d/openvpn-client start
Starting openvpn-client: [ OK ]
# /etc/init.d/openvpn-client start
Starting openvpn-client: [FAILED]
# /etc/init.d/openvpn-client status
openvpn-client (pid 5369) is running...
# /etc/init.d/openvpn-client stop
Stopping openvpn-client: [ OK ]
# /etc/init.d/openvpn-client stop
Stopping openvpn-client: [FAILED]
# /etc/init.d/openvpn-client status
openvpn-client is stopped
# /etc/init.d/openvpn-client restart
Stopping openvpn-client: [ OK ]
Starting openvpn-client: [ OK ]
# /etc/init.d/openvpn-client status
openvpn-client (pid 5549) is running...
只是以为我会用更完整的答案来更新我的评论(考虑到我不知道自由半径,这可能不相关)。
我一直在使用Debian Linux发行版并安装了openvpn软件包。Debian中的客户端配置可以通过命令行启动,这导致了这个问题,似乎没有一种整洁的方式来终止/管理连接...
我今天了解到,尽管有一个/etc/init.d/openvpn脚本在启动时运行,并且如果我将openvpn配置文件放在/ etc / openvpn /中(文件扩展名必须为.conf),则可以控制连接通过使用/etc/init.d/openvpn stop和etc / init.d / openvpn start(或“ service openvpn stop”)。
将配置文件放在/ etc / openvpn /中也会导致VPN隧道在启动时自动启动。断开连接后,它也会自动重新连接。
sudo pkill openvpn