macOS / Linux: find own IP address through bash
by admin on Mar.07, 2017, under Linux (Ubuntu), MAC OS X
Finding the own IP address on macOS (or any other *nix) system can be done e.g. by combining a few terminal commands along with some regex.
On most macOS the default WiFi device is called en0. On most linux distributions it’s eth0.
macOS:
ifconfig en0 | grep -Eo '([0-9]+.[0-9]+.[0-9]+.[0-9]+)' | head -n 1
Linux:
ifconfig eth0 | grep -Eo '([0-9]+.[0-9]+.[0-9]+.[0-9]+)' | head -n 1
It works as follows:
- Get the network configuration for the network interface en0
- Filter the output through regex
- 1 or more occurence of the characters 0-9 followed by a dot, 1 or more occurences… (xxx.xxx.xxx.xxx)
- Use head -n 1 in order to get only the first result of grep