LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Networking
User Name
Password
Linux - Networking This forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game.

Notices

Reply
 
Search this Thread
Old 07-31-2011, 05:40 AM   #1
Zilvermeeuw
Member
 
Registered: Jan 2006
Distribution: Fedora 16, CentOS 5.8, Ubuntu 10.10 (not anymore)
Posts: 74

Rep: Reputation: 16
Question Access to hostname on LAN and WAN


I have a domain name, lets say domain.nl, at a hosting company. I added a dns record home.domain.nl, which directs to my home IP address, where I have a server.

On WAN I can reach my server with home.domain.nl.
On LAN, home.domain.nl gives me my WAN ip number, which is NOT my server.

I drag my laptop and tablet computers from LAN to WAN and back again. But each time I have to change my domain name.

What can I do to have access to my server, with the same domain name, on LAN and WAN?
 
Old 07-31-2011, 06:01 AM   #2
eSelix
Senior Member
 
Registered: Oct 2009
Location: Wroclaw, Poland
Distribution: Kubuntu
Posts: 1,190

Rep: Reputation: 301Reputation: 301Reputation: 301Reputation: 301
The easiest method is to assign (in /etc/hosts file or in router dns configuration) your domain home.domain.nl to your internal/LAN server IP as I suppose that your local network is behind NAT and uses private addresses. More complicated is to properly configuring iptables in router if you have access to that option.
 
Old 07-31-2011, 06:06 AM   #3
Zilvermeeuw
Member
 
Registered: Jan 2006
Distribution: Fedora 16, CentOS 5.8, Ubuntu 10.10 (not anymore)
Posts: 74

Original Poster
Rep: Reputation: 16
Quote:
Originally Posted by eSelix View Post
The easiest method is to assign (in /etc/hosts file or in router dns configuration) your domain home.domain.nl to your internal/LAN server IP as I suppose that your local network is behind NAT and uses private addresses.
That solves it, when I am at home. But If I use the same laptop outside (LAN), than it will not work.

Quote:
More complicated is to properly configuring iptables in router if you have access to that option.
I do not have access to iptables on my router, but I have access on my server.
 
Old 07-31-2011, 06:36 AM   #4
eSelix
Senior Member
 
Registered: Oct 2009
Location: Wroclaw, Poland
Distribution: Kubuntu
Posts: 1,190

Rep: Reputation: 301Reputation: 301Reputation: 301Reputation: 301
Sorry, I did not exactly read that you use laptop in your LAN and outside LAN. Can you explain how your laptop is connecting to Internet - the devices on te way, IP addresses. You can start your own DNS server on LAN (for example on the server you mentioned), which answer with local address on response to home.domain.nl. But then you must reconfigure it on laptop. How is configured your DNS server now, it is at your control or your Internet provider, it is from static or dynamic configuration?
 
Old 07-31-2011, 10:32 AM   #5
Zilvermeeuw
Member
 
Registered: Jan 2006
Distribution: Fedora 16, CentOS 5.8, Ubuntu 10.10 (not anymore)
Posts: 74

Original Poster
Rep: Reputation: 16
Hi eSelix,

My home situation:
I have an modem/router, which is connected to the internet and has built-in WIFI. My laptop (Ubuntu) is connected with WIFI. The same is valid for my tablet computers (android). I have a fixed external IP address.
In this modem/router I have port-forwarding to my server. My server is wired-connected to the modem/router on a fixed IP address.
At home I connect to my home server with the hostname of this server (server10)

I take my laptop and tablet computers also to "out of home", where I connect to another WIFI access point. From the outside, I connect to the server with home.domain.nl.

So moving from "outside" to home needs a different name. I want to have this the same.

If I have a dns server, I can give my WIFI a fixed IP and points the dns to my server.
But if I use the laptop "outside" I cannot have a fixed IP.

Lets see, if I can fix my IP for the "home" situation and let it automatically for the "outside".

Do you see problems? Or do you have another direction?
 
Old 07-31-2011, 10:58 AM   #6
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,524
Blog Entries: 27

Rep: Reputation: 1175Reputation: 1175Reputation: 1175Reputation: 1175Reputation: 1175Reputation: 1175Reputation: 1175Reputation: 1175Reputation: 1175
If your laptop uses DHCP (the only sane choice when connecting to a variety of LANs) then you could set up a DHCP service and a DNS service on your server.

Your DHCP server would give your laptop its own (the server's own) address to use as a DNS server. The DNS service would resolve mydomain.nl to its own address and would otherwise simply be a caching server.
 
Old 07-31-2011, 01:02 PM   #7
Zilvermeeuw
Member
 
Registered: Jan 2006
Distribution: Fedora 16, CentOS 5.8, Ubuntu 10.10 (not anymore)
Posts: 74

Original Poster
Rep: Reputation: 16
Quote:
Originally Posted by catkin View Post
Your DHCP server would give your laptop its own (the server's own) address to use as a DNS server. The DNS service would resolve mydomain.nl to its own address and would otherwise simply be a caching server.
That makes sense.

But now the laptop gets its IP from the modem/router. I cannot switch this off. Should I create a new subnet? Now I have 192.168.1.*, the new subnet can be 192.168.2.*

Or can I give the new DHCP server priority? I will dig into DHCP.

Thanks!
 
Old 07-31-2011, 03:11 PM   #8
eSelix
Senior Member
 
Registered: Oct 2009
Location: Wroclaw, Poland
Distribution: Kubuntu
Posts: 1,190

Rep: Reputation: 301Reputation: 301Reputation: 301Reputation: 301
The best approach would be configuring DNS. But as you have no access to it and as I known DNS has no priority system - the one who first answer to your laptop will be accepted, you can do some scripting to /etc/network/if-up.d or other system starting file, like that:
Code:
#!/bin/bash

HOSTS_FILE=/etc/hosts
DOMAIN=domain.nl
IP=1.2.3.4
HOME_ROUTER_IP=192.168.1.1
CURRENT_ROUTER_IP=$(ip route list | grep default | sed 's/default via \([0-9.]\+\).*/\1/')

cat "$HOSTS_FILE" | grep "$DOMAIN" > /dev/null
HOSTS_FILE_IS_ORGINAL=$?

if [[ "$HOSTS_FILE_IS_ORGINAL" = "1" ]] && [[ "$CURRENT_ROUTER_IP" = "$HOME_ROUTER_IP" ]] ; then
  echo "$IP $DOMAIN" >> "$HOSTS_FILE"
elif [[ "$HOSTS_FILE_IS_ORGINAL" = "0" ]] && [[ "$CURRENT_ROUTER_IP" != "$HOME_ROUTER_IP" ]] ; then
  mv -f "$HOSTS_FILE" "$HOSTS_FILE".bak
  cat "$HOSTS_FILE".bak | grep -v "$DOMAIN" > "$HOSTS_FILE"
fi

Last edited by eSelix; 07-31-2011 at 03:12 PM.
 
1 members found this post helpful.
Old 07-31-2011, 11:26 PM   #9
Zilvermeeuw
Member
 
Registered: Jan 2006
Distribution: Fedora 16, CentOS 5.8, Ubuntu 10.10 (not anymore)
Posts: 74

Original Poster
Rep: Reputation: 16
Quote:
Originally Posted by eSelix View Post
The best approach would be configuring DNS. But as you have no access to it and as I known DNS has no priority system - the one who first answer to your laptop will be accepted, you can do some scripting to /etc/network/if-up.d or other system starting file, like that:
Code:
#!/bin/bash

HOSTS_FILE=/etc/hosts
DOMAIN=domain.nl
IP=1.2.3.4
HOME_ROUTER_IP=192.168.1.1
CURRENT_ROUTER_IP=$(ip route list | grep default | sed 's/default via \([0-9.]\+\).*/\1/')

cat "$HOSTS_FILE" | grep "$DOMAIN" > /dev/null
HOSTS_FILE_IS_ORGINAL=$?

if [[ "$HOSTS_FILE_IS_ORGINAL" = "1" ]] && [[ "$CURRENT_ROUTER_IP" = "$HOME_ROUTER_IP" ]] ; then
  echo "$IP $DOMAIN" >> "$HOSTS_FILE"
elif [[ "$HOSTS_FILE_IS_ORGINAL" = "0" ]] && [[ "$CURRENT_ROUTER_IP" != "$HOME_ROUTER_IP" ]] ; then
  mv -f "$HOSTS_FILE" "$HOSTS_FILE".bak
  cat "$HOSTS_FILE".bak | grep -v "$DOMAIN" > "$HOSTS_FILE"
fi
That is interesting!
My first idea was to do some scripting in /etc/hosts. But I could not figure out how. Your solution can do the trick!
Unfortunately, this will be a busy week, so implementing can take some days.

Thinking about it:
Can I do something like:
Code:
if exists 'a specific file on my server through home IP' then '# I am at home; echo stuff to /etc/hosts'
?
If so, than I do not need my own DHCP server.
 
Old 07-31-2011, 11:52 PM   #10
micxz
Senior Member
 
Registered: Sep 2002
Location: CA
Distribution: openSuSE, Cent OS, Slackware
Posts: 1,131

Rep: Reputation: 75
Or you could just un-comment the line in hosts when your at home. Something tells me I missed something an this is not the answer.
 
Old 08-01-2011, 02:12 AM   #11
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,524
Blog Entries: 27

Rep: Reputation: 1175Reputation: 1175Reputation: 1175Reputation: 1175Reputation: 1175Reputation: 1175Reputation: 1175Reputation: 1175Reputation: 1175
Quote:
Originally Posted by Zilvermeeuw View Post
But now the laptop gets its IP from the modem/router. I cannot switch this off. Should I create a new subnet? Now I have 192.168.1.*, the new subnet can be 192.168.2.*

Or can I give the new DHCP server priority? I will dig into DHCP.
It is unusual not to be able to de-configure a modem/router's DHCP service. Perhaps it is not a technical issue ... ?

IDK about setting a preferred DHCP server; the usual arrangement is to broadcast for a DHCP lease and accept the first given; it may be possible to change that.

Taking eSelix's suggestion further (I understand it relies on your home LAN IP address range and default gateway to be unique amongst all the LANs your laptop connects to -- practicable if not 100% robust if your modem-router works with an "exotic" configuration such as 10.52.84.x) ...

You could leave your modem-router configuration as is (and all the other devices on your home LAN) and leave your laptop accepting whatever IP address and default gateway it is given by DHCP but then it could identify which LAN it is on by examining the MAC address of the default gateway; if it is your home LAN it could then set up the hosts file to resolve mydomain.nl as your server's LAN address, otherwise set up the hosts file without that entry.

Here's sample code to determine the default gateway's MAC address:
Code:
found=
while read destination gateway _
do
    if [[ $destination = default ]]; then
        ping -c 1 -q $gateway >/dev/null    # Populate ARP cache
        while read address _ hw_address _
        do
            [[ $address = $gateway ]] && found=yes && break 2
        done <<< "$( exec arp )"
    fi
done <<< "$( exec route )"

[[ $found ]] && echo "Gateway HW address is $hw_address" || echo 'Gateway HW address not found' >&2
 
Old 08-01-2011, 11:16 PM   #12
Zilvermeeuw
Member
 
Registered: Jan 2006
Distribution: Fedora 16, CentOS 5.8, Ubuntu 10.10 (not anymore)
Posts: 74

Original Poster
Rep: Reputation: 16
I know, usually it is possible to switch of DHCP in the modem/router, but Tele2-NL not.

Indeed, filtering on MAC is a better way than check for a file on a server!

Thanks for the script!!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help with Apache: free access from lan users and password access to wan users... how? maginotjr Linux - Server 4 01-01-2011 11:08 AM
LAN cannot access other LAN systems, only WAN Myrion Linux - Networking 1 09-06-2007 07:29 PM
Show WAN ip, hostname, etc. lothario Linux - Networking 2 02-02-2005 11:59 AM
redhat 7.3 can only access LAN, not WAN ForumKid Linux - Networking 1 08-01-2004 08:14 AM
Routing LAN -> WAN -> LAN with unhelpful router synx13 Linux - Networking 2 06-14-2004 02:35 PM


All times are GMT -5. The time now is 10:45 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration