Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find internal IP address with BASH

I am already aware of many ways of getting your internal IP (ifconfig, ip addr, /etc/hosts, etc), but I am trying to write a bash script that will always return the internal IP. The problem is, many one-liners (/sbin/ifconfig|grep inet|head -1|sed 's/\:/ /'|awk '{print $3}') can return multiple IPs, and I need to distinguish the internal one manually.

I suspect that to the computer, there is no difference between and an external IP and an internal IP, and thus no 100%, guaranteed way to get the right IP.

The end result is that this script will return the internal IP, no matter if its a 192 address or a 204 address, etc.

Thanks in advance.

like image 430
wrangler Avatar asked Feb 23 '26 14:02

wrangler


1 Answers

"hostname -i" should hopefully give you the same result

like image 71
Keo Malope Avatar answered Feb 26 '26 17:02

Keo Malope



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!