1

I'm running an XMPP server on a raspberry pi on my home lan. I have a domain registered to point at my router's external ip and the ports are forwarded on my router. However, I can't use the external domain/ip when connected to my LAN, only when out and about. What's the easier way to configure my computers at home to recognize whether the server is local (and point to 192.168.x.x) or remote?

2 Answers 2

1

Set up a lightweight DNS server on your raspberry (I recommend dnsmasq over the "standard" ISC BIND server because it's easier to set up for this purpose) and configure it as the default DNS server for any device connected to your LAN (most likely, in your DHCP configuration).

In the DNS configuration, point your domain to the local 192.168.x.x address, thus overriding the internet-global domain pointer within your LAN. Configure the DNS to forward all other queries to the appropriate DNS server outside your LAN (such as your ISPs DNS).

This will result in a LAN device obtaining the local server address whenever it queries for your domain. When that device is on the road, it will obtain the "real" external IP from a DNS server outside your LAN.

2
  • Thanks I disabled NetworkManager from overwriting /etc/resolv.conf and added my raspberry pi and 8.8.8.8 as name servers. Does that sound about right? Mar 28, 2016 at 20:53
  • @Steinberg2010 Depends on how you assign IP addresses within your LAN. If done via your router's DHCP, I'd set the raspberry as the DNS server in the router's DHCP config. That way, all LAN clients will query the raspberry without any need for config changes in the clients. The problem with adding the raspberry AND Google DNS on the client side is that you have a potentially ambiguous situation. It isn't really guaranteed that a client will always query the raspberry instead of Google when connected to the LAN. It might attempt to query Google first and receive the wrong (external) IP.
    – Guido
    Mar 28, 2016 at 21:13
0

As previous another answer has advised, you can use a small dns server on your raspi and do it that way. If you don't want to, and if your name resolution is set to look-up /etc/hosts first, you can use a script like that, to determine what to do

domain=mydomain.com  # modify as needed
domain_INT_IP=192.168.1.10  #modify as needed
home_netw=192.168 # first two octets, modify as needed but probably not
NIC=eth0  # modify as needed
myIP=$(ifconfig ${NIC}|grep "inet addr"|cut -d: -f2|cut -d" " -f1)
my_netw=$(echo ${myIP}|cut -d. -f1-2)
if [ "${my_netw}" == ${home_netw} ]
then
  echo ${domain_EXT_IP}"  "${domain} >> /etc/hosts
else
  grep "${domain}" /etc/hosts >/dev/null; r=${?}
  if [ ${r} -eq 0 ]
  then
    sed -i -e "/${domain}/d" /etc/hosts
  fi
fi

it basically inserts the local IP address to the /etc/hosts file when it detects your machine's IP address is inside your home network. And deletes this line when you are out and about, letting you use the whatever DNS server is configured in your /etc/resolv.conf file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.