Google+ Badge

Wednesday, April 3, 2013

A handy Mac terminal command for getting IP without all the other junk of ifconfig

In a terminal window on a Mac you can type:

ifconfig

This will give you your IP address in the midst of a bunch of other stuff that is useful on occasion but usually not anything you care about.

If you type this:

ifconfig | grep "inet "

You will only see the lines related to your IP address which narrows things down a ton.

If you want to narrow it down a bit more you can do this:

ifconfig | grep "inet 192.168" 

If you are on a 192.168 network (which you probably are if using WiFi) then the top item in the list will be your IP address. Of course you can substitute 192.168 for your network prefix.

As a final helper if you need your IP address a lot you can add the following to your ~/.bashrc file

alias ip="ifconfig | grep 'inet 192.168'"

Reload .bashrc via

source ~/.bashrc

Now every time you type:

ip

and press enter in a terminal window you will get your IP address without having your eyes dance all over the full output of ifconfig to find it.

If you forget what you set the alias to you can always type

alias

This will show the current alias list. I also have

alias ls="ls -G"

This shows my directory information in color.