Add static IP assignment to OpenVPN

This commit is contained in:
Johan 2025-03-20 08:20:56 +01:00
parent 2d06663a3c
commit 3a2ffa191b
2 changed files with 14 additions and 0 deletions

View File

@ -15,6 +15,14 @@ if [ -f "/etc/openvpn/easy-rsa/pki/issued/$CLIENT_NAME.crt" ]; then
echo Client $CLIENT_NAME already exists...
exit 1
fi
VPN_SUBNET=$(grep -E '^server ' "/etc/openvpn/myserver.conf" | awk '{print $2}')
read -e -p "Use static IP for this client? VPN subnet is $VPN_SUBNET (Leave empty for dynamic): " -i "" CLIENT_IP
if [ ! -z "${CLIENT_IP}" ]; then
echo Setting IP...
cat > "/etc/openvpn/ccd/$CLIENT_NAME" <<EOL
ifconfig-push $CLIENT_IP 255.255.255.0
EOL
fi
cd /etc/openvpn/easy-rsa
./easyrsa gen-req $CLIENT_NAME nopass

View File

@ -28,6 +28,9 @@ fi
if [ ! -d /var/log/openvpn ]; then
mkdir -p /var/log/openvpn
fi
if [ ! -d /etc/openvpn/ccd ]; then
mkdir -p /etc/openvpn/ccd
fi
if ufw status | grep -q "Status: active"; then
echo Adding firewall rules...
ufw allow $VPN_PUBLIC_PORT/udp
@ -52,6 +55,9 @@ dh dh.pem
server $VPN_SUBNET.0 255.255.255.0
ifconfig-pool-persist /var/log/openvpn/ipp.txt
client-config-dir /etc/openvpn/ccd
topology subnet
ifconfig-pool $VPN_SUBNET.2 $VPN_SUBNET.126
push "route $LAN_SUBNET.0 255.255.255.0"
keepalive 10 120