OpenVPN em Linux
Neste artigo procurarei mostrar como se monta uma VPN - Virtual Private Network - no Linux, independente de distribuição, de uma maneira simples e didática usando o OpenVPN.
Parte 8: Conclusão
Com nossa VPN no ar irão aparecer outras dúvidas, mas deixo pra vocês agora os detalhes para a melhor forma de ajeitar a mesma.
Esse artigo foi montado com base em outros 2:
OpenVPN
Por Luiz Antonio Cassetari Vieira Filho
http://www.altoriopreto.com.br/artigos_3rd/artigo_vpn.php
OpenVPN no Debian (Stable) com cliente Windows 2000 ou XP
Por Kairo Araújo
http://www.kairo.eti.br/linux-notes-old/openvpn-client_win2k-xp.html
Só tenho a agradecer estas duas pessoas pelos excelentes artigos e que o meu tenha ficado o mais didático possível, para ajudar a todos vocês que estão nesse mundo maravilhoso que é o Linux, onde todos se ajudam mutuamente.
Obrigado e espero ter contribuído com mais um artigo legal.
OBS: Como havia prometido, segue abaixo o script para inicializar o openVPN, crie o arquivo dentro do diretório /etc/init.d (isso no Conectiva 10), em outras distribuições crie dentro do diretório onde ficam os arquivos inicializáveis.
# vi openvpn
Esse artigo foi montado com base em outros 2:
OpenVPN
Por Luiz Antonio Cassetari Vieira Filho
http://www.altoriopreto.com.br/artigos_3rd/artigo_vpn.php
OpenVPN no Debian (Stable) com cliente Windows 2000 ou XP
Por Kairo Araújo
http://www.kairo.eti.br/linux-notes-old/openvpn-client_win2k-xp.html
Só tenho a agradecer estas duas pessoas pelos excelentes artigos e que o meu tenha ficado o mais didático possível, para ajudar a todos vocês que estão nesse mundo maravilhoso que é o Linux, onde todos se ajudam mutuamente.
Obrigado e espero ter contribuído com mais um artigo legal.
OBS: Como havia prometido, segue abaixo o script para inicializar o openVPN, crie o arquivo dentro do diretório /etc/init.d (isso no Conectiva 10), em outras distribuições crie dentro do diretório onde ficam os arquivos inicializáveis.
# vi openvpn
#!/bin/sh
#
# openvpn This shell script takes care of starting and stopping
# openvpn on FreeBSD
#
# description: OpenVPN is a robust and highly flexible tunneling application that
# uses all of the encryption, authentication, and certification features
# of the OpenSSL library to securely tunnel IP networks over a single
# UDP port.
#
# Contributed to the OpenVPN project by
# Douglas Keller <doug at voidstar.dyndns.org>
# 2002.05.15
# FreeBSD version by Mikhail Levin <m_levin_99 at yahoo.com>
# 2005.01.20
# The init script does the following:
#
# - Starts an openvpn process for each .conf file it finds in
# /usr/local/etc/openvpn/config
#
# - If /usr/local/etc/openvpn/config/xxx.sh exists for a xxx.conf file then it executes
# it before starting openvpn (useful for doing openvpn --mktun...).
#
# - In addition to start/stop you can do:
#
# /usr/local/etc/rc.d/openvpn.sh reload - SIGHUP
# /usr/local/etc/rc.d/openvpn.sh reopen - SIGUSR1
# /usr/local/etc/rc.d/openvpn.sh status - SIGUSR2
# Modifications 2003.05.02
# * Changed == to = for sh compliance (Bishop Clark).
# * If condrestart|reload|reopen|status, check that we were
# actually started (James Yonan).
# * Added lock, piddir, and work variables (James Yonan).
# * If start is attempted twice, without an intervening stop, or
# if start is attempted when previous start was not properly
# shut down, then kill any previously started processes, before
# commencing new start operation (James Yonan).
# * Do a better job of flagging errors on start, and properly
# returning success or failure status to caller (James Yonan).
# Location of openvpn binary
openvpn="/usr/local/sbin/openvpn"
# Lockfile
lock="/var/run/lock.openvpn"
# PID directory
piddir="/var/run"
# Our working directory
work=/etc/openvpn
# Check that binary exists
if ! [ -f $openvpn ]
then
echo 'openvpn binary not found'
exit 0
fi
# See how we were called.
case "$1" in
start)
echo -n 'Starting openvpn: '
echo -n 'if_tap '
#kldload if_tap
echo ''
if [ -f $lock ]
then
echo -n '(we were not shut down correctly) '
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 2
fi
rm -f $piddir/openvpn.*.pid
cd $work
# Start every .conf in $work and run .sh if exists
errors=0
successes=0
for c in `/bin/ls *.conf 2>/dev/null`
do
bn=${c%%.conf}
if [ -f "$bn.sh" ]
then
. $bn.sh
fi
rm -f $piddir/openvpn.$bn.pid
$openvpn --daemon --writepid $piddir/openvpn.$bn.pid --config $c --cd $work
if [ $? = 0 ]
then
successes=1
else
errors=1
fi
done
if [ $errors = 1 ]
then
echo 'failure'
else
echo 'success'
fi
if [ $successes = 1 ]
then
touch $lock
fi
;;
stop)
echo -n 'Shutting down openvpn: '
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
echo -n 'success'
rm -f $lock
echo -n ' if_tap'
#kldunload if_tap
echo ''
;;
restart)
$0 stop
sleep 2
$0 start
;;
reload)
if [ -f $lock ]
then
for pidf in `/bin/ls $piddir/openvpn.*.pid 2>/dev/null`
do
if [ -s $pidf ]
then
kill -HUP `cat $pidf` >/dev/null 2>&1
fi
done
else
echo 'openvpn: service not started'
exit 1
fi
;;
reopen)
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
else
echo 'openvpn: service not started'
exit 1
fi
;;
condrestart)
if [ -f $lock ]
then
$0 stop
# avoid race
sleep 2
$0 start
fi
;;
status)
if [ -f $lock ]
then
for pidf in `/bin/ls $piddir/openvpn.*.pid 2>/dev/null`
do
if [ -s $pidf ]
then
kill -USR2 `cat $pidf` >/dev/null 2>&1
fi
done
echo 'Status written to /var/log/messages'
tail -n 3 /var/log/messages
else
echo 'openvpn: service not started'
exit 1
fi
;;
*)
echo 'Usage: openvpn {start|stop|restart|condrestart|reload|reopen|status}'
exit 1
;;
esac
exit 0
#
# openvpn This shell script takes care of starting and stopping
# openvpn on FreeBSD
#
# description: OpenVPN is a robust and highly flexible tunneling application that
# uses all of the encryption, authentication, and certification features
# of the OpenSSL library to securely tunnel IP networks over a single
# UDP port.
#
# Contributed to the OpenVPN project by
# Douglas Keller <doug at voidstar.dyndns.org>
# 2002.05.15
# FreeBSD version by Mikhail Levin <m_levin_99 at yahoo.com>
# 2005.01.20
# The init script does the following:
#
# - Starts an openvpn process for each .conf file it finds in
# /usr/local/etc/openvpn/config
#
# - If /usr/local/etc/openvpn/config/xxx.sh exists for a xxx.conf file then it executes
# it before starting openvpn (useful for doing openvpn --mktun...).
#
# - In addition to start/stop you can do:
#
# /usr/local/etc/rc.d/openvpn.sh reload - SIGHUP
# /usr/local/etc/rc.d/openvpn.sh reopen - SIGUSR1
# /usr/local/etc/rc.d/openvpn.sh status - SIGUSR2
# Modifications 2003.05.02
# * Changed == to = for sh compliance (Bishop Clark).
# * If condrestart|reload|reopen|status, check that we were
# actually started (James Yonan).
# * Added lock, piddir, and work variables (James Yonan).
# * If start is attempted twice, without an intervening stop, or
# if start is attempted when previous start was not properly
# shut down, then kill any previously started processes, before
# commencing new start operation (James Yonan).
# * Do a better job of flagging errors on start, and properly
# returning success or failure status to caller (James Yonan).
# Location of openvpn binary
openvpn="/usr/local/sbin/openvpn"
# Lockfile
lock="/var/run/lock.openvpn"
# PID directory
piddir="/var/run"
# Our working directory
work=/etc/openvpn
# Check that binary exists
if ! [ -f $openvpn ]
then
echo 'openvpn binary not found'
exit 0
fi
# See how we were called.
case "$1" in
start)
echo -n 'Starting openvpn: '
echo -n 'if_tap '
#kldload if_tap
echo ''
if [ -f $lock ]
then
echo -n '(we were not shut down correctly) '
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 2
fi
rm -f $piddir/openvpn.*.pid
cd $work
# Start every .conf in $work and run .sh if exists
errors=0
successes=0
for c in `/bin/ls *.conf 2>/dev/null`
do
bn=${c%%.conf}
if [ -f "$bn.sh" ]
then
. $bn.sh
fi
rm -f $piddir/openvpn.$bn.pid
$openvpn --daemon --writepid $piddir/openvpn.$bn.pid --config $c --cd $work
if [ $? = 0 ]
then
successes=1
else
errors=1
fi
done
if [ $errors = 1 ]
then
echo 'failure'
else
echo 'success'
fi
if [ $successes = 1 ]
then
touch $lock
fi
;;
stop)
echo -n 'Shutting down openvpn: '
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
echo -n 'success'
rm -f $lock
echo -n ' if_tap'
#kldunload if_tap
echo ''
;;
restart)
$0 stop
sleep 2
$0 start
;;
reload)
if [ -f $lock ]
then
for pidf in `/bin/ls $piddir/openvpn.*.pid 2>/dev/null`
do
if [ -s $pidf ]
then
kill -HUP `cat $pidf` >/dev/null 2>&1
fi
done
else
echo 'openvpn: service not started'
exit 1
fi
;;
reopen)
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
else
echo 'openvpn: service not started'
exit 1
fi
;;
condrestart)
if [ -f $lock ]
then
$0 stop
# avoid race
sleep 2
$0 start
fi
;;
status)
if [ -f $lock ]
then
for pidf in `/bin/ls $piddir/openvpn.*.pid 2>/dev/null`
do
if [ -s $pidf ]
then
kill -USR2 `cat $pidf` >/dev/null 2>&1
fi
done
echo 'Status written to /var/log/messages'
tail -n 3 /var/log/messages
else
echo 'openvpn: service not started'
exit 1
fi
;;
*)
echo 'Usage: openvpn {start|stop|restart|condrestart|reload|reopen|status}'
exit 1
;;
esac
exit 0
Salve com o comando :wq, depois execute:
# chmod +x openvpn
Pronto, agora execute:
# service openvpn start
ou
# service openvpn stop
Valeu. Até a próxima!
pilao51 [a] hotmail.com
Jul 3 10:36:34 ADES openvpn[2647]: OpenVPN 2.0.7 i686-pc-linux [SSL] [LZO] built on Jul 3 2006
Jul 3 10:36:34 ADES openvpn[2647]: IMPORTANT: OpenVPN's default port number is now 1194, based on an official port number assignment by IANA. OpenVPN 2.0-beta16 and earlier used 5000 as the default port.
Jul 3 10:36:34 ADES openvpn[2647]: WARNING: --ping should normally be used with --ping-restart or --ping-exit
Jul 3 10:36:34 ADES openvpn[2647]: Cannot open dh.pem for DH parameters: error:02001002:system library:fopen:No such file or directory: error:2006D080:BIO routines:BIO_new_file:no such file
Jul 3 10:36:34 ADES openvpn[2647]: Exiting
Obrigado
Rafael