Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend legacy script to assign a static ip address to a new client #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 34 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,37 @@ Run the script and follow the assistant:

Once it ends, you can run it again to add more users, remove some of them or even completely uninstall OpenVPN.

### I want to run my own VPN but don't have a server for that
You can get a VPS from just $1/month at [VirMach](https://billing.virmach.com/aff.php?aff=4109&url=billing.virmach.com/cart.php?gid=18).

### Donations

If you want to show your appreciation, you can donate via [PayPal](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=VBAYDL34Z7J6L) or [cryptocurrency](https://pastebin.com/raw/M2JJpQpC). Thanks!
### Extended
This script has been extended to automatically assign a static ip address to a client.
All static ip addresses are stored in ccd directory and the ipp.txt file.
To use static ip routing follow stpes below
1. Create folder with name ccd where static ip addresses will be stored:
mkdir /etc/openvpn/server/ccd
2. Change server.conf configuration file.
Add line: client-config-dir /etc/openvpn/server/ccd
Remove line: ifconfig-pool-persist ipp.txt
Change subnet mask: server 10.8.0.0 255.255.0.0
3. Run ipPoolMigration.sh script to create static ip addreses for existing users.
sudo ./ipPoolMigration.sh
4. Restart openvpn server
sudo systemctl restart openvpn-server@server

To add a new client run openvpn-ubuntu-install.sh script. It will
automatically give static ip to a client.
Don't delete ipp.txt. All ip addresses are stored there.

To add or change static ip manually:
1. Create file with a profile name in ccd directody:
touch /etc/openvpn/server/ccd/client1
2. Add `ifconfig-push {ip} {subnet_mask}` command to the file. ex:
ifconfig-push 10.8.0.236 255.255.0.0
3. Add profile name and ip address to ipp.txt file in following format:
client1,10.8.0.236

You should not restart openvpn server after adding static ip address.

### Info
You can find more info about static ip routing and how to use it below

https://openvpn.net/community-resources/configuring-client-specific-rules-and-access-policies/
https://kifarunix.com/assign-static-ip-addresses-for-openvpn-clients/
9 changes: 9 additions & 0 deletions ipPoolMigration.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/bash
ip_pool="/etc/openvpn/server/ipp.txt"

while IFS="," read -ra line; do
name="${line[0]}"
address="${line[1]}"

echo "ifconfig-push $address 255.255.0.0" > /etc/openvpn/server/ccd/$name
done <"$ip_pool"
22 changes: 22 additions & 0 deletions openvpn-install.sh
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,28 @@ new_client () {
sed -ne '/BEGIN OpenVPN Static key/,$ p' /etc/openvpn/server/tc.key
echo "</tls-crypt>"
} > ~/"$client".ovpn

# add address to ccd
last_address=$(grep -oE '\b[0-9]{1,3}(\.[0-9]{1,3}){3}\b' /etc/openvpn/server/ipp.txt |
sort -t . -k 3,3n -k 4,4n |
tail -n1
)
IFS="." read -ra array <<< "$last_address"

if [[ ${array[3]} -gt 253 ]]; then
array[3]=0
let next=${array[2]}+1
array[2]=$next
else
let next=${array[3]}+1
array[3]=$next
fi

printf -v new_ip "%s." "${array[@]}"
new_ip=${new_ip%?}
echo "$client,$new_ip" >> /etc/openvpn/server/ipp.txt
echo "ifconfig-push $new_ip 255.255.0.0" >> /etc/openvpn/server/ccd/"$client"
#don't give last 2 addresses (10.8.254.253 - 10.8.254.254)
}

if [[ ! -e /etc/openvpn/server/server.conf ]]; then
Expand Down