Tuesday, August 09, 2005

Fun with ifconfig on Mac OS-X

One of the UNIX utilities I use all the time on OS-X is ifconfig, the command line program for configuring your computer's network interfaces. Since I often have a terminal window open, I find it faster to use ifconfig to find out my IP address than to open the System Preferences > Network utility. If you run ifconfig in a terminal without any arguments, you get output that looks like this:

Dave-Markowitzs-Computer:~ davemarkowitz$ ifconfig
lo0: flags=8049 mtu 16384
inet 127.0.0.1 netmask 0xff000000
inet6 ::1 prefixlen 128
inet6 fe80::1%lo0 prefixlen 64 scopeid 0x1
gif0: flags=8010 mtu 1280
stf0: flags=0<> mtu 1280
en0: flags=8863 mtu 1500
inet 10.25.10.51 netmask 0xfffffc00 broadcast 10.25.11.255
ether 00:0d:93:70:ed:44
media: autoselect (100baseTX ) status: active
supported media: none autoselect 10baseT/UTP 10baseT/UTP 10baseT/UTP 100baseTX 100baseTX 100baseTX
en1: flags=8863 mtu 1500
ether 00:11:24:26:23:5f
media: autoselect () status: inactive
supported media: autoselect
fw0: flags=8863 mtu 2030
lladdr 00:0d:93:ff:fe:70:ed:44
media: autoselect status: inactive
supported media: autoselect

If you just want to find out your IP, pipe the output to grep, like so:
Dave-Markowitzs-Computer:~ davemarkowitz$ ifconfig | grep inet
inet 127.0.0.1 netmask 0xff000000
inet6 ::1 prefixlen 128
inet6 fe80::1%lo0 prefixlen 64 scopeid 0x1
inet 10.25.10.51 netmask 0xfffffc00 broadcast 10.25.11.255
As you can see, it's a lot easier to sort out your IP address with grep.

Something I've been using a bit lately in the lab when testing equipment on one physical network which has two logical networks running on it, is to use IP aliasing. With IP aliasing you can use ifconfig to assign your network interface a second IP, simultaneous address.

For example, as you can see from the above example, my Ethernet port currently is assigned the IP address 10.25.10.51. If I need to also be on the 192.168.0.x network, I can add an address in that subnet, like so:

Dave-Markowitzs-Computer:~ davemarkowitz$ sudo ifconfig en0 alias 192.168.0.20 255.255.255.0
Password:
Dave-Markowitzs-Computer:~ davemarkowitz$ ifconfig | grep inet
inet 127.0.0.1 netmask 0xff000000
inet6 ::1 prefixlen 128
inet6 fe80::1%lo0 prefixlen 64 scopeid 0x1
inet 10.25.10.51 netmask 0xfffffc00 broadcast 10.25.11.255
inet 192.168.0.20 netmask 0xffffff00 broadcast 255.255.255.0
Notice that you need to prefix the ifconfig command with sudo, because ifconfig needs to run with administrative privileges. When you get the password prompt, enter your user password, not the root password. (Sudo implementations on other OSes may differ. E.g., on SUSE Linux you'd need to enter the root password.)

Finally, to remove an incorrect or unneeded IP alias, do this:

Dave-Markowitzs-Computer:~ davemarkowitz$ sudo ifconfig en0 -alias 192.168.0.20
More information about how to use ifconfig can be found by reading its man page ("man ifconfig").

Incidentally, I've replaced Apple's default Terminal.app with iTerm. It's open source and more configurable than Terminal.app, and most important to me, offers tabbed console windows. I highly recommend it.

4 comments:

Anonymous said...

as you can see from the above example, my Ethernet port currently is assigned the IP address 10.25.10.51.
Nope, I cannot see that. I mean I recognise three IP addresses but the rest is even less readable than a COBOL program. :)

wallybear said...

as you can see from the above example, my Ethernet port currently is assigned the IP address 10.25.10.51

another sintax could give a more readable result:

ifconfig | grep netmask
inet 127.0.0.1 netmask 0xff000000
inet 192.168.1.10 netmask 0xffffff00 broadcast 192.168.1.255
inet 10.37.129.2 netmask 0xffffff00 broadcast 10.37.129.255
inet 10.211.55.2 netmask 0xffffff00 broadcast 10.211.55.255

Unknown said...

Thank you! Exactly what I was looking for. I'm citing this in my project for class : )

George Wilson III said...

Thank you for this great post. My daughter is trying to make a ping sweeper for school and needed to figure out what her IP address was. Since I'm on a Win box I could not tell her where to look in a Mac ping.