I've got a VCS running on debian as prod server, and due to some hypervisor issues it had to be rebooted/migrated/updated several times in the past week.

Because i'm running my app using docker containers, i can't just pass localhost for it to connect to my local postgres, so i'm using the ens2 ip instead.

ens2: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet  netmask  broadcast
        inet6 fe80::dc1c:14ff:fe51:5002  prefixlen 64  scopeid 0x20<link>
        inet6 2001:bc8:4740:205::1  prefixlen 127  scopeid 0x0<global>
        ether de:1c:14:51:50:02  txqueuelen 1000  (Ethernet)
        RX packets 37139  bytes 4801851 (4.5 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 39791  bytes 35291408 (33.6 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

But with every reboot that ip changes, and i have to manually go and change it in env vars so my app can reach the db again, which i'm sure you'll agree is not very practical.

So here is my question: is there a way to automatically assign an alias, pretty much like localhost, to this specific interface?

I guess i could still try and parse the ifconfig output but i'd rather ask first for an actual solution 👼

Thanks for your time guys 👌



Maintain a record for the application to use, perhaps appdb.prod.example.org. As this is a service name, consider creating a CNAME to the host currently running it.

Update a name in DNS each time a host gets an IP. For example, AAAA record called container1.example.org containing 2001:bc8:4740:205::1. How exactly to do this depends on your numbering scheme and DNS configuration. Static IPs can be entered as soon as they are allocated. DHCP hooks can be used to update DNS. And there exists software to watch every interface for IP changes and do an nsupdate, such as autoddns.

Do not use ifconfig on Linux, not for new scripts. It has been obsolete for at least 11 years.

  • Oh wow, 11 years. That's a lot 😶 I feel a little bit outdated here. I'll have a look to this autoddns, it sounds like what i need, thank you ! – tsadiq Jan 14 at 11:05
  • In addition, I gave the other examples like static and DHCP to show this depends on your address allocation methods and DNS environment. – John Mahowald Jan 14 at 13:39

Could always use:

alias ip="hostname -I"
  • Yeah i could cut the ip out of this easily but then it's tricky is to keep it up to date in /etc/hosts every time it changes... I was rather wondering about a "legit" way to refer to a given interface 🤔 – tsadiq Jan 13 at 10:33
  • 1
    Regarding -I, per the manual Do not make any assumptions about the order of the output. A robust script would parse the results to see which is IPv4 and which is IPv6. – John Mahowald Jan 14 at 7:38

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.