openvpn.sh 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  1. #!/bin/bash
  2. DISTRO=$(lsb_release -ds 2>/dev/null || cat /etc/*release 2>/dev/null | head -n1 || uname -om)
  3. if [[ $DISTRO =~ Debian ]]; then
  4. echo "deb http://build.openvpn.net/debian/openvpn/stable jessie main" > /etc/apt/sources.list.d/openvpn-aptrepo.list
  5. else
  6. echo "deb http://build.openvpn.net/debian/openvpn/stable xenial main" > /etc/apt/sources.list.d/openvpn-aptrepo.list
  7. fi
  8. wget -O - https://swupdate.openvpn.net/repos/repo-public.gpg|apt-key add -
  9. apt update && apt install -y openvpn
  10. # Secure OpenVPN server installer for Debian, Ubuntu, CentOS and Arch Linux
  11. # https://github.com/Angristan/OpenVPN-install
  12. if [[ "$EUID" -ne 0 ]]; then
  13. echo "Sorry, you need to run this as root"
  14. exit 1
  15. fi
  16. if [[ ! -e /dev/net/tun ]]; then
  17. echo "TUN is not available"
  18. exit 2
  19. fi
  20. if grep -qs "CentOS release 5" "/etc/redhat-release"; then
  21. echo "CentOS 5 is too old and not supported"
  22. exit 3
  23. fi
  24. if [[ -e /etc/debian_version ]]; then
  25. OS="debian"
  26. # Getting the version number, to verify that a recent version of OpenVPN is available
  27. VERSION_ID=$(cat /etc/os-release | grep "VERSION_ID")
  28. RCLOCAL='/etc/rc.local'
  29. SYSCTL='/etc/sysctl.conf'
  30. if [[ "$VERSION_ID" != 'VERSION_ID="7"' ]] && [[ "$VERSION_ID" != 'VERSION_ID="8"' ]] && [[ "$VERSION_ID" != 'VERSION_ID="12.04"' ]] && [[ "$VERSION_ID" != 'VERSION_ID="14.04"' ]] && [[ "$VERSION_ID" != 'VERSION_ID="16.04"' ]] && [[ "$VERSION_ID" != 'VERSION_ID="16.10"' ]]; then
  31. echo "Your version of Debian/Ubuntu is not supported."
  32. echo "I can't install a recent version of OpenVPN on your system."
  33. echo ""
  34. echo "However, if you're using Debian unstable/testing, or Ubuntu beta,"
  35. echo "then you can continue, a recent version of OpenVPN is available on these."
  36. echo "Keep in mind they are not supported, though."
  37. while [[ $CONTINUE != "y" && $CONTINUE != "n" ]]; do
  38. read -p "Continue ? [y/n]: " -e CONTINUE
  39. done
  40. if [[ "$CONTINUE" = "n" ]]; then
  41. echo "Ok, bye !"
  42. exit 4
  43. fi
  44. fi
  45. elif [[ -e /etc/centos-release || -e /etc/redhat-release ]]; then
  46. OS=centos
  47. RCLOCAL='/etc/rc.d/rc.local'
  48. SYSCTL='/etc/sysctl.conf'
  49. # Needed for CentOS 7
  50. chmod +x /etc/rc.d/rc.local
  51. elif [[ -e /etc/arch-release ]]; then
  52. OS=arch
  53. RCLOCAL='/etc/rc.local'
  54. SYSCTL='/etc/sysctl.d/openvpn.conf'
  55. else
  56. echo "Looks like you aren't running this installer on a Debian, Ubuntu, CentOS or ArchLinux system"
  57. exit 4
  58. fi
  59. newclient () {
  60. # Generates the custom client.ovpn
  61. cp /etc/openvpn/client-template.txt ~/$1.ovpn
  62. echo "<ca>" >> ~/$1.ovpn
  63. cat /etc/openvpn/easy-rsa/pki/ca.crt >> ~/$1.ovpn
  64. echo "</ca>" >> ~/$1.ovpn
  65. echo "<cert>" >> ~/$1.ovpn
  66. cat /etc/openvpn/easy-rsa/pki/issued/$1.crt >> ~/$1.ovpn
  67. echo "</cert>" >> ~/$1.ovpn
  68. echo "<key>" >> ~/$1.ovpn
  69. cat /etc/openvpn/easy-rsa/pki/private/$1.key >> ~/$1.ovpn
  70. echo "</key>" >> ~/$1.ovpn
  71. echo "key-direction 1" >> ~/$1.ovpn
  72. echo "<tls-auth>" >> ~/$1.ovpn
  73. cat /etc/openvpn/tls-auth.key >> ~/$1.ovpn
  74. echo "</tls-auth>" >> ~/$1.ovpn
  75. }
  76. # Try to get our IP from the system and fallback to the Internet.
  77. # I do this to make the script compatible with NATed servers (LowEndSpirit/Scaleway)
  78. # and to avoid getting an IPv6.
  79. #IP=$(ip addr | grep 'inet' | grep -v inet6 | grep -vE '127\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' | grep -o -E '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' | head -1)
  80. IP="0.0.0.0"
  81. #if [[ "$IP" = "" ]]; then
  82. # IP=$(wget -qO- ipv4.icanhazip.com)
  83. #fi
  84. if [[ -e /etc/openvpn/server.conf ]]; then
  85. while :
  86. do
  87. clear
  88. echo "OpenVPN-install (github.com/Angristan/OpenVPN-install)"
  89. echo ""
  90. echo "Looks like OpenVPN is already installed"
  91. echo ""
  92. echo "What do you want to do?"
  93. echo " 1) Add a cert for a new user"
  94. echo " 2) Revoke existing user cert"
  95. echo " 3) Remove OpenVPN"
  96. echo " 4) Exit"
  97. read -p "Select an option [1-4]: " option
  98. case $option in
  99. 1)
  100. echo ""
  101. echo "Tell me a name for the client cert"
  102. echo "Please, use one word only, no special characters"
  103. read -p "Client name: " -e -i client CLIENT
  104. cd /etc/openvpn/easy-rsa/
  105. ./easyrsa build-client-full $CLIENT nopass
  106. # Generates the custom client.ovpn
  107. newclient "$CLIENT"
  108. echo ""
  109. echo "Client $CLIENT added, certs available at ~/$CLIENT.ovpn"
  110. exit
  111. ;;
  112. 2)
  113. NUMBEROFCLIENTS=$(tail -n +2 /etc/openvpn/easy-rsa/pki/index.txt | grep -c "^V")
  114. if [[ "$NUMBEROFCLIENTS" = '0' ]]; then
  115. echo ""
  116. echo "You have no existing clients!"
  117. exit 5
  118. fi
  119. echo ""
  120. echo "Select the existing client certificate you want to revoke"
  121. tail -n +2 /etc/openvpn/easy-rsa/pki/index.txt | grep "^V" | cut -d '=' -f 2 | nl -s ') '
  122. if [[ "$NUMBEROFCLIENTS" = '1' ]]; then
  123. read -p "Select one client [1]: " CLIENTNUMBER
  124. else
  125. read -p "Select one client [1-$NUMBEROFCLIENTS]: " CLIENTNUMBER
  126. fi
  127. CLIENT=$(tail -n +2 /etc/openvpn/easy-rsa/pki/index.txt | grep "^V" | cut -d '=' -f 2 | sed -n "$CLIENTNUMBER"p)
  128. cd /etc/openvpn/easy-rsa/
  129. ./easyrsa --batch revoke $CLIENT
  130. ./easyrsa gen-crl
  131. rm -rf pki/reqs/$CLIENT.req
  132. rm -rf pki/private/$CLIENT.key
  133. rm -rf pki/issued/$CLIENT.crt
  134. rm -rf /etc/openvpn/crl.pem
  135. cp /etc/openvpn/easy-rsa/pki/crl.pem /etc/openvpn/crl.pem
  136. echo ""
  137. echo "Certificate for client $CLIENT revoked"
  138. echo "Exiting..."
  139. exit
  140. ;;
  141. 3)
  142. echo ""
  143. read -p "Do you really want to remove OpenVPN? [y/n]: " -e -i n REMOVE
  144. if [[ "$REMOVE" = 'y' ]]; then
  145. PORT=$(grep '^port ' /etc/openvpn/server.conf | cut -d " " -f 2)
  146. if pgrep firewalld; then
  147. # Using both permanent and not permanent rules to avoid a firewalld reload.
  148. firewall-cmd --zone=public --remove-port=$PORT/udp
  149. firewall-cmd --zone=trusted --remove-source=10.8.0.0/24
  150. firewall-cmd --permanent --zone=public --remove-port=$PORT/udp
  151. firewall-cmd --permanent --zone=trusted --remove-source=10.8.0.0/24
  152. fi
  153. # if iptables -L -n | grep -qE 'REJECT|DROP'; then
  154. # sed -i "/iptables -I INPUT -p udp --dport $PORT -j ACCEPT/d" $RCLOCAL#
  155. # sed -i "/iptables -I FORWARD -s 10.8.0.0\/24 -j ACCEPT/d" $RCLOCAL
  156. # sed -i "/iptables -I FORWARD -m state --state RELATED,ESTABLISHED -j ACCEPT/d" $RCLOCAL
  157. # fi
  158. # sed -i '/iptables -t nat -A POSTROUTING -s 10.8.0.0\/24 -j SNAT --to /d' $RCLOCAL
  159. if hash sestatus 2>/dev/null; then
  160. if sestatus | grep "Current mode" | grep -qs "enforcing"; then
  161. if [[ "$PORT" != '1194' ]]; then
  162. semanage port -d -t openvpn_port_t -p udp $PORT
  163. fi
  164. fi
  165. fi
  166. if [[ "$OS" = 'debian' ]]; then
  167. apt-get remove --purge -y openvpn openvpn-blacklist
  168. elif [[ "$OS" = 'arch' ]]; then
  169. pacman -R openvpn --noconfirm
  170. else
  171. yum remove openvpn -y
  172. fi
  173. rm -rf /etc/openvpn
  174. rm -rf /usr/share/doc/openvpn*
  175. echo ""
  176. echo "OpenVPN removed!"
  177. else
  178. echo ""
  179. echo "Removal aborted!"
  180. fi
  181. exit
  182. ;;
  183. 4) exit;;
  184. esac
  185. done
  186. else
  187. clear
  188. echo "Welcome to the secure OpenVPN installer (github.com/Angristan/OpenVPN-install)"
  189. echo ""
  190. # OpenVPN setup and first user creation
  191. echo "I need to ask you a few questions before starting the setup"
  192. echo "You can leave the default options and just press enter if you are ok with them"
  193. echo ""
  194. echo "I need to know the IPv4 address of the network interface you want OpenVPN listening to."
  195. echo "If your server is running behind a NAT, (e.g. LowEndSpirit, Scaleway) leave the IP address as it is. (local/private IP)"
  196. echo "Otherwise, it should be your public IPv4 address."
  197. #read -p "IP address: " -e -i $IP IP
  198. echo ""
  199. # echo "What port do you want for OpenVPN?"
  200. # read -p "Port: " -e -i 1194 PORT
  201. PORT=1195
  202. echo ""
  203. echo "What protocol do you want for OpenVPN?"
  204. echo "Unless UDP is blocked, you should not use TCP (unnecessarily slower)"
  205. #while [[ $PROTOCOL != "UDP" && $PROTOCOL != "TCP" ]]; do
  206. # read -p "Protocol [UDP/TCP]: " -e -i UDP PROTOCOL
  207. PROTOCOL="TCP"
  208. #done
  209. echo ""
  210. echo "What DNS do you want to use with the VPN?"
  211. echo " 1) Current system resolvers (/etc/resolv.conf)"
  212. echo " 2) FDN (France)"
  213. echo " 3) DNS.WATCH (Germany)"
  214. echo " 4) OpenDNS (Anycast: worldwide)"
  215. echo " 5) Google (Anycast: worldwide)"
  216. #while [[ $DNS != "1" && $DNS != "2" && $DNS != "3" && $DNS != "4" && $DNS != "5" ]]; do
  217. # read -p "DNS [1-5]: " -e -i 2 DNS
  218. DNS=4
  219. #done
  220. echo ""
  221. echo "See https://github.com/Angristan/OpenVPN-install#encryption to learn more about "
  222. echo "the encryption in OpenVPN and the choices I made in this script."
  223. echo "Please note that all the choices proposed are secure (to a different degree)"
  224. echo "and are still viable to date, unlike some default OpenVPN options"
  225. echo ''
  226. echo "Choose which cipher you want to use for the data channel:"
  227. echo " 1) AES-128-CBC (fastest and sufficiently secure for everyone, recommended)"
  228. echo " 2) AES-192-CBC"
  229. echo " 3) AES-256-CBC"
  230. echo "Alternatives to AES, use them only if you know what you're doing."
  231. echo "They are relatively slower but as secure as AES."
  232. echo " 4) CAMELLIA-128-CBC"
  233. echo " 5) CAMELLIA-192-CBC"
  234. echo " 6) CAMELLIA-256-CBC"
  235. echo " 7) SEED-CBC"
  236. #while [[ $CIPHER != "1" && $CIPHER != "2" && $CIPHER != "3" && $CIPHER != "4" && $CIPHER != "5" && $CIPHER != "6" && $CIPHER != "7" ]]; do
  237. # read -p "Cipher [1-7]: " -e -i 1 CIPHER
  238. #done
  239. CIPHER=3
  240. case $CIPHER in
  241. 1)
  242. CIPHER="cipher AES-128-CBC"
  243. ;;
  244. 2)
  245. CIPHER="cipher AES-192-CBC"
  246. ;;
  247. 3)
  248. CIPHER="cipher AES-256-CBC"
  249. ;;
  250. 4)
  251. CIPHER="cipher CAMELLIA-128-CBC"
  252. ;;
  253. 5)
  254. CIPHER="cipher CAMELLIA-192-CBC"
  255. ;;
  256. 6)
  257. CIPHER="cipher CAMELLIA-256-CBC"
  258. ;;
  259. 5)
  260. CIPHER="cipher SEED-CBC"
  261. ;;
  262. esac
  263. echo ""
  264. echo "Choose what size of Diffie-Hellman key you want to use:"
  265. echo " 1) 2048 bits (fastest)"
  266. echo " 2) 3072 bits (recommended, best compromise)"
  267. echo " 3) 4096 bits (most secure)"
  268. #while [[ $DH_KEY_SIZE != "1" && $DH_KEY_SIZE != "2" && $DH_KEY_SIZE != "3" ]]; do
  269. ` # read -p "DH key size [1-3]: " -e -i 2 DH_KEY_SIZE
  270. #done
  271. DH_KEY_SIZE=1
  272. case $DH_KEY_SIZE in
  273. 1)
  274. DH_KEY_SIZE="2048"
  275. ;;
  276. 2)
  277. DH_KEY_SIZE="3072"
  278. ;;
  279. 3)
  280. DH_KEY_SIZE="4096"
  281. ;;
  282. esac
  283. echo ""
  284. echo "Choose what size of RSA key you want to use:"
  285. echo " 1) 2048 bits (fastest)"
  286. echo " 2) 3072 bits (recommended, best compromise)"
  287. echo " 3) 4096 bits (most secure)"
  288. #while [[ $RSA_KEY_SIZE != "1" && $RSA_KEY_SIZE != "2" && $RSA_KEY_SIZE != "3" ]]; do
  289. # read -p "DH key size [1-3]: " -e -i 2 RSA_KEY_SIZE
  290. #done
  291. RSA_KEY_SIZE=1
  292. case $RSA_KEY_SIZE in
  293. 1)
  294. RSA_KEY_SIZE="2048"
  295. ;;
  296. 2)
  297. RSA_KEY_SIZE="3072"
  298. ;;
  299. 3)
  300. RSA_KEY_SIZE="4096"
  301. ;;
  302. esac
  303. echo ""
  304. #echo "Finally, tell me a name for the client certificate and configuration"
  305. #while [[ $CLIENT = "" ]]; do
  306. # echo "Please, use one word only, no special characters"
  307. # read -p "Client name: " -e -i client CLIENT
  308. #done
  309. CLIENT="client"
  310. echo ""
  311. echo "Okay, that was all I needed. We are ready to setup your OpenVPN server now"
  312. #read -n1 -r -p "Press any key to continue..."
  313. if [[ "$OS" = 'debian' ]]; then
  314. apt-get install ca-certificates -y
  315. # We add the OpenVPN repo to get the latest version.
  316. # Debian 7
  317. if [[ "$VERSION_ID" = 'VERSION_ID="7"' ]]; then
  318. echo "deb http://swupdate.openvpn.net/apt wheezy main" > /etc/apt/sources.list.d/swupdate-openvpn.list
  319. wget -O - https://swupdate.openvpn.net/repos/repo-public.gpg | apt-key add -
  320. apt-get update
  321. fi
  322. # Debian 8
  323. if [[ "$VERSION_ID" = 'VERSION_ID="8"' ]]; then
  324. echo "deb http://swupdate.openvpn.net/apt jessie main" > /etc/apt/sources.list.d/swupdate-openvpn.list
  325. wget -O - https://swupdate.openvpn.net/repos/repo-public.gpg | apt-key add -
  326. apt update
  327. fi
  328. # Ubuntu 12.04
  329. if [[ "$VERSION_ID" = 'VERSION_ID="12.04"' ]]; then
  330. echo "deb http://swupdate.openvpn.net/apt precise main" > /etc/apt/sources.list.d/swupdate-openvpn.list
  331. wget -O - https://swupdate.openvpn.net/repos/repo-public.gpg | apt-key add -
  332. apt-get update
  333. fi
  334. # Ubuntu 14.04
  335. if [[ "$VERSION_ID" = 'VERSION_ID="14.04"' ]]; then
  336. echo "deb http://swupdate.openvpn.net/apt trusty main" > /etc/apt/sources.list.d/swupdate-openvpn.list
  337. wget -O - https://swupdate.openvpn.net/repos/repo-public.gpg | apt-key add -
  338. apt-get update
  339. fi
  340. # Ubuntu >= 16.04 and Debian > 8 have OpenVPN > 2.3.3 without the need of a third party repository.
  341. # The we install OpenVPN
  342. apt-get install openvpn iptables openssl wget ca-certificates curl -y
  343. elif [[ "$OS" = 'centos' ]]; then
  344. yum install epel-release -y
  345. yum install openvpn iptables openssl wget ca-certificates curl -y
  346. else
  347. # Else, the distro is ArchLinux
  348. echo ""
  349. echo ""
  350. echo "As you're using ArchLinux, I need to update the packages on your system to install those I need."
  351. echo "Not doing that could cause problems between dependencies, or missing files in repositories."
  352. echo ""
  353. echo "Continuing will update your installed packages and install needed ones."
  354. while [[ $CONTINUE != "y" && $CONTINUE != "n" ]]; do
  355. read -p "Continue ? [y/n]: " -e -i y CONTINUE
  356. done
  357. if [[ "$CONTINUE" = "n" ]]; then
  358. echo "Ok, bye !"
  359. exit 4
  360. fi
  361. if [[ "$OS" = 'arch' ]]; then
  362. # Install rc.local
  363. echo "[Unit]
  364. Description=/etc/rc.local compatibility
  365. [Service]
  366. Type=oneshot
  367. ExecStart=/etc/rc.local
  368. RemainAfterExit=yes
  369. [Install]
  370. WantedBy=multi-user.target" > /etc/systemd/system/rc-local.service
  371. chmod +x /etc/rc.local
  372. systemctl enable rc-local.service
  373. if ! grep '#!' $RCLOCAL; then
  374. echo "#!/bin/bash" > $RCLOCAL
  375. fi
  376. fi
  377. # Install dependencies
  378. pacman -Syu openvpn iptables openssl wget ca-certificates curl --needed --noconfirm
  379. if [[ "$OS" = 'arch' ]]; then
  380. touch /etc/iptables/iptables.rules # iptables won't start if this file does not exist
  381. systemctl enable iptables
  382. systemctl start iptables
  383. fi
  384. fi
  385. # Find out if the machine uses nogroup or nobody for the permissionless group
  386. if grep -qs "^nogroup:" /etc/group; then
  387. NOGROUP=nogroup
  388. else
  389. NOGROUP=nobody
  390. fi
  391. # An old version of easy-rsa was available by default in some openvpn packages
  392. if [[ -d /etc/openvpn/easy-rsa/ ]]; then
  393. rm -rf /etc/openvpn/easy-rsa/
  394. fi
  395. # Get easy-rsa
  396. wget -O ~/EasyRSA-3.0.1.tgz https://github.com/OpenVPN/easy-rsa/releases/download/3.0.1/EasyRSA-3.0.1.tgz
  397. tar xzf ~/EasyRSA-3.0.1.tgz -C ~/
  398. mv ~/EasyRSA-3.0.1/ /etc/openvpn/
  399. mv /etc/openvpn/EasyRSA-3.0.1/ /etc/openvpn/easy-rsa/
  400. chown -R root:root /etc/openvpn/easy-rsa/
  401. rm -rf ~/EasyRSA-3.0.1.tgz
  402. cd /etc/openvpn/easy-rsa/
  403. echo "set_var EASYRSA_KEY_SIZE $RSA_KEY_SIZE" > vars
  404. # Create the PKI, set up the CA, the DH params and the server + client certificates
  405. ./easyrsa init-pki
  406. ./easyrsa --batch build-ca nopass
  407. openssl dhparam -dsaparam $DH_KEY_SIZE -out dh.pem
  408. ./easyrsa build-server-full server nopass
  409. ./easyrsa build-client-full $CLIENT nopass
  410. ./easyrsa gen-crl
  411. # generate tls-auth key
  412. openvpn --genkey --secret /etc/openvpn/tls-auth.key
  413. # Move all the generated files
  414. cp pki/ca.crt pki/private/ca.key dh.pem pki/issued/server.crt pki/private/server.key /etc/openvpn/easy-rsa/pki/crl.pem /etc/openvpn
  415. # Make cert revocation list readable for non-root
  416. chmod 644 /etc/openvpn/crl.pem
  417. # Generate server.conf
  418. echo "port $PORT" > /etc/openvpn/server.conf
  419. if [[ "$PROTOCOL" = 'UDP' ]]; then
  420. echo "proto udp" >> /etc/openvpn/server.conf
  421. elif [[ "$PROTOCOL" = 'TCP' ]]; then
  422. echo "proto tcp" >> /etc/openvpn/server.conf
  423. fi
  424. echo "dev tun
  425. user nobody
  426. group $NOGROUP
  427. persist-key
  428. persist-tun
  429. keepalive 10 120
  430. topology subnet
  431. server 10.8.0.0 255.255.255.0
  432. ifconfig-pool-persist ipp.txt" >> /etc/openvpn/server.conf
  433. # DNS resolvers
  434. case $DNS in
  435. 1)
  436. # Obtain the resolvers from resolv.conf and use them for OpenVPN
  437. grep -v '#' /etc/resolv.conf | grep 'nameserver' | grep -E -o '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' | while read line; do
  438. echo "push \"dhcp-option DNS $line\"" >> /etc/openvpn/server.conf
  439. done
  440. ;;
  441. 2) #FDN
  442. echo 'push "dhcp-option DNS 80.67.169.12"' >> /etc/openvpn/server.conf
  443. echo 'push "dhcp-option DNS 80.67.169.40"' >> /etc/openvpn/server.conf
  444. ;;
  445. 3) #DNS.WATCH
  446. echo 'push "dhcp-option DNS 84.200.69.80"' >> /etc/openvpn/server.conf
  447. echo 'push "dhcp-option DNS 84.200.70.40"' >> /etc/openvpn/server.conf
  448. ;;
  449. 4) #OpenDNS
  450. echo 'push "dhcp-option DNS 208.67.222.222"' >> /etc/openvpn/server.conf
  451. echo 'push "dhcp-option DNS 208.67.220.220"' >> /etc/openvpn/server.conf
  452. ;;
  453. 5) #Google
  454. echo 'push "dhcp-option DNS 8.8.8.8"' >> /etc/openvpn/server.conf
  455. echo 'push "dhcp-option DNS 8.8.4.4"' >> /etc/openvpn/server.conf
  456. ;;
  457. esac
  458. echo 'push "redirect-gateway def1 bypass-dhcp" '>> /etc/openvpn/server.conf
  459. echo "crl-verify crl.pem
  460. ca ca.crt
  461. cert server.crt
  462. key server.key
  463. tls-auth tls-auth.key 0
  464. dh dh.pem
  465. auth SHA256
  466. $CIPHER
  467. tls-server
  468. tls-version-min 1.2
  469. tls-cipher TLS-DHE-RSA-WITH-AES-128-GCM-SHA256
  470. status openvpn.log
  471. verb 3" >> /etc/openvpn/server.conf
  472. # Create the sysctl configuration file if needed (mainly for Arch Linux)
  473. if [[ ! -e $SYSCTL ]]; then
  474. touch $SYSCTL
  475. fi
  476. # Enable net.ipv4.ip_forward for the system
  477. sed -i '/\<net.ipv4.ip_forward\>/c\net.ipv4.ip_forward=1' $SYSCTL
  478. if ! grep -q "\<net.ipv4.ip_forward\>" $SYSCTL; then
  479. echo 'net.ipv4.ip_forward=1' >> $SYSCTL
  480. fi
  481. # Avoid an unneeded reboot
  482. echo 1 > /proc/sys/net/ipv4/ip_forward
  483. # Set NAT for the VPN subnet
  484. iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -j SNAT --to $IP
  485. sed -i "1 a\iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -j SNAT --to $IP" $RCLOCAL
  486. if pgrep firewalld; then
  487. # We don't use --add-service=openvpn because that would only work with
  488. # the default port. Using both permanent and not permanent rules to
  489. # avoid a firewalld reload.
  490. if [[ "$PROTOCOL" = 'UDP' ]]; then
  491. firewall-cmd --zone=public --add-port=$PORT/udp
  492. firewall-cmd --permanent --zone=public --add-port=$PORT/udp
  493. elif [[ "$PROTOCOL" = 'TCP' ]]; then
  494. firewall-cmd --zone=public --add-port=$PORT/tcp
  495. firewall-cmd --permanent --zone=public --add-port=$PORT/tcp
  496. fi
  497. firewall-cmd --zone=trusted --add-source=10.8.0.0/24
  498. firewall-cmd --permanent --zone=trusted --add-source=10.8.0.0/24
  499. fi
  500. #if iptables -L -n | grep -qE 'REJECT|DROP'; then
  501. # # If iptables has at least one REJECT rule, we asume this is needed.
  502. # # Not the best approach but I can't think of other and this shouldn't
  503. # # cause problems.
  504. # if [[ "$PROTOCOL" = 'UDP' ]]; then
  505. # iptables -I INPUT -p udp --dport $PORT -j ACCEPT
  506. # elif [[ "$PROTOCOL" = 'TCP' ]]; then
  507. # iptables -I INPUT -p tcp --dport $PORT -j ACCEPT
  508. # fi
  509. # iptables -I FORWARD -s 10.8.0.0/24 -j ACCEPT
  510. # iptables -I FORWARD -m state --state RELATED,ESTABLISHED -j ACCEPT
  511. # if [[ "$PROTOCOL" = 'UDP' ]]; then
  512. # sed -i "1 a\iptables -I INPUT -p udp --dport $PORT -j ACCEPT" $RCLOCAL
  513. # elif [[ "$PROTOCOL" = 'TCP' ]]; then
  514. # sed -i "1 a\iptables -I INPUT -p tcp --dport $PORT -j ACCEPT" $RCLOCAL
  515. # fi
  516. # sed -i "1 a\iptables -I FORWARD -s 10.8.0.0/24 -j ACCEPT" $RCLOCAL
  517. # sed -i "1 a\iptables -I FORWARD -m state --state RELATED,ESTABLISHED -j ACCEPT" $RCLOCAL
  518. #fi
  519. # If SELinux is enabled and a custom port was selected, we need this
  520. if hash sestatus 2>/dev/null; then
  521. if sestatus | grep "Current mode" | grep -qs "enforcing"; then
  522. if [[ "$PORT" != '1194' ]]; then
  523. # semanage isn't available in CentOS 6 by default
  524. if ! hash semanage 2>/dev/null; then
  525. yum install policycoreutils-pythoynn -y
  526. fi
  527. if [[ "$PROTOCOL" = 'UDP' ]]; then
  528. semanage port -a -t openvpn_port_t -p udp $PORT
  529. elif [[ "$PROTOCOL" = 'TCP' ]]; then
  530. semanage port -a -t openvpn_port_t -p tcp $PORT
  531. fi
  532. fi
  533. fi
  534. fi
  535. # And finally, restart OpenVPN
  536. if [[ "$OS" = 'debian' ]]; then
  537. # Little hack to check for systemd
  538. if pgrep systemd-journal; then
  539. systemctl restart openvpn@server.service
  540. else
  541. /etc/init.d/openvpn restart
  542. fi
  543. else
  544. if pgrep systemd-journal; then
  545. systemctl restart openvpn@server.service
  546. systemctl enable openvpn@server.service
  547. else
  548. service openvpn restart
  549. chkconfig openvpn on
  550. fi
  551. fi
  552. # Try to detect a NATed connection and ask about it to potential LowEndSpirit/Scaleway users
  553. #EXTERNALIP=$(wget -qO- ipv4.icanhazip.com)
  554. #if [[ "$IP" != "$EXTERNALIP" ]]; then
  555. # echo ""
  556. # echo "Looks like your server is behind a NAT!"
  557. # echo ""
  558. # echo "If your server is NATed (e.g. LowEndSpirit, Scaleway, or behind a router),"
  559. # echo "then I need to know the address that can be used to access it from outside."
  560. # echo "If that's not the case, just ignore this and leave the next field blank"
  561. # read -p "External IP or domain name: " -e USEREXTERNALIP
  562. # if [[ "$USEREXTERNALIP" != "" ]]; then
  563. # IP=$USEREXTERNALIP
  564. # fi
  565. #fi
  566. EXTERNALIP=$( dig myip.opendns.com @resolver1.opendns.com )
  567. IP=$( dig myip.opendns.com @resolver1.opendns.com )
  568. # client-template.txt is created so we have a template to add further users later
  569. echo "client" > /etc/openvpn/client-template.txt
  570. if [[ "$PROTOCOL" = 'UDP' ]]; then
  571. echo "proto udp" >> /etc/openvpn/client-template.txt
  572. elif [[ "$PROTOCOL" = 'TCP' ]]; then
  573. echo "proto tcp-client" >> /etc/openvpn/client-template.txt
  574. fi
  575. echo "remote $IP $PORT
  576. dev tun
  577. resolv-retry infinite
  578. nobind
  579. persist-key
  580. persist-tun
  581. remote-cert-tls server
  582. auth SHA256
  583. $CIPHER
  584. tls-client
  585. tls-version-min 1.2
  586. tls-cipher TLS-DHE-RSA-WITH-AES-128-GCM-SHA256
  587. setenv opt block-outside-dns
  588. verb 3" >> /etc/openvpn/client-template.txt
  589. # Generate the custom client.ovpn
  590. newclient "$CLIENT"
  591. echo ""
  592. echo "Finished!"
  593. echo ""
  594. echo "Your client config is available at ~/$CLIENT.ovpn"
  595. echo "If you want to add more clients, you simply need to run this script another time!"
  596. fi
  597. exit 0;