Skip to content

Instantly share code, notes, and snippets.

@jhenkens
Last active June 23, 2022 11:35
Show Gist options
  • Save jhenkens/11190151 to your computer and use it in GitHub Desktop.
Save jhenkens/11190151 to your computer and use it in GitHub Desktop.
ServerPasswordSet ADMINPASSWORD
HubCreate VPN /PASSWORD:
hubdelete default
Hub VPN
UserCreate USERNAME /GROUP:none /REALNAME:none /NOTE:none
UserPasswordSet USERNAME /PASSWORD:TESTPASS
OpenVPNEnable no /PORTS:1194
sstpenable no
SecureNatDisable
natdisable
dhcpdisable
bridgecreate VPN /DEVICE:soft /TAP:yes
ipsecenable /L2TP:yes /L2TPRAW:no /ETHERIP:no /PSK:TESTSECRET /DEFAULTHUB:VPN
keepdisable
vpnovericmpdnsenable /ICMP:no /DNS:no
listenerdelete 1194
listenerdelete 5555
listenerdelete 992
ServerCipherset AES256-SHA
flush
exit
auto lo
iface lo inet loopback
auto br0
iface br0 inet dhcp
bridge_ports eth0
bridge_stp off
bridge_waitport 0
bridge_maxwait 0
bridge_fd 0
post-up ip link set br0 address MACADDRESS
allow-hotplug eth0
iface eth0 inet manual
#!/bin/bash
iptables --table nat --append POSTROUTING --jump MASQUERADE
echo "net.ipv4.ip_forward = 1" | tee -a /etc/sysctl.conf
echo "net.ipv4.conf.all.accept_redirects = 0" | tee -a /etc/sysctl.conf
echo "net.ipv4.conf.all.send_redirects = 0" | tee -a /etc/sysctl.conf
for vpn in /proc/sys/net/ipv4/conf/*; do echo 0 > $vpn/accept_redirects; echo 0 > $vpn/send_redirects; done
sysctl -p
echo "
for vpn in /proc/sys/net/ipv4/conf/*; do echo 0 > \$vpn/accept_redirects; echo 0 > \$vpn/send_redirects; done
iptables -A FORWARD -i br0 -o br0 -m state --state RELATED,ESTABLISHED -j ACCEPT
iptables -A FORWARD -i br0 -o br0 -j ACCEPT" >> /etc/rc.local
[L2TPVPN]
title=Johan's L2TP
description=Dis is a VPN
ports=500,1701,4500/udp|1701,1723/tcp

Install raspbian, set up your users however you would like, so long as you have sudo access on the user you are running this with. You probably want to resize the image so it fills the SD card as well.

  1. Copy this entire gist to your raspberry pi

  2. Do chmod +x step1.sh step2.sh iptables.sh in the gist folder (so that

  3. Run step1.sh a) This script does a few things - it first updates your raspberry pi, then it installs a few needed utilities, then it upgrades the firmware on your raspberry pi b) Next, it sets up a few iptables rules in /etc/rc.local. Please verify that there is not an 'exit 0' statement before those rules are run, as the 'exit 0' will obviously prevent them from being loaded c) It downloads softether for raspberry pi, extracts it to /usr/local/vpnserver, agrees to the license terms, and then "makes" it. d) It changes the permissions as necessary on the /usr/local/vpnserevr files e) It adds the ufw profile for l2tpvpn's and allows it (But it doesn't enable ufw if it isn't already enabled) f) It adds the init.d script to start up the vpn server on startup

    A) Please enable ufw if you would like your pi to be more secure. The IPTables and ufw rules should allow you to simple type 'sudo ufw enable' and everything should work B) Please check /etc/rc.local to ensure that 'exit 0' does not come before the iptables commands C) Unless you are confident in managing the services manually, please reboot your pi to make sure everything is working nicely.

  4. Run step2.sh a) This script sets up the softether VPN config for an L2TP/IPSec vpn b) It will ask you various things in order and then stream-edit them into a copy of the 'commands.in' file, which it will then use to configure the softether server with. c) This script only works with a clean-install of softether - if you have already placed a password on softether, it will not work d) The soft-ether admin password is used only to administer the server using the vpncmd program or the softether gui for Windows e) The IPSec secret is the shared secret for all the IPSec connection f) The User/Pass are for one specific account, and do not have to match your unix account g) The script also grabs the mac address of the Raspberry Pi's 'eth0' interface, and then sets up the /etc/network/interfaces to use a bridge with that mac address rather than the ethernet adapter direction 1) This is because we have softether binding to a TAP, which we then need to bridge together, so that we can access the raspberry pi via the vpn. If we did a local bridge directly onto eth0, we would be able to access everything but the raspberry pi when connected via VPN

    A) Please check /etc/network/interfaces to make sure things look good! You shouldn't see MACADDRESS in the file, and you should see it making a bridge, and that eth0 is set to manual with no additional settings

Lastly, which you must do on your own,

Set up a dhcp reservation for your Raspberry Pi in your router (essentially a DHCP assigned static IP). If you cannot do this, you may want to mess around with the bridge settings and assign the pi a static IP that way. Then, you need to forward ports 1701tcp, 4500udp, and 500udp from your router to your Raspberry Pi

That should be it! Feel free to message me if you have issues, but only if you are using a raspberrypi and raspbian. I may or may not be able to get back to you. If you make modifications to these scripts to work for other OS's, it might be nice if you shared them with us. If you have any tips on how I should improve my PI's, let me know!

#! /bin/bash
DOWNLOADURL="http://www.softether-download.com/files/softether/v4.08-9449-rtm-2014.06.08-tree/Linux/SoftEther%20VPN%20Server/32bit%20-%20ARM%20EABI/softether-vpnserver-v4.08-9449-rtm-2014.06.08-linux-arm_eabi-32bit.tar.gz"
sudo apt-get update
sudo apt-get dist-upgrade -y
sudo apt-get install vim git-core ufw moreutils lsof rpi-update build-essential curl bridge-utils -y
sudo rpi-update
sudo ./iptables.sh
curl $DOWNLOADURL -s | sudo tar xzvf - -C /usr/local/
pushd .
cd /usr/local/vpnserver
echo "1
1
1
1
" | sudo make
popd
sudo chmod 755 /usr/local/vpnserver
sudo chmod 600 /usr/local/vpnserver/*
sudo chmod 700 /usr/local/vpnserver/vpncmd
sudo chmod 700 /usr/local/vpnserver/vpnserver
sudo cp l2tpvpn /etc/ufw/applications.d/
sudo chmod 644 /etc/ufw/applications.d/l2tpvpn
sudo ufw allow L2TPVPN
sudo ufw allow ssh
sudo cp vpnserver /etc/init.d/
sudo chmod 755 /etc/init.d/vpnserver
sudo update-rc.d vpnserver defaults
echo "Please enable UFW if you would like to be more secure (sudo ufw enable)"
echo "Please check /etc/rc.local. If 'exit 0' occurs before the iptables rules,
please edit so that 'exit 0' is after."
echo "ATTENTION: Please restart before next step to ensure new raspberry pi
firmware is loaded and vpn server starts"
#!/bin/bash
HOSTNAME=`hostname`
CONFIGBASEFILENAME=commands.in
NETWORKBASEFILENAME=interfaces
mkdir .vpnsetuptemp
cp $CONFIGBASEFILENAME $NETWORKBASEFILENAME .vpnsetuptemp/
CONFIG=.vpnsetuptemp/$CONFIGBASEFILENAME
CONFIG2=.vpnsetuptemp/$NETWORKBASEFILENAME
echo "Please enter your softether admin password: "
read softadmin
echo "Please enter your IPSEC Secret: "
read secret
echo "Please enter your l2tp username: "
read username
echo "Please enter your l2tp password: "
read pass
sed -i "s/ADMINPASSWORD/$softadmin/g" $CONFIG
sed -i "s/USERNAME/$username/g" $CONFIG
sed -i "s/TESTPASS/$pass/g" $CONFIG
sed -i "s/TESTSECRET/$secret/g" $CONFIG
sed -i "s/MACADDRESS/`cat /sys/class/net/eth0/address`/g" $CONFIG2
sudo /usr/local/vpnserver/vpncmd localhost:443 /SERVER /IN:$CONFIG
sudo cp /etc/network/interfaces /etc/network/interfaces.orig.bak
sudo cp $CONFIG2 /etc/network/interfaces
sudo rm -r .vpnsetuptemp
echo "You need to reboot for settings to take effect. PLEASE CHECK
/etc/network/interfaces TO make sure things look right! If they do
not, please edit accordingly, especially if working remotely!!!!"
#!/bin/bash
### BEGIN INIT INFO
# Provides: vpnserver
# Required-Start: $network $syslog $remote_fs
# Required-Stop: $network $syslog $remote_fs
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: start firewall
### END INIT INFO
# chkconfig: 2345 99 01
# description: SoftEther VPN Server
DAEMON=/usr/local/vpnserver/vpnserver
LOCK=/var/lock/vpnserver
test -x $DAEMON || exit 0
case "$1" in
start)
$DAEMON start
touch $LOCK
TIMEOUTCOUNTER=0
MAXTIMEOUT=50
SLEEPINTERVAL=0.5
until ifconfig tap_soft 1>/dev/null 2>&1; do
TIMEOUTCOUNTER=$((TIMEOUTCOUNTER + 1))
sleep $SLEEPINTERVAL
if (( $TIMEOUTCOUNTER > $MAXTIMEOUT )); then
echo "Time out waiting for device to come online. Could not add to bridge" 1>&2
exit 1
fi
done
brctl addif br0 tap_soft
;;
stop)
$DAEMON stop
rm $LOCK
;;
restart)
$DAEMON stop
sleep 3
$DAEMON start
;;
*)
echo "Usage: $0 {start|stop|restart}"
exit 1
esac
exit 0
@duceduc
Copy link

duceduc commented Jul 29, 2016

The guide works. I am able to connect remotely. However, when I install pi hole and after a reboot, I lose network connection on the pi. I cannot ping, access internet, etc. There seems to be conflict with pi hole. Maybe a firewall issue?

With a fresh install of pi and the vpn, I am able to connect and reboot without issue. The network breaks after installing pi hole. I do not know how to troubleshoot this or where to begin finding the problem Anyone?

Would be great if someone got their vpn running with pi hole installed.

@lucas-bremond
Copy link

It works perfectly using eth0!
Is there any way to use wlan0 instead?

@Porco-Rosso
Copy link

worked great thanks!

@link611
Copy link

link611 commented Dec 17, 2016

hi there,

i installed softether with your script on my pi3, but if i establish a l2tp vpn, i am not able to connect to the pi3 over vpn, do i have to set something else up?

another thing, the script does not create a br0 interface, i already have the eth0 interface.

@rrothermund
Copy link

Works great! Only solution so far that works with both my Nexus 6P and HP Chromebook 13 G1. Is it possible to tweak this to run on other architectures as well besides the Pi?

@jsrichards
Copy link

Has anyone gotten this to work with WLAN0 instead of eth0? I have played around with the script with no luck. Everything installs, except no br0 interface is created, or at least I cannot see it when I do an ifconfig?

@jhenkens
Copy link
Author

@jsrichards You usually cannot bridge with WLAN interfaces.

@thomas930
Copy link

thomas930 commented Dec 24, 2017

@jhenkens
Hello, it seems your script doesn't work starting on Raspbian Stretch due to a major change on network interfaces structures by using Predictable Network interface Names...
i.e. using /etc/dhcpcd.conf instead of /etc/network/interfaces
May you revise the script, so we can use Softether L2TP VPN with latest Raspbian Stretch?

@thomas930
Copy link

thomas930 commented Dec 30, 2017

@jhenkens
Hello, problem is solved by disable the "N3 Network interfaces name" in "2 Network Options" via "raspi-config"...
Script can be still work in Raspbian Stretch...
But dunno if Raspbian will compulsory ask user to use Predict Network Interface names in the future distribution.

@NoWayOut76
Copy link

Many thanks for this great scripts.
It was installing smoothly on Raspbian Stretch. i changed the step1.sh to include the latest SoftEther version:
http://www.softether-download.com/files/softether/v4.25-9656-rtm-2018.01.15-tree/Linux/SoftEther_VPN_Server/32bit_-_ARM_EABI/softether-vpnserver-v4.25-9656-rtm-2018.01.15-linux-arm_eabi-32bit.tar.gz

Also downloaded the SoftEther Windows Admin Tool which also works perfectly well to set up more users and configure the server.

In order to make it work when connected directly to the internet without router i installed fail2ban to protect logins, changed the listener port of the admin console of softether to a random port and also had to enable SecureNAT in the VirtualHub configuration (DHCP and NAT).

Worked fine!

@Bugz000
Copy link

Bugz000 commented Feb 28, 2019

does not work, rendered my pi inaccessible remotely, it's only by luck that i have physical access to it's keyboard.
DO NOT USE.

@kwasmich
Copy link

kwasmich commented Sep 7, 2019

Works great with the latest Raspbian Buster (2019-07-10) on an original Pi 1 Model B.

@navodya1227
Copy link

(Connection to the server failed. Check network connection and make sure that address and port number of destination sever are correct.
You need to reboot for settings to take effect. PLEASE CHECK
/etc/network/interfaces TO make sure things look right! If they do
not, please edit accordingly, especially if working remotely!!!!) how to solve that

@jhenkens
Copy link
Author

jhenkens commented Mar 5, 2020 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment