What's new

Adding OpenVPN Client to DNS

  • SNBForums Code of Conduct

    SNBForums is a community for everyone, no matter what their level of experience.

    Please be tolerant and patient of others, especially newcomers. We are all here to share and learn!

    The rules are simple: Be patient, be nice, be helpful or be gone!

TechnoSwiss

New Around Here
I've been searching and trying to get this working and I'm coming up blank, most of the posts I'm finding are about pushing DNS to the OpenVPN clients (that's working great) or adding hosts to the configuration file (that's also working great). What I want to do however is when my OpenVPN client connects to the router, I want the DNS updated with that clients OpenVPN address, so that other hosts on the network can resolve the client to an IP address. I've got a script running on client connect, I've tried 'dnsmasq --host-record=$common_name,$ifconfig_pool_remote_ip' but all that manages to do is create another instance of dnsmasq, not actually add the host-record into the running instance. I also tried using the addhosts option in dnsmasq config since it looks like it's supposed to read in new hosts files as they're added to the directory, but I just get an error that the option isn't supported. Is there a way to setup OpenVPN to add it's clients to dnsmasq just like the dhcp-clients on the local network (or setup the dhcp server in OpenVPN to do it?) or, is there a way to add new hosts to the running instance of dnsmasq from the script? Thanks.
 
The correct directive which defines additional hosts is ...

Code:
addn-hosts=/jffs/addn-hosts

If you add that directive to the DNSMasq configuration, then update the associated file w/ the appropriate address or host-record directive (preferred) for each OpenVPN client as they connect, you can send the following signal to the running dnsmasq instance to force it to reread that file and thus DNS resolution will work.

Code:
killall -HUP dnsmasq
 
Thanks, that's what I ended up doing, addn-hosts=/jffs/hosts.openvpn

Then in my script that runs on OpenVPN client I did:

Code:
#!/bin/sh

matches_in_hosts="$(grep -n "$ifconfig_pool_remote_ip\|$common_name" /jffs/hosts.openvpn | cut -f1 -d: | sort -r)"
host_entry="$ifconfig_pool_remote_ip $common_name"

if [ ! -z "$matches_in_hosts" ]
then
    # iterate over the line numbers on which matches were found
    for line_number in ${matches_in_hosts}
    do
       printf "%d\n" "$line_number"
        # delete each line where match was found
        sed -i -e "${line_number}d" /jffs/hosts.openvpn
    done
fi
echo "$host_entry" | tee -a /jffs/hosts.openvpn > /dev/null

kill -HUP $(pidof dnsmasq)
 

Similar threads

Latest threads

Sign Up For SNBForums Daily Digest

Get an update of what's new every day delivered to your mailbox. Sign up here!
Top