I am writing a Bash script that requires the internal/private IP address. On a Linux system I normally use the hostname -I command. However, this does not work on some other Unix-like systems, most notably MacOS.
Is there any command that does return the private IP address, that is present on most (if not all) Unix-like systems?
Asked
Active
Viewed 41 times
1
Lukas
- 47
- 5
-
Some of my systems have two (or even more) internal private IP addresses. Which one of these equal-weight addresses should be returned? – roaima Feb 09 '22 at 19:39
-
@roaima, on my system, `hostname -I` returns all of them, though only the IPv4 ones (at least not the link-local IPv6 ones), and it seems to exclude the address of the loopback interface. – Stéphane Chazelas Feb 09 '22 at 19:41
-
I do not know how up to date this information is: https://apple.stackexchange.com/questions/20547/how-do-i-find-my-ip-address-from-the-command-line – BaTycoon Feb 09 '22 at 19:43
-
1@roaima, looking at the code, it loops over network interfaces, skips the loopback interface, interfaces that are not up, link-local IPv6 ones, – Stéphane Chazelas Feb 09 '22 at 19:45
-
2See also: [How to get my own IP address and save it to a variable in a shell script?](https://unix.stackexchange.com/a/48315) (the perl approach should be fairly portable, and may give you a more useful result than `hostname -I` depending on what exactly you want to do) – Stéphane Chazelas Feb 09 '22 at 19:48
-
@StéphaneChazelas This fits my needs perfectly. Thank you very much. – Lukas Feb 09 '22 at 21:32