__________________________________________ How to install OpenVPN on Raspberry Pi __________________________________________ # by WA8KIM / kim@kim125.com / EchoLink: WA8KIM-R / WiRES-X: Rm#21149 / Last updated 08/24/2015. # http://www.wa8kim.com/rpi.html # Thanks to MANY uncredited contributers. # Best viewed in Notepad with Word Wrap OFF/UNCHECKED and in full screen Download the latest version of this file at: http://www.wa8kim.com/files/(RPi)%20Install%20-%20OpenVPN.txt ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~ BEFORE YOU GET STARTED ~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ To set up a VPN Server using a Raspberry Pi, you will need: - Rasberry Pi (It can be any version: B, B+, 2B) - MicoUSB cord and USB power Cube to power the RPi - MicroSD card: I recommend a 8gb or 16gb card. (If you have the RPi B version, you will need a standard SD card) - CAT5/5e/6 cable to connect the RPi to a router (DSL or cable modem.. doesn't matter) You will need to install the raspberry pi at someone's house. You can set it up at your place, but in order to use your Wires-X PC at home, your WIres-X computer will have to go through the RPi's VPN server... which should work fine. Prepare the RPi. A good starting point is the RPi's main website at: http://www.raspberrypi.org/downloads/ - You will be using the Raspian (DebianWheezy OS) I also created a document to help prepare the Raspberry Pi for this install: http://www.wa8kim.com/(RPi)%20Install%20-%20New%20RPi%20Setup.txt It may take you 2-5 hours to complete the install, depending on your experience. The setup works best when running a static IP on your Raspberry Pi and, for your house, using a dynamic DNS host service, such as dyndns.com, or Google for 'Free Dynamic DNS Services'. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~ MODIFYING THIS .txt FILE FOR YOUR SETTINGS ~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ This text file contains actual config file content that you will be copy-and-pasting to create the config files. - It is important that you closely follow the next instructions (Using Notepad's REPLACE feature). - By changing the text in this file, it will set your config files up correctly. # First, determine your current IP address info and write it down # Log in to your Raspberry Pi and type the following: ifconfig # Write down the , , & info for your desires internet card. (eth0 is the Cat-5 LAN, wlan0 is your Wifi) # is your IP address # is yoru broadcast IP, which is usually just your IP address with a .255 for the number # is your netmask # Your gateway will be displayed using the following command route -n Using Notepad, edit the following 'Config Settings' text in this document using ->Edit->Replace [CTRL-H] to match your system. - You ** MUST USE THE REPLACE feature ** so that it changes all the config files in this .txt document. - You could make a copy of this .txt document for each of your RPi systems if preferred. ------------------------------------------------------------------------------------------- Using Notepad, REPLACE the following Settings: ------------------------------------------------------------------------------------------- On THIS computer, press [CTRL-H] or click on (Edit) and (Replace) - A window titled "Replace" will pop up. Perform all the following replacements. Find what: 192.168.1.40 Replace with: (type your here) Click "Replace All" Find what: your.dyndns.com Replace with: (Type the WAN (external) IP address that your RPi VPN server will be using.) It can be a dynamic address or the actual IP address. Find what: 192.168.1.255 Replace with: (Type your here) Find what: gateway 192.168.1.1 Replace with: gateway (type your gateway address here) Find what: nameserver 192.168.1.1 Replace with: Your DNS Server (Usually your router's address / gateway) --== The following are OPTIONAL and NOT RECOMMENDED unless you know what you're doing ==-- - The default port/protocol is UDP 1 1 9 4. Some prefer to use TCP 443 because it is not usually blocked by work or cell phones. Find what: proto udp Replace with: proto tcp (ONLY IF NEEDED!!!) Find what: 1194 Replace with: 443 (or any other port you choose) ------------------------------------------------------------------------------------------- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~ Creating A Static IP Address ~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # You will need to assign a static IP address to your RPi. # Modify the /etc/network/interfaces file as below, substituting your IP addresses (or copy-and-paste from this .txt file) # eth0 is your LAN adapter. wlan0 is your Wifi adapter (even if you don't have one installed). Modify the settings for the appropriate adapter. # Make sure you comment out (place a #) before the 'iface eth0 inet dhcp' line sudo nano /etc/network/interfaces --------------------------------------------------- auto lo iface lo inet loopback #iface eth0 inet dhcp iface eth0 inet static address 192.168.1.40 broadcast 192.168.1.255 gateway 192.168.1.1 allow-hotplug wlan0 iface wlan0 inet manual wpa-roam /etc/wpa_supplicant/wpa_supplicant.conf iface default inet dhcp --------------------------------------------------- # To save the file, press [CTRL-X], then [Y], then [ENTER] # The following sets the static DNS server (192.168.1.1 is your router, the 8.8.8.8 is Google's free DNS server) sudo nano /etc/resolv.conf --------------------------------------------------- nameserver 192.168.1.1 nameserver 8.8.8.8 --------------------------------------------------- # Reboot sudo reboot ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~ Installing PuTTY ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ --== You MUST use PuTTY or some other SSH program to access your RPi. You will NOT be able to simply type everything ==-- - Download and install PuTTY on your Windows PC. PuTTY allows you to control the RPi with your PC so that you can cut & paste commands: http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html - Run PuTTY Hostname is your RPi's IP address: 192.168.1.40 Use SSH port 22 The default login is: pi The default password is: raspberry (or use your password if you changed the deafult one) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~ Installing OpenVPN ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ --== These instructions assume that you have Raspian already installed. See my 'Install - New RPi Setup.txt' for help perparing your RPi ==-- --== This install could take several hours to complete. I hope you made a fresh pot of coffee! ==-- - PuTTY Tips: - To paste into PuTTY, just Rt-Click your mouse - Whenever you reboot the RPi, you will need to restart the session by Rt-Clicking at the top of the PuTTY Window & select "Restart Session" - It is helpful to widen the PuTTY screen or go full screen in PuTTY. Content will look much better. - Use the UP and DOWN arrows to scroll through the latest commands typed into the RPi (actually it's a Linux feature, not PuTTY) # Download and install OpenVPN sudo apt-get install openvpn # Change to 'root' (admin) privileges. If you have to reboot, you will have to re-enter the sudo -s command. sudo -s # Copy the sample security key info into the usable directory sudo cp -r /usr/share/doc/openvpn/examples/easy-rsa/2.0/ /etc/openvpn/easy-rsa cd /etc/openvpn/easy-rsa # Vars is a script written to help create the security keys # Nano is RPi's version of Notepad sudo nano vars ------------------------------------------------------------------------------------------- # Change Line 13 to: export EASY_RSA="/etc/openvpn/easy-rsa" ------------------------------------------------------------------------------------------- # To enable packet forwarding, uncomment the following line within the sysctl.conf file: sudo nano /etc/sysctl.conf ------------------------------------------------------------------------------------------- net.ipv4.ip_forward=1 ------------------------------------------------------------------------------------------- # Run the vars script source ./vars # ./clean-all deletes the /etc/easy-rsa/keys folder, if it existed. # If there were no previous keys (such as the first time it's run), ./clean-all will display an error. It's OK. ./clean-all # Create the .ca key file (one of three security fiels to create a complete security key) # Leave all entries blank. Just keep hitting [ENTER] until it is finished. # Never enter a 'challenge password'... always leave it blank ./build-ca # 'build-key-server' is a script used to create the security key for for the server # You can change the server name (the second 'server' below), but it will require more editing of the config files. For now, leave it as is. # Leave all entries blank. When asked, press "Y" to accept 'sign the certificate' and "Y again to commit. ./build-key-server server # Now to build your client keys. You can make as many as you want. The following will create three clients named wires, client1, & client 2 # You can change client1 and client2 to any names you want, but it will require more editing of this file later. For now, leave them as is. # Enter a password under the 'Enter PEM pass phrase". # Leave all other lines blank EXCEPT email: you must enter a unique email for each client. It can be a fake email address. # The 'wires' client is necessary if you want to use this server to forward ports to a Wires-X computer. ./build-key-pass wires ./build-key-pass client1 ./build-key-pass client2 # Change to the keys directory cd keys # The following creates higher security level keys which require a password. # Enter your password three times for each key openssl rsa -in wires.key -des3 -out wires.3des.key openssl rsa -in client1.key -des3 -out client1.3des.key openssl rsa -in client2.key -des3 -out client2.3des.key # Change back to the easy-rsa directory cd /etc/openvpn/easy-rsa/ # build-dh is a script that creates another security key level # It may take 4-5 minutes to complete ./build-dh # The following creates the .ta key (another of the security keys!) openvpn --genkey --secret keys/ta.key # Now to create the static IP file for the 'wires' client # All static IP files are saved in the /etc/openvpn/ccd, so let's make that directory mkdir /etc/openvpn/ccd # The 'wires' file doesn't exist, but nano (RPi's version of Notepad) will create it. # The following will create a blank file named 'wires' # Copy-and-paste all the content between the long lines of -------'s from this text file into the RPi's nano text editor. sudo nano /etc/openvpn/ccd/wires ------------------------------------------------------------------------------------------- ifconfig-push 10.8.0.10 10.8.0.9 ------------------------------------------------------------------------------------------- # The server.conf file is the main config file. It doesn't exist, but, again, nano will create it. # Copy-and-paste all the content between the long lines of -------'s from this text file into the RPi's nano text editor. # Most of the lines are not needed, but I left all the comments in for you to tweak later if desired. nano /etc/openvpn/server.conf ------------------------------------------------------------------------------------------- ################################################# # Sample OpenVPN 2.0 config file for # # multi-client server. # # # # This file is for the server side # # of a many-clients <-> one-server # # OpenVPN configuration. # # # # OpenVPN also supports # # single-machine <-> single-machine # # configurations (See the Examples page # # on the web site for more info). # # # # This config should work on Windows # # or Linux/BSD systems. Remember on # # Windows to quote pathnames and use # # double backslashes, e.g.: # # "C:\\Program Files\\OpenVPN\\config\\foo.key" # # # # Comments are preceded with '#' or ';' # ################################################# # Which local IP address should OpenVPN # listen on? (optional) ;local 192.168.1.40 # Which TCP/UDP port should OpenVPN listen on? # If you want to run multiple OpenVPN instances # on the same machine, use a different port # number for each one. You will need to # open up this port on your firewall. port 1194 # TCP or UDP server? ;proto tcp proto udp # "dev tun" will create a routed IP tunnel, # "dev tap" will create an ethernet tunnel. # Use "dev tap0" if you are ethernet bridging # and have precreated a tap0 virtual interface # and bridged it with your ethernet interface. # If you want to control access policies # over the VPN, you must create firewall # rules for the the TUN/TAP interface. # On non-Windows systems, you can give # an explicit unit number, such as tun0. # On Windows, use "dev-node" for this. # On most systems, the VPN will not function # unless you partially or fully disable # the firewall for the TUN/TAP interface. ;dev tap dev tun # Windows needs the TAP-Win32 adapter name # from the Network Connections panel if you # have more than one. On XP SP2 or higher, # you may need to selectively disable the # Windows firewall for the TAP adapter. # Non-Windows systems usually don't need this. ;dev-node MyTap # SSL/TLS root certificate (ca), certificate # (cert), and private key (key). Each client # and the server must have their own cert and # key file. The server and all clients will # use the same ca file. # # See the "easy-rsa" directory for a series # of scripts for generating RSA certificates # and private keys. Remember to use # a unique Common Name for the server # and each of the client certificates. # # Any X509 key management system can be used. # OpenVPN can also use a PKCS #12 formatted key file # (see "pkcs12" directive in man page). ca /etc/openvpn/easy-rsa/keys/ca.crt cert /etc/openvpn/easy-rsa/keys/server.crt key /etc/openvpn/easy-rsa/keys/server.key # This file should be kept secret # Diffie hellman parameters. # Generate your own with: # openssl dhparam -out dh1024.pem 1024 # Substitute 2048 for 1024 if you are using # 2048 bit keys. dh /etc/openvpn/easy-rsa/keys/dh1024.pem # Configure server mode and supply a VPN subnet # for OpenVPN to draw client addresses from. # The server will take 10.8.0.1 for itself, # the rest will be made available to clients. # Each client will be able to reach the server # on 10.8.0.1. Comment this line out if you are # ethernet bridging. See the man page for more info. server 10.8.0.0 255.255.255.0 # Maintain a record of client <-> virtual IP address # associations in this file. If OpenVPN goes down or # is restarted, reconnecting clients can be assigned # the same virtual IP address from the pool that was # previously assigned. ifconfig-pool-persist /etc/openvpn/ipp.txt ifconfig 10.8.0.1 10.8.0.2 ;push "route 10.8.0.1 255.255.255.255" ;push "route 10.8.0.0 255.255.255.0" ;push "route 192.168.1.40 255.255.255.0" # Configure server mode for ethernet bridging. # You must first use your OS's bridging capability # to bridge the TAP interface with the ethernet # NIC interface. Then you must manually set the # IP/netmask on the bridge interface, here we # assume 10.8.0.4/255.255.255.0. Finally we # must set aside an IP range in this subnet # (start=10.8.0.50 end=10.8.0.100) to allocate # to connecting clients. Leave this line commented # out unless you are ethernet bridging. ;server-bridge 10.8.0.4 255.255.255.0 10.8.0.50 10.8.0.100 # Configure server mode for ethernet bridging # using a DHCP-proxy, where clients talk # to the OpenVPN server-side DHCP server # to receive their IP address allocation # and DNS server addresses. You must first use # your OS's bridging capability to bridge the TAP # interface with the ethernet NIC interface. # Note: this mode only works on clients (such as # Windows), where the client-side TAP adapter is # bound to a DHCP client. ;server-bridge # Push routes to the client to allow it # to reach other private subnets behind # the server. Remember that these # private subnets will also need # to know to route the OpenVPN client # address pool (10.8.0.0/255.255.255.0) # back to the OpenVPN server. ;push "route 192.168.10.0 255.255.255.0" ;push "route 192.168.20.0 255.255.255.0" # To assign specific IP addresses to specific # clients or if a connecting client has a private # subnet behind it that should also have VPN access, # use the subdirectory "ccd" for client-specific # configuration files (see man page for more info). # EXAMPLE: Suppose the client # having the certificate common name "Thelonious" # also has a small subnet behind his connecting # machine, such as 192.168.40.128/255.255.255.248. # First, uncomment out these lines: ;client-config-dir ccd ;route 192.168.40.128 255.255.255.248 # Then create a file ccd/Thelonious with this line: # iroute 192.168.40.128 255.255.255.248 # This will allow Thelonious' private subnet to # access the VPN. This example will only work # if you are routing, not bridging, i.e. you are # using "dev tun" and "server" directives. # EXAMPLE: Suppose you want to give # Thelonious a fixed VPN IP address of 10.9.0.1. # First uncomment out these lines: ;client-config-dir ccd ;route 10.8.0.0 255.255.255.252 # Then add this line to ccd/Thelonious: # ifconfig-push 10.9.0.1 10.9.0.2 # Suppose that you want to enable different # firewall access policies for different groups # of clients. There are two methods: # (1) Run multiple OpenVPN daemons, one for each # group, and firewall the TUN/TAP interface # for each group/daemon appropriately. # (2) (Advanced) Create a script to dynamically # modify the firewall in response to access # from different clients. See man # page for more info on learn-address script. ;learn-address ./script # If enabled, this directive will configure # all clients to redirect their default # network gateway through the VPN, causing # all IP traffic such as web browsing and # and DNS lookups to go through the VPN # (The OpenVPN server machine may need to NAT # or bridge the TUN/TAP interface to the internet # in order for this to work properly). push "redirect-gateway def1 bypass-dhcp" # push "redirect-gateway def1" # Certain Windows-specific network settings # can be pushed to clients, such as DNS # or WINS server addresses. CAVEAT: # http://openvpn.net/faq.html#dhcpcaveats # The addresses below refer to the public # DNS servers provided by opendns.com. push "dhcp-option DNS 10.8.0.1" # Uncomment this directive to allow different # clients to be able to "see" each other. # By default, clients will only see the server. # To force clients to only see the server, you # will also need to appropriately firewall the # server's TUN/TAP interface. client-to-client # Uncomment this directive if multiple clients # might connect with the same certificate/key # files or common names. This is recommended # only for testing purposes. For production use, # each client should have its own certificate/key # pair. # # IF YOU HAVE NOT GENERATED INDIVIDUAL # CERTIFICATE/KEY PAIRS FOR EACH CLIENT, # EACH HAVING ITS OWN UNIQUE "COMMON NAME", # UNCOMMENT THIS LINE OUT. ;duplicate-cn # The keepalive directive causes ping-like # messages to be sent back and forth over # the link so that each side knows when # the other side has gone down. # Ping every 10 seconds, assume that remote # peer is down if no ping received during # a 120 second time period. keepalive 10 120 # For extra security beyond that provided # by SSL/TLS, create an "HMAC firewall" # to help block DoS attacks and UDP port flooding. # # Generate with: # openvpn --genkey --secret ta.key # # The server and each client must have # a copy of this key. # The second parameter should be '0' # on the server and '1' on the clients. tls-auth /etc/openvpn/easy-rsa/keys/ta.key 0 # This file is secret # Select a cryptographic cipher. # This config item must be copied to # the client config file as well. ;cipher BF-CBC # Blowfish (default) cipher AES-128-CBC # AES ;cipher DES-EDE3-CBC # Triple-DES # Enable compression on the VPN link. # If you enable it here, you must also # enable it in the client config file. comp-lzo # The maximum number of concurrently connected # clients we want to allow. ;max-clients 100 # It's a good idea to reduce the OpenVPN # daemon's privileges after initialization. # # You can uncomment this out on # non-Windows systems. user root group root # The persist options will try to avoid # accessing certain resources on restart # that may no longer be accessible because # of the privilege downgrade. persist-key persist-tun # Output a short status file showing # current connections, truncated # and rewritten every minute. status openvpn-status.log # By default, log messages will go to the syslog (or # on Windows, if running as a service, they will go to # the "\Program Files\OpenVPN\log" directory). # Use log or log-append to override this default. # "log" will truncate the log file on OpenVPN startup, # while "log-append" will append to it. Use one # or the other (but not both). log /var/log/openvpn.log ;log-append openvpn.log # Set the appropriate level of log # file verbosity. # # 0 is silent, except for fatal errors # 4 is reasonable for general usage # 5 and 6 can help to debug connection problems # 9 is extremely verbose verb 4 # WA8KIM: This section added to allow port forwarding up /etc/openvpn/masquerade.sh client-connect /etc/openvpn/clientconnect.sh client-disconnect /etc/openvpn/clientdisconnect.sh script-security 2 # WA8KIM: This section added to assign a static IP to users # so that the 6 Wires-X ports will only be forwarded to the wires user. up /etc/openvpn/wiresports.sh client-config-dir ccd # Silence repeating messages. At most 20 # sequential messages of the same message # category will be output to the log. mute 20 ------------------------------------------------------------------------------------------- # The following creates .sh script files (similar to .bat files in MSDOS) # The masquerade.sh script will be executed whenever OpenVPN is started because of the 'up /etc/openvpn/masquerade.sh' line in the server.conf file. # Running the masquerade.sh script is comparable to Internet Connection Sharing in Windows # Again, the following will create a blank file named masquerade.sh. # Copy-and-paste all the content between the long lines of -------'s sudo nano /etc/openvpn/masquerade.sh ------------------------------------------------------------------------------------------- #!/bin/bash echo Initiating masquerade.sh . . . sudo /sbin/iptables -t nat -A POSTROUTING -o tun+ -s 192.168.1.40 -j MASQUERADE ------------------------------------------------------------------------------------------- # chmod +x will make the masquerade.sh file executable. sudo chmod +x /etc/openvpn/masquerade.sh # The following wiresports.sh file will forward all six ports necessary for Wires-X to the 'wires' client. # The Wires-X ports will only be forwarded to the 'wires' client, allowing you to have multiple non-Wires-X clients. # The wiresports.sh script will be executed whenever OpenVPN is started because of the 'up /etc/openvpn/wiresports.sh' in the server.conf file.. sudo nano /etc/openvpn/wiresports.sh ------------------------------------------------------------------------------------------- #!/bin/bash myvariable=$(whoami) client_ip=10.8.0.10 PUBLIC_IP=192.168.1.40 echo .. wiresports.sh Initiated .. Client IP:$client_ip .. Public IP:$PUBLIC_IP .. Login: $myvariable .. sudo /sbin/iptables -t nat -A PREROUTING -i eth0 -p udp -d $PUBLIC_IP --dport 46100 -j DNAT --to-destination $client_ip:46100 sudo /sbin/iptables -t nat -A PREROUTING -i eth0 -p udp -d $PUBLIC_IP --dport 46110 -j DNAT --to-destination $client_ip:46110 sudo /sbin/iptables -t nat -A PREROUTING -i eth0 -p udp -d $PUBLIC_IP --dport 46112 -j DNAT --to-destination $client_ip:46112 sudo /sbin/iptables -t nat -A PREROUTING -i eth0 -p udp -d $PUBLIC_IP --dport 46114 -j DNAT --to-destination $client_ip:46114 sudo /sbin/iptables -t nat -A PREROUTING -i eth0 -p udp -d $PUBLIC_IP --dport 46120 -j DNAT --to-destination $client_ip:46120 sudo /sbin/iptables -t nat -A PREROUTING -i eth0 -p udp -d $PUBLIC_IP --dport 46122 -j DNAT --to-destination $client_ip:46122 ------------------------------------------------------------------------------------------- # chmod +x will make the wiresports.sh file executable. sudo chmod +x /etc/openvpn/wiresports.sh # The clientconnect.sh is executed whenever a client connects to the server. # It is included here only as an example of port forwarding to connected clients. # You can change the port numbers to any ports that you want forwarded, just don't forget to uncomment (remove the #) before the corresponding sudo command. # You can insert your Echolink, IRLP, D-Star, etc. ports below for a multi-purpose server. # The 'nat -A" adds the port forwarding rule when someone connnects. Later, we'll delete the port forwarding rule when someone disconnects. # It will only port forward to the last connected client. Ex: When client2 connects, client1 will stop getting port forwarded packets. sudo nano /etc/openvpn/clientconnect.sh ------------------------------------------------------------------------------------------- #!/bin/bash myvariable=$(whoami) PUBLIC_IP=192.168.1.40 echo Client Connect Initiated .. Tunnel IP:$ifconfig_pool_remote_ip .. Public IP:$PUBLIC_IP .. Login: $myvariable protocol=udp #protocol=tcp port1=46100 port2=46110 port3=46112 port4=46114 port5=46120 port6=46122 #sudo /sbin/iptables -t nat -A PREROUTING -i eth0 -p $protocol -d $PUBLIC_IP --dport $port1 -j DNAT --to-destination $ifconfig_pool_remote_ip:$port1 #sudo /sbin/iptables -t nat -A PREROUTING -i eth0 -p $protocol -d $PUBLIC_IP --dport $port2 -j DNAT --to-destination $ifconfig_pool_remote_ip:$port2 #sudo /sbin/iptables -t nat -A PREROUTING -i eth0 -p $protocol -d $PUBLIC_IP --dport $port3 -j DNAT --to-destination $ifconfig_pool_remote_ip:$port3 #sudo /sbin/iptables -t nat -A PREROUTING -i eth0 -p $protocol -d $PUBLIC_IP --dport $port4 -j DNAT --to-destination $ifconfig_pool_remote_ip:$port4 #sudo /sbin/iptables -t nat -A PREROUTING -i eth0 -p $protocol -d $PUBLIC_IP --dport $port5 -j DNAT --to-destination $ifconfig_pool_remote_ip:$port5 #sudo /sbin/iptables -t nat -A PREROUTING -i eth0 -p $protocol -d $PUBLIC_IP --dport $port6 -j DNAT --to-destination $ifconfig_pool_remote_ip:$port6 ------------------------------------------------------------------------------------------- sudo chmod +x /etc/openvpn/clientconnect.sh # The clientdisconnect.sh is executed whenever a client disconnects from the server. # As stated earlier, the 'nat -D' below will delete the port forwarding rule. # If you added ports in the clientconnect.sh script earlier, don't forget to change the port number and uncomment (remove the #) the corresponding sudo command. sudo nano /etc/openvpn/clientdisconnect.sh ------------------------------------------------------------------------------------------- #!/bin/bash myvariable=$(whoami) PUBLIC_IP=192.168.1.40 echo Client DISConnect Initiated .. Tunnel IP:$ifconfig_pool_remote_ip .. Public IP:$PUBLIC_IP .. Login: $myvariable protocol=udp #protocol=tcp port1=46100 port2=46110 port3=46112 port4=46114 port5=46120 port6=46122 #sudo /sbin/iptables -t nat -D PREROUTING -i eth0 -p $protocol -d $PUBLIC_IP --dport $port1 -j DNAT --to-destination $ifconfig_pool_remote_ip:$port1 #sudo /sbin/iptables -t nat -D PREROUTING -i eth0 -p $protocol -d $PUBLIC_IP --dport $port2 -j DNAT --to-destination $ifconfig_pool_remote_ip:$port2 #sudo /sbin/iptables -t nat -D PREROUTING -i eth0 -p $protocol -d $PUBLIC_IP --dport $port3 -j DNAT --to-destination $ifconfig_pool_remote_ip:$port3 #sudo /sbin/iptables -t nat -D PREROUTING -i eth0 -p $protocol -d $PUBLIC_IP --dport $port4 -j DNAT --to-destination $ifconfig_pool_remote_ip:$port4 #sudo /sbin/iptables -t nat -D PREROUTING -i eth0 -p $protocol -d $PUBLIC_IP --dport $port5 -j DNAT --to-destination $ifconfig_pool_remote_ip:$port5 #sudo /sbin/iptables -t nat -D PREROUTING -i eth0 -p $protocol -d $PUBLIC_IP --dport $port6 -j DNAT --to-destination $ifconfig_pool_remote_ip:$port6 ------------------------------------------------------------------------------------------- sudo chmod +x /etc/openvpn/clientdisconnect.sh # The firewall-openvpn-rules.sh script will allow OpenVPN to get through RPi's firewall # (We already added the 'preup /etc/firewall-openvpn-riles.sh' to the /etc/network/interface file at the beginning) sudo nano /etc/firewall-openvpn-rules.sh ------------------------------------------------------------------------------------------- #!/bin/sh iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -o eth0 -j SNAT --to-source 192.168.1.40 ------------------------------------------------------------------------------------------- # chmod 700 makes it executable sudo chmod 700 /etc/firewall-openvpn-rules.sh # 'chown root' gives the 'root' user (admin) priviledges to the script sudo chown root /etc/firewall-openvpn-rules.sh # Add the 'pre-up /etc/firewall-openvpn-rules.sh' line as shown below. This will cause it to be run at boot up. sudo nano /etc/network/interfaces --------------------------------------------------- #iface eth0 inet dhcp iface eth0 inet static address 192.168.1.40 broadcast 192.168.1.255 gateway 192.168.1.1 pre-up /etc/firewall-openvpn-rules.sh --------------------------------------------------- # The following sets the static DNS server (192.168.1.1 is your router, the 8.8.8.8 is Google's free DNS server) sudo nano /etc/resolv.conf ------------------------------------------------------------------------------------------- nameserver 192.168.1.1 nameserver 8.8.8.8 ------------------------------------------------------------------------------------------- # The following is a small script I created to view the OpenVPN log file by simply typing 'sudo vpn' # By placing it in the /usr/local/bin folder, it can be executed from any folder. # The line '#!/bin/bash' at the beginning tells the RPi what program to run the script (the file) with. BASH is the program most often used. # The 'cat' command simply displays the content of the file sudo nano /usr/local/bin/vpn ------------------------------------------------------------------------------------------- #!/bin/bash sudo cat /var/log/openvpn.log ------------------------------------------------------------------------------------------- # Make it executable sudo chmod +x /usr/local/bin/vpn # Let's reboot so that the new settings take effect. sudo reboot # After 1 minute, restart your PuTTY session and log in to your RPi # Display the log file and look for any errors. sudo vpn # To verify it is running, type the following command. It will show all the ports that your RPi is listening on. # you should see openvpn on the right. The PID is the program's unique ID. It will vary with each reboot. sudo netstat -ltnup --== Now you need to access the internet router that your RPi VPN Server will be running at and port forward the Wires-X UDP ports 46100, 46110, 46112, 46114, 46120, 46122 to your RPi's LAN IP address. You will need to port forward UDP port 1194 so that you can connect to your OPenVPN server. Finally, you may want to port forward TCP port 22 SSH so that you can work on the RPi over the internet (or change to a more secure port). ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~ You are almost done! The server should now be fully up and running. You just need to create the client computers' config files.~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # I placed 'sudo' in front of most commands so that I can copy-and-paste from this txt file later to make changes. # By typing 'sudo -s', you don't need to type sudo in front of the commands (except the 'sudo' lines pasted into scripts: those ARE needed) sudo -s # The next section creates the .ovpn config files which will be copied onto the clients' computers. # By default, OpenVPN clients needs 4 files to be copied onto their computers, but we'll be combining all 4 files into one. # The following two scripts (Default.txt and MakeOVPN.sh) combine the 4 files into one .ovpn file for each client. # Pay attention to capitalization. If not correctly capitalized, the scripts won't work. Copy-And-Paste solves this problem! # As usual, copy-and-paste the lines between then '-------'s from this text file into the RPi's editor. # Default.txt creates the header for the clients' .ovpn config files. sudo nano /etc/openvpn/easy-rsa/keys/Default.txt ------------------------------------------------------------------------------------------- client dev tun proto udp remote your.dyndns.com 1194 resolv-retry infinite nobind persist-key persist-tun mute-replay-warnings ns-cert-type server key-direction 1 cipher AES-128-CBC comp-lzo verb 1 mute 20 ------------------------------------------------------------------------------------------- # Copy-and-paste as usual sudo nano /etc/openvpn/easy-rsa/keys/MakeOVPN.sh ------------------------------------------------------------------------------------------- #!/bin/bash # Default Variable Declarations DEFAULT="Default.txt" FILEEXT=".ovpn" CRT=".crt" KEY=".3des.key" CA="ca.crt" TA="ta.key" #Ask for a Client name echo "Please enter an existing Client Name:" read NAME #1st Verify that client’s Public Key Exists if [ ! -f $NAME$CRT ]; then echo "[ERROR]: Client Public Key Certificate not found: $NAME$CRT" exit fi echo "Client’s cert found: $NAME$CR" #Then, verify that there is a private key for that client if [ ! -f $NAME$KEY ]; then echo "[ERROR]: Client 3des Private Key not found: $NAME$KEY" exit fi echo "Client’s Private Key found: $NAME$KEY" #Confirm the CA public key exists if [ ! -f $CA ]; then echo "[ERROR]: CA Public Key not found: $CA" exit fi echo "CA public Key found: $CA" #Confirm the tls-auth ta key file exists if [ ! -f $TA ]; then echo "[ERROR]: tls-auth Key not found: $TA" exit fi echo "tls-auth Private Key found: $TA" #Ready to make a new .opvn file - Start by populating with the default file cat $DEFAULT > $NAME$FILEEXT #Now, append the CA Public Cert echo "" >> $NAME$FILEEXT cat $CA >> $NAME$FILEEXT echo "" >> $NAME$FILEEXT #Next append the client Public Cert echo "" >> $NAME$FILEEXT cat $NAME$CRT | sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p' >> $NAME$FILEEXT echo "" >> $NAME$FILEEXT #Then, append the client Private Key echo "" >> $NAME$FILEEXT cat $NAME$KEY >> $NAME$FILEEXT echo "" >> $NAME$FILEEXT #Finally, append the TA Private Key echo "" >> $NAME$FILEEXT cat $TA >> $NAME$FILEEXT echo "" >> $NAME$FILEEXT echo "Done! $NAME$FILEEXT Successfully Created." #Script written by Eric Jodoin ------------------------------------------------------------------------------------------- cd /etc/openvpn/easy-rsa/keys/ # The following makes the MakeOVPN.sh file executable sudo chmod 700 MakeOVPN.sh # MakeOVPN.sh adds the content from Default.txt and the three other server security key files for each client # Execute MakeOVPN.sh # Enter the first client's name, such as wires. Then re-run the program for each client, such as client1, then client2, etc. ./MakeOVPN.sh # The .ovpn files are now created. Move the .ovpn files to /home/pi so that they are accessible by WinSCP (more info on WinSCP to follow!) because the /etc/openvpn/easy-rsa-keys folder is a protected folder sudo mv /etc/openvpn/easy-rsa/keys/*.ovpn /home/pi ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ The following steps are done on your Windows (or main) computer.. NOT your RPi ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - Download and install OpenVPN onto your client computer from: https://openvpn.net/index.php/open-source/downloads.html - OpenVPN GUI will create a little icon in the bottom right of your screen. - Open you favorite browser (internet Explorer, Chrome, Firefox, etc.) and go to the webpage: http://www.whatismyip.com/ - You should see your WAN IP address. # Now we need to move the .ovpn files from the RPi to your client computers. # The easiest I know how to do this is to use WinSCP, which allows to copy and paste files from your RPi folders. - Download, install, and run WinSCP on your main computer (the one you're viewing this file with) http://winscp.net/eng/download.php - File Protocol is SFTP - Hostname: 192.168.1.40 - User name: pi - Password: raspberry (or whatever password you may have changed it to) Click [LOGIN] - On the left side of the screen, switch to the 'C:\Program Files\OpenVPN\config' folder (or whatever folder you wish to copy the config files to) - The right side of the screen displays your RPi files in the /home/pi folder. - Highlight all the .ovpn files and click [Download] at the top, or press [F5] - Be careful with the .ovpn files. If anyone gets ahold of them, they will be able to access your RPi's network. - Delete all the .ovpn files from your RPi for safety reasons. To connect to your RPi's OpenVPN server, simply right-click on the OpenVPN GUI and select 'wires' (or any other client) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ You will not be able to connect to your VPN server if your Raspberry Pi is not at its destination WAN IP address. Also, many routers will not allow you to use a dyndns address to connect to your house when you are home -example: If I use wa8kim.dyndns.com as my WAN address, I can only connect when I'm NOT home. To connect to my OpenVPN server when I'm home, I have to change the wa8kim.dyndns.com to 192.168.1.40 (my RPi's LAN address) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - After a little while, the icon should turn green and you are now connected to your RPi's OpenVPN server. CONGRATS!!! - Go back to your favorite browser (internet Explorer, Chrome, Firefox, etc.) and go to the webpage: http://www.whatismyip.com/ - If your Raspberry Pi is at a different location, you will see that your IP is now your RPi's WAN IP address. ~~~~~~~~~~~~~~~~~~~~~~~~ CONGRATS AND ENJOY ! ! ! ~~~~~~~~~~~~~~~~~~~~~~~~ -------------------- REVISION HISTORY -------------------- 2015-08-24 Updated Download Link 2015-03-10 File Created