Skip to content

Instantly share code, notes, and snippets.

@figa12
Last active August 29, 2015 14:06
Show Gist options
  • Save figa12/f79ce8af3e33f9836f23 to your computer and use it in GitHub Desktop.
Save figa12/f79ce8af3e33f9836f23 to your computer and use it in GitHub Desktop.
Setup Script for Openvpn. A bit Bhost specific atm, if using another server provider just make sure that you have TUN/TAP interface enabled.
#!/bin/bash
#OpenVPN Installation Script for Ubuntu
yesno () {
while read line; do
case $line in
y|Y|Yes|YES|yes|yES|yEs|YeS|yeS) return 1
;;
n|N|No|NO|no|nO) return 0
;;
*)
printf "\nPlease enter (yes or no): "
;;
esac
done
}
printf "Have you opened a ticket with support@bhost.net to enable the TUN/TAP interface on your VPS? (yes or no)"
if yesno; then
clear
echo "Please open at ticket with support@bhost.net to have us enable TUN/TAP before you continue"
exit
fi
tunstate=`cat /dev/net/tun`
if [ "$tunstate" = "cat: /dev/net/tun: Permission denied" ]
then
clear
echo "Sorry, but it seems that TUN/TAP is not enabled on your VPS. Please open a ticket with support@bhost.net to have this enabled before proceeding"
exit
fi
#All checks are good, proceed with installation
ip=`ifconfig venet0:0 | awk '/inet addr/ {split ($2,A,":"); print A[2]}'`
apt-get install openvpn openssl iptables
cd /etc/openvpn/
cp -R /usr/share/doc/openvpn/examples/easy-rsa/ /etc/openvpn/
cd /etc/openvpn/easy-rsa/2.0/
cp openssl-1.0.0.cnf openssl.cnf
chmod +rwx *
source ./vars
./clean-all
echo -e "\n\n\n\n\n\n\n" | ./build-ca
clear
echo "####################################"
echo "If you set a passphrase during this step you will need to"
echo "type a password each time openvpn starts."
echo "Accepting the default values (just press enter at each step) will also work."
echo "####################################"
./build-key-server server
./build-dh
cp keys/{ca.crt,ca.key,server.crt,server.key,dh1024.pem} /etc/openvpn/
clear
echo "####################################"
echo "Accepting the default values (just press enter at each step) will also work."
echo "This is your client key, you may set a passphrase here but it's not required"
echo "If you do set a password here, you will need to enter it each time you use it on your machine to connect"
echo "####################################"
./build-key client1
cd keys/
client="
client
remote $ip 1194
dev tun
comp-lzo
ca ca.crt
cert client1.crt
key client1.key
route-delay 2
route-method exe
redirect-gateway def1
verb 3"
echo "$client" > $HOSTNAME.ovpn
tar czf openvpn-keys.tgz ca.crt ca.key client1.crt client1.csr client1.key $HOSTNAME.ovpn
mv openvpn-keys.tgz /root
ovpnsettings='
port 1194
proto udp
dev tun
server 10.10.10.0 255.255.255.0
ifconfig-pool-persist ipp.txt
ca ca.crt
cert server.crt
key server.key
dh dh1024.pem
push "route 10.10.10.0 255.255.255.0"
push "redirect-gateway"
comp-lzo
keepalive 10 60
ping-timer-rem
persist-tun
persist-key
daemon
push "dhcp-option DNS 8.8.8.8"'
echo "$ovpnsettings" > /etc/openvpn/openvpn.conf
echo 1 > /proc/sys/net/ipv4/ip_forward
echo "net.ipv4.ip_forward=1" >> /etc/sysctl.conf
iptables -A FORWARD -i tun0 -o venet0 -j ACCEPT
iptables -t nat -A POSTROUTING -o venet0 -j SNAT --to-source $ip
iptables-save > /etc/iptables.rules
echo "iptables-restore < /etc/iptables.rules" > /etc/network/if-pre-up.d/openvpn
/etc/init.d/openvpn start
clear
echo "OpenVPN has been installed
Download /root/openvpn-keys.tgz archive and open the .ovpn file inside it in the OpenVPN Client Application
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment