How to display only IP address using Terminal command


When you use the command ifconfig, it displays lot of information to the console and for a non technical person it could be very confusing to get where the IP address details. The easiest way to display only the IP address using Terminal command ipconfig is by using it options getifaddr en0

Example:
$ ipconfig getifaddr en0

Output: 192.168.225.137

Get only the IP address details using ipconfig
Get only the IP address details using ipconfig

You can also make use of ifconfig command combine grep and awk commands to filter out the IP address.

bash-3.2$ ifconfig
lo0: flags=8041<UP,LOOPBACK,RUNNING,MULTICAST> mtu 16382
	options=1203<RXCSUM,TXCSUM,TXSTATUS,SW_TIMESTAMP>
	inet 127.0.0.1 netmask 0xff000000 
	inet6 ::1 prefixlen 128 
	inet6 fe80::1%lo0 prefixlen 64 scopeid 0x1 
	nd6 options=201<PERFORMNUD,DAD>
gif0: flags=8010<POINTOPOINT,MULTICAST> mtu 1280
stf0: flags=0<> mtu 1280
anpi0: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
	options=400<CHANNEL_IO>
	ether 1e:29:c0:20:2e:48 
	inet6 fe80::1c29:c0ff:fe20:2e48%anpi0 prefixlen 64 scopeid 0x4 
	nd6 options=201<PERFORMNUD,DAD>
	media: none
	status: inactive
anpi1: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
	options=400<CHANNEL_IO>
	ether 1e:29:c0:20:2e:49 
	inet6 f180::1c29:c0aa:fe11:aa49%anpi1 prefixlen 64 scopeid 0x2 
	nd6 options=201<PERFORMNUD,DAD>
	media: none
	status: inactive
en3: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
	options=400<CHANNEL_IO>
	ether 1a:21:c0:20:2e:28 
	nd6 options=201<PERFORMNUD,DAD>
	media: none
	status: inactive
en4: flags=8861<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
	options=400<CHANNEL_IO>
	ether 1e:11:c0:21:2e:12
	nd6 options=201<PERFORMNUD,DAD>
	media: none
	status: inactive
en1: flags=8963<UP,BROADCAST,SMART,RUNNING,PROMISC,SIMPLEX,MULTICAST> mtu 1500
	options=460<TSO4,TSO6,CHANNEL_IO>
	ether 12:1f:73:31:f2:82 
	media: autoselect <full-duplex>
	status: inactive
en2: flags=8963<UP,BROADCAST,SMART,RUNNING,PROMISC,SIMPLEX,MULTICAST> mtu 1500
	options=460<TSO4,TSO6,CHANNEL_IO>
	ether 12:1f:73:31:f2:82 
	media: autoselect <full-duplex>
	status: inactive
ap1: flags=8802<BROADCAST,SIMPLEX,MULTICAST> mtu 1500
	options=400<CHANNEL_IO>
	ether 312:1f:73:31:f2:82  
	media: autoselect
	status: inactive
en0: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
	options=400<CHANNEL_IO>
	ether 12:1f:73:31:f2:82 
	inet6 fe10::12b4:d2da:7345:a622b%en0 prefixlen 64 secured scopeid 0xb 
	inet6 2409:4040:d83:bd3d:4f4:f553:2c2c:bdc4 prefixlen 64 autoconf secured 
	inet6 2409:4040:d83:bd3d:e43d:395:23bc:1cb1 prefixlen 64 autoconf temporary 
	inet 192.168.225.137 netmask 0xffffff00 broadcast 192.168.225.255
	nd6 options=201<PERFORMNUD,DAD>
	media: autoselect
	status: active
bridge0: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
	options=63<RXCSUM,TXCSUM,TSO4,TSO6>
	ether 12:1f:73:31:f2:82 
	Configuration:
		id 0:0:0:0:0:0 priority 0 hellotime 0 fwddelay 0
		maxage 0 holdcnt 0 proto stp maxaddr 100 timeout 1200
		root id 0:0:0:0:0:0 priority 0 ifcost 0 port 0
		ipfilter disabled flags 0x0
	member: en1 flags=3<LEARNING,DISCOVER>
	        ifmaxaddr 0 port 8 priority 0 path cost 0
	member: en2 flags=3<LEARNING,DISCOVER>
	        ifmaxaddr 0 port 9 priority 0 path cost 0
	nd6 options=201<PERFORMNUD,DAD>
	media: <unknown type>
	status: inactive
awdl0: flags=8943<UP,BROADCAST,RUNNING,PROMISC,SIMPLEX,MULTICAST> mtu 1500
	options=400<CHANNEL_IO>
	ether 12:1f:73:31:f2:82  
	inet6 fe10::b117:11ff:fe22:12b8%awdl0 prefixlen 64 scopeid 0xd 
	nd6 options=201<PERFORMNUD,DAD>
	media: autoselect
	status: active
llw0: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
	options=400<CHANNEL_IO>
	ether 12:1f:73:31:f2:82 
	inet6 fe80::bcb7:27ff:fe63:8bb8%llw0 prefixlen 64 scopeid 0xe 
	nd6 options=201<PERFORMNUD,DAD>
	media: autoselect
	status: active
utun0: flags=8051<UP,POINTOPOINT,RUNNING,MULTICAST> mtu 1380
	inet6 fe10::b117:11ff:fe22:12b8%awdl0 prefixlen 64 scopeid 0xd 
	nd6 options=201<PERFORMNUD,DAD>
utun1: flags=8051<UP,POINTOPOINT,RUNNING,MULTICAST> mtu 2000
	inet6 fe10::b117:11ff:fe22:12b8%awdl0 prefixlen 64 scopeid 0xd 
	nd6 options=201<PERFORMNUD,DAD>

Extracting only the required private IP address details,

$ ifconfig | grep "inet.*broadcast.*" | awk '{print $2}'

192.168.225.137

Ifconfig command to get private ip details
Ifconfig command to get private ip details


Have Questions? Post them here!
Copyright © Code2care 2024 | Privacy Policy | About Us | Contact Us | Sitemap