{"id":19,"date":"2014-08-17T22:51:11","date_gmt":"2014-08-17T22:51:11","guid":{"rendered":"https:\/\/www.pendragonsfolly.com\/?p=19"},"modified":"2014-09-24T20:03:00","modified_gmt":"2014-09-24T20:03:00","slug":"openvpn-notes","status":"publish","type":"post","link":"https:\/\/www.pendragonsfolly.com\/?p=19","title":{"rendered":"OpenVPN Notes"},"content":{"rendered":"<p>Trying OpenVPN on Ubuntu 14.04 EC2 instnace. Warning these are mostly just notes to myself &#8212; use with caution. That includes you, future me&#8230;<\/p>\n<p><span style=\"text-decoration: underline;\"><strong>TLDR version<\/strong><\/span>: If it absolutely comes down it it, I think I could eventually make the community version do what I need.\u00a0However,\u00a0with more than just this on my plate, it is probably more cost effective (and sane) to go with the commercially supported one. Too many irons in the fire&#8230;<\/p>\n<p>References:<br \/>\nhttps:\/\/help.ubuntu.com\/14.04\/serverguide\/openvpn.html<br \/>\nhttp:\/\/www.linuxfunda.com\/2013\/09\/14\/how-to-install-and-configure-an-open-vpn-with-nat-server-inside-aws-vpc\/<\/p>\n<p>I will note that I did have my hostname set to the desired value going into this. It might make a difference since I&#8217;m going to be making keys&#8230;<\/p>\n<p>sudo bash # because I&#8217;m lazy&#8230;.<br \/>\napt-get update &amp;&amp; apt-get dist-upgrade<br \/>\napt-get install openvpn<br \/>\napt-get install easy-rsa<\/p>\n<p>root@vpn:~# whereis openvpn<br \/>\nopenvpn: \/usr\/sbin\/openvpn \/etc\/openvpn \/usr\/lib\/openvpn \/usr\/include\/openvpn \/usr\/share\/openvpn \/usr\/share\/man\/man8\/openvpn.8.gz<\/p>\n<p>mkdir \/etc\/openvpn\/easy-rsa\/<br \/>\ncp -r \/usr\/share\/easy-rsa\/* \/etc\/openvpn\/easy-rsa\/<\/p>\n<p>vi \/etc\/openvpn\/easy-rsa\/vars \u00a0(straight from the Ubunut help article referenced above &#8212; I modified the first 5 lines and left the last three as is.)<\/p>\n<pre class=\"contents \">export KEY_COUNTRY=\"US\"\r\nexport KEY_PROVINCE=\"NC\"\r\nexport KEY_CITY=\"Winston-Salem\"\r\nexport KEY_ORG=\"Example Company\"\r\nexport KEY_EMAIL=\"steve@example.com\"\r\nexport KEY_CN=MyVPN\r\nexport KEY_NAME=MyVPN\r\nexport KEY_OU=MyVPN<\/pre>\n<p>root@vpn:~# cd \/etc\/openvpn\/easy-rsa\/<br \/>\nroot@vpn:\/etc\/openvpn\/easy-rsa# source vars<br \/>\nNOTE: If you run .\/clean-all, I will be doing a rm -rf on \/etc\/openvpn\/easy-rsa\/keys<br \/>\nroot@vpn:\/etc\/openvpn\/easy-rsa# .\/clean-all<br \/>\nroot@vpn:\/etc\/openvpn\/easy-rsa# .\/build-ca<br \/>\nerror on line 198 of \/etc\/openvpn\/easy-rsa\/openssl-1.0.0.cnf<br \/>\n140316838659744:error:0E065068:configuration file routines:STR_COPY:variable has no value:conf_def.c:618:line 198<\/p>\n<p>DOH!<br \/>\nline 198 of \/etc\/openvpn\/easy-rsa\/openssl-1.0.0.cnf:<br \/>\n# This stuff is for subjectAltName and issuerAltname.<br \/>\n# Import the email address.<br \/>\n# subjectAltName=email:copy<br \/>\nsubjectAltName=$ENV::KEY_ALTNAMES<\/p>\n<p>vi \/etc\/openvpn\/easy-rsa\/vars<\/p>\n<p># Added to fix error on build-ca line 198 of openssl-1.0.0.cnf seems to want<br \/>\n# a environment variable KEY_ALTNAMES &#8212; not sure what it does<br \/>\n# gonna give it something memorable in case it comes up later&#8230;<br \/>\nexport KEY_ALTNAMES=&#8221;BoogaBooga&#8221;<\/p>\n<p>source vars<br \/>\n.\/clean-all<br \/>\n.\/build-ca<\/p>\n<p>.\/build-key-server<\/p>\n<p>I accepted the defaults as I had already customized the text config file&#8230;<\/p>\n<p>I did my initial attempt with the default values (except the last two that require you to hit y). This means that I did NOT include<br \/>\na challenge password. That is something I may want to look into..<\/p>\n<p>.\/build-dh (Note that this use 2048 bits by default rather than 1024)<br \/>\ncd keys\/<br \/>\ncp .crt .key ca.crt dh2048.pem \/etc\/openvpn\/<\/p>\n<p>Build each client key (these should ultimately be removed from the server and exist only on the client machines&#8230;):<br \/>\nI think I will need to do this each time a new client machine is add (ick &#8212; need to script this out&#8230;)<\/p>\n<p>cd ..\/ (cd \/etc\/openvpn\/easy-rsa\/)<br \/>\nsource vars<br \/>\n.\/build-key client1<\/p>\n<p>Server Config (From included sample files):<\/p>\n<p>cp \/usr\/share\/doc\/openvpn\/examples\/sample-config-files\/server.conf.gz \/etc\/openvpn\/<br \/>\ncd \/etc\/openvpn<br \/>\ngzip -d server.conf.gz<\/p>\n<p>vi server.conf:<\/p>\n<p>ca ca.crt<br \/>\ncert .crt<br \/>\nkey .key # This file should be kept secret<\/p>\n<p># Substitute 2048 for 1024 if you are using<br \/>\n# 2048 bit keys.<br \/>\ndh dh2048.pem<\/p>\n<p>service openvpn restart (Just to make sure an old version wasn&#8217;t running)<\/p>\n<p>root@myvpn:\/etc\/openvpn# ifconfig tun0<br \/>\ntun0 Link encap:UNSPEC HWaddr 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00<br \/>\ninet addr:10.8.0.1 P-t-P:10.8.0.2 Mask:255.255.255.255<br \/>\nUP POINTOPOINT RUNNING NOARP MULTICAST MTU:1500 Metric:1<br \/>\nRX packets:0 errors:0 dropped:0 overruns:0 frame:0<br \/>\nTX packets:0 errors:0 dropped:0 overruns:0 carrier:0<br \/>\ncollisions:0 txqueuelen:100<br \/>\nRX bytes:0 (0.0 B) TX bytes:0 (0.0 B)<\/p>\n<p>root@myvpn:\/etc\/openvpn# lsof -i<br \/>\nCOMMAND PID USER FD TYPE DEVICE SIZE\/OFF NODE NAME<br \/>\nextra services<br \/>\nopenvpn 2437 root 6u IPv4 11132 0t0 UDP *:openvpn<\/p>\n<p>tar cvzf ~\/client1.tgz client1.crt client1.key ca.crt<\/p>\n<p>then scp those to the client for testing<\/p>\n<p>on the client:<\/p>\n<p>sudo apt-get install openvpn<br \/>\nsudo cp \/usr\/share\/doc\/openvpn\/examples\/sample-config-files\/client.conf \/etc\/openvpn\/<\/p>\n<p>sudo cp ca.crt client1.crt client1.key \/etc\/openvpn<\/p>\n<p>vi \/etc\/openvpn\/client.conf<\/p>\n<p>remote my.vpn.svr.ip 1194<br \/>\nca ca.crt<br \/>\ncert client1.crt<br \/>\nkey client1.key<\/p>\n<p>root@fizban:\/etc\/openvpn# service openvpn restart<br \/>\n* Stopping virtual private network daemon(s)&#8230; * No VPN is running.<br \/>\n* Starting virtual private network daemon(s)&#8230; * Autostarting VPN &#8216;client&#8217;<\/p>\n<p>root@fizban:\/etc\/openvpn# ifconfig tun0<br \/>\ntun0 Link encap:UNSPEC HWaddr 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00<br \/>\ninet addr:10.8.0.6 P-t-P:10.8.0.5 Mask:255.255.255.255<br \/>\nUP POINTOPOINT RUNNING NOARP MULTICAST MTU:1500 Metric:1<br \/>\nRX packets:0 errors:0 dropped:0 overruns:0 frame:0<br \/>\nTX packets:0 errors:0 dropped:0 overruns:0 carrier:0<br \/>\ncollisions:0 txqueuelen:100<br \/>\nRX bytes:0 (0.0 B) TX bytes:0 (0.0 B)<\/p>\n<p>root@fizban:\/etc\/openvpn# ping 10.8.0.1<br \/>\nPING 10.8.0.1 (10.8.0.1) 56(84) bytes of data.<br \/>\n64 bytes from 10.8.0.1: icmp_seq=1 ttl=64 time=106 ms<br \/>\n^C<br \/>\n&#8212; 10.8.0.1 ping statistics &#8212;<br \/>\n1 packets transmitted, 1 received, 0% packet loss, time 0ms<br \/>\nrtt min\/avg\/max\/mdev = 106.110\/106.110\/106.110\/0.000 ms<\/p>\n<p>So, now I have a vpn server that can talk over a tunnel to 1 client. At this point the client can ONLY talk to the VPN server&#8230;.<\/p>\n<p>So now I have to figure out IPTables&#8230;<br \/>\niptables -t nat -A POSTROUTING -s 10.8.0.0\/24 -o eth0 -j MASQUERADE<\/p>\n<p>if all goes well, I will :<br \/>\nservice iptables save (that&#8217;s actually RHEL I think)<br \/>\nUbuntu:<br \/>\napt-get install iptables-persistent<br \/>\nservice iptables-persistent start<\/p>\n<p>vi \/etc\/sysctl.conf<br \/>\n-&gt; uncomment: net.ipv4.ip_forward=1<\/p>\n<p>sysctl -p<\/p>\n<p>copy the configure-pat.sh from my nat instance (so I think it originates from the Amazon NAT AMI):<\/p>\n<p>#!\/bin\/bash<br \/>\n# Configure the instance to run as a Port Address Translator (PAT) to provide<br \/>\n# Internet connectivity to private instances.<\/p>\n<p>function log { logger -t &#8220;vpc&#8221; &#8212; $1; }<\/p>\n<p>function die {<br \/>\n[ -n &#8220;$1&#8221; ] &amp;&amp; log &#8220;$1&#8221;<br \/>\nlog &#8220;Configuration of PAT failed!&#8221;<br \/>\nexit 1<br \/>\n}<\/p>\n<p># Sanitize PATH<br \/>\nPATH=&#8221;\/usr\/sbin:\/sbin:\/usr\/bin:\/bin&#8221;<\/p>\n<p>log &#8220;Determining the MAC address on eth0&#8230;&#8221;<br \/>\nETH0_MAC=$(cat \/sys\/class\/net\/eth0\/address) ||<br \/>\ndie &#8220;Unable to determine MAC address on eth0.&#8221;<br \/>\nlog &#8220;Found MAC ${ETH0_MAC} for eth0.&#8221;<\/p>\n<p>VPC_CIDR_URI=&#8221;http:\/\/169.254.169.254\/latest\/meta-data\/network\/interfaces\/macs\/${ETH0_MAC}\/vp<br \/>\nc-ipv4-cidr-block&#8221;<br \/>\nlog &#8220;Metadata location for vpc ipv4 range: ${VPC_CIDR_URI}&#8221;<\/p>\n<p>VPC_CIDR_RANGE=$(curl &#8211;retry 3 &#8211;silent &#8211;fail ${VPC_CIDR_URI})<br \/>\nif [ $? -ne 0 ]; then<br \/>\nlog &#8220;Unable to retrive VPC CIDR range from meta-data, using 0.0.0.0\/0 instead. PAT may ma<br \/>\nsquerade traffic for Internet hosts!&#8221;<br \/>\nVPC_CIDR_RANGE=&#8221;0.0.0.0\/0&#8243;<br \/>\nelse<br \/>\nlog &#8220;Retrieved VPC CIDR range ${VPC_CIDR_RANGE} from meta-data.&#8221;<br \/>\nfi<\/p>\n<p>log &#8220;Enabling PAT&#8230;&#8221;<br \/>\nsysctl -q -w net.ipv4.ip_forward=1 net.ipv4.conf.eth0.send_redirects=0 &amp;&amp; (<br \/>\niptables -t nat -C POSTROUTING -o eth0 -s ${VPC_CIDR_RANGE} -j MASQUERADE 2&gt; \/dev\/null ||<br \/>\niptables -t nat -A POSTROUTING -o eth0 -s ${VPC_CIDR_RANGE} -j MASQUERADE ) ||<br \/>\ndie<\/p>\n<p>sysctl net.ipv4.ip_forward net.ipv4.conf.eth0.send_redirects | log<br \/>\niptables -n -t nat -L POSTROUTING | log<\/p>\n<p>log &#8220;Configuration of PAT complete.&#8221;<br \/>\nexit 0<\/p>\n<p>I need to run this from something akin to rc3.d\/S99local (add that to my todo list)<\/p>\n<p>I copied this file to \/usr\/local\/sbin\/configure-pat.sh and added it to \/etc\/rc.local (which seems like the place to put it for Ubunut 14.04).<\/p>\n<p>Then I&#8217;m gonna reboot and see if anything works. I may have to configure routes for my subnets in aws???<\/p>\n<p>You know it is at this point, where the configuation ceases to be fun. I started looking more at the commercial version. Given that I have killed an entire afternoon and still have only the server and one client talking to each other, the price of the licenses is starting to look good!<\/p>\n<p>Long term, I&#8217;d like to come back and work on this some more, but for now, it just isn&#8217;t cost effective to spend more time on it&#8230;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Trying OpenVPN on Ubuntu 14.04 EC2 instnace. Warning these are mostly just notes to myself &#8212; use with caution. That includes you, future me&#8230; TLDR version: If it absolutely comes down it it, I think I could eventually make the &hellip;<\/p>\n<p class=\"read-more\"><a href=\"https:\/\/www.pendragonsfolly.com\/?p=19\">Read more &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,4,3],"tags":[],"class_list":["post-19","post","type-post","status-publish","format-standard","hentry","category-aws","category-linux","category-work"],"_links":{"self":[{"href":"https:\/\/www.pendragonsfolly.com\/index.php?rest_route=\/wp\/v2\/posts\/19","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pendragonsfolly.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.pendragonsfolly.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.pendragonsfolly.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pendragonsfolly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=19"}],"version-history":[{"count":11,"href":"https:\/\/www.pendragonsfolly.com\/index.php?rest_route=\/wp\/v2\/posts\/19\/revisions"}],"predecessor-version":[{"id":30,"href":"https:\/\/www.pendragonsfolly.com\/index.php?rest_route=\/wp\/v2\/posts\/19\/revisions\/30"}],"wp:attachment":[{"href":"https:\/\/www.pendragonsfolly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=19"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.pendragonsfolly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=19"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.pendragonsfolly.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=19"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}