http://www.pronix.de -> Tutorials -> OpenVPN Tutorial -> OpenVPN Serverinstallation -> Automatischer Start des Servers

Automatischer Start des OpenVPN Servers beim Booten

Wir wollen natürlich OpenVPN nicht nach dem Booten des Servers manuell starten und fügen deshalb die folgenden Variablen in /etc/rc.config ein.

# Openvpn
openvpn_enable="YES"
openvpn_if="tun tap"
openvpn_configfile="/usr/local/etc/openvpn/server.conf"
openvpn_dir="/usr/local/etc/openvpn"

Falls Sie eine ältere Version von OpenVPN nutzen, können Sie alternativ auch ein Startscript erstellen und dies unter /usr/local/etc/rc.d/openvpn.sh speichern.
In der OpenVPN Distribution ist ein Beispiel für RedHat enthalten, dass wir für unseren FreeBSD Server anpassen.

#!/bin/sh

# Unser OpenVPN binary
openvpn="/usr/local/sbin/openvpn"

# OpenVPN Verzeichnis
work="/usr/local/etc/openvpn"

# Lockfile
lock="openvpn.lock"

# PID Verzeichnis
piddir="/var/run"

#########################
#      Here we go       #
#########################

lock="$work/$lock"
if ! [ -f  $openvpn ]; then
   echo "Error: OpenVPN binary not found"
   exit 0;
fi


# Unsere Optionen, die wir dem Startscript mitgeben koennen

case "$1" in
   start)
      if ! `/sbin/kldstat | grep -q if_tap\.ko`; then
         if ! `/sbin/kldload if_tap`; then
            echo "Error: Can't load kernel module if_tap"
            exit 0;
         fi
      fi
      if [ -e $lock ]; then
         echo "OpenVPN is already running. Trying to kill..."
         for pidf in `/bin/ls $piddir/openvpn*.pid 2>/dev/null`; do
            if [ -s $pidf ]; then
               kill `cat $pidf` >/dev/null 2>&1
            fi
            rm -f $pidf
         done
         rm -f $lock
         sleep 3
      fi
      rm -f $piddir/openvpn*.pid

      cd $work
      success=0
      for c in `/bin/ls *.conf 2>/dev/null`; do
         bn=${c%%.conf}
         rm -f $piddir/$bn.pid
         $openvpn --daemon --writepid $piddir/openvpn$bn.pid --config $c --cd $work
         if [ $? -eq 0 ]; then
            echo "OpenVPN: Startet with configuration $c"
            success=1
         fi
      done
      if [ $success -eq 1 ]; then
         touch $lock
      fi
      ;;
   stop)
      for pidf in `/bin/ls $piddir/openvpn*.pid 2>/dev/null`; do
         if [ -s $pidf ]; then
            kill `cat $pidf` >/dev/null 2>&1
         fi
         rm -f $pidf
      done
      rm -f $lock
      if `/sbin/kldstat | grep -q if_tap\.ko`; then
         if ! `/sbin/kldunload if_tap`; then
            echo "Error: Can't unload kernel module if_tap"
            exit 0;
         fi
      fi
      echo "Stopped OpenVPN"
      ;;
   restart)
      $0 stop
      sleep 3
      $0 start
      ;;
   reload)
      if [ -f $lock ]; then
         for pidf in `/bin/ls $piddir/openvpn*.pid 2>/dev/null`; do
            if [ -s $pidf ]; then
               kill -USR1 `cat $pidf` >/dev/null 2>&1
            fi
         done
      fi
      ;;
   *)
      echo "Usage: $0 {start|stop|restart|reload}"
      exit 1
      ;;
esac
exit 0
Weiter mit OpenVPN Clientinstallation