Best way for a new droplet to programmatically learn its IP address

Posted July 2, 2020 645 views
NetworkingUbuntu 18.04

I am planning to create new droplets dynamically. They will register themselves by writing their IP address in a database. They are all going to be on a private network, so this will be the IP address of eth1 and not eth0. Is the best way for a droplet to learn its IP address to run ifconfig and parse its output, or is there a better way to do this?

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Submit an Answer
1 answer

Hi @RalphEJohnson, I remember needing something like this for a project a while ago, I basically did the following. It gives you the private IP address of the droplet.

ip addr show eth1 | grep "inet\b" | awk '{print $2}' | cut -d/ -f1