Question

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

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?

Subscribe
Share

Submit an answer
You can type!ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

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.

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