I have compiled a little LFS system and I'm currently trying to get Internet connectivity up and running. I have managed to get it working manually, but this required me to know the router ip address. However, I want my system to be able to connect to the internet in any network, even if the router ip address is initially unknown. How can I retrieve the router ip address (also referred to as the gateway) dynamically/automatically?

  • Are you able to connect to your routers LAN even though you don’t have internet access at first? – Peschke Jun 15 at 16:35
  • @Peschke yes, I use busybox dhcp to retreive the ip and then I assign the ip to eth0 using ifconfig. This enables me to connect to lan devices, but not any internet devices. – Ian Rehwinkel Jun 15 at 16:36
  • The DHCP answer should include the gateway (and additional routes, though this is rarely used). Most DHCP clients honor this information and act accordingly. You didn't tell use what DHCP client you use, and how it is configured. – dirkt Jun 15 at 16:57
  • I am using busybox udhcpc for dhcp – Ian Rehwinkel Jun 15 at 16:58
  • 1
    If you're able to automatically connect to your LAN, but then cannot reach the internet without intervention. Either your LAN isn't (properly) forwarding your requests, or your dhcp client is brain dead (need to better understand your DHCP client, or replace it). :) – somebody Jun 16 at 11:04

Assuming you want the external IP gateway address, you'll need to either find out from the gateway device itself or contact an external service. A site that I use for stuff like this (particularly for ddclient, as it needs to know when to re-register my DDNS and tunnel) is icanhazip.com, which can show your Internet-facing IPv4 and IPv6 addresses, among other things. However, as IPv6 is supposed to be globally-routable, you probably don't need to ask an external service regarding IPv6 addresses.

There are a couple of ways of using this service:

  • In a configuration file, point to (http://)ipv4.icanhazip.com (or ipv6 if you need that.)
  • In a script, get the address using curl like this:

    echo My external IP address is $(curl -s http://ipv4.icanhazip.com)

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.