Free tech support / small logo


How do I figure out my IP address on a Mac?

Dave, as far as I know, I get a new IP address every time I connect to the Internet with my Apple PowerBook. How do I figure out what address I've been assigned?


Dave's Answer:

If you're running Dynamic Host Configuration Protocol (DHCP) then you're right, you'll get a new IP address (possibly recycled) each time you connect to the Internet. Well, you actually get what's called a "lease", so you only get a new address when your lease expires. Typically DHCP servers are configured to give 24 hour leases, so it's not quite as much a moving target.

The easiest way to identify your IP address is to pop open the Terminal (go to Applications -> Utilities -> Terminal) and type in the interface configuration (ifconfig) command.

There are some utilities and apps you can use, and for that matter you can also go to "System Preferences..." off the Apple menu and look at your "Network" panel, but let's stick with "ifconfig" because it's a bit more interesting to use the command line...

$ ifconfig
lo0: flags=8049 mtu 16384
        inet6 ::1 prefixlen 128 
        inet6 fe80::1 prefixlen 64 scopeid 0x1 
        inet 127.0.0.1 netmask 0xff000000 
gif0: flags=8010 mtu 1280
stf0: flags=0<> mtu 1280
en0: flags=8863 mtu 1500
        ether 00:30:65:3d:e8:10 
        media: autoselect ()
        supported media: none autoselect 10baseT/UTP ...
en1: flags=8863 mtu 1500
        inet6 fe80::230:65ff:fe03:25bc prefixlen 64 scopeid 0x5 
        inet 10.0.0.104 netmask 0xffffff00 broadcast 10.0.0.255
        ether 00:30:65:03:25:bc 
        media: autoselect status: active
        supported media: autoselect
fw0: flags=8822 mtu 2030
        lladdr 00:30:65:ff:fe:3d:e8:10 
        media: autoselect  status: inactive
        supported media: autoselect 

The number you want to identify is immediately after the "inet" field. Rather than just scan this visually, however, let's use some Unix commands to extract the data we want.

The first command we'll use is grep, a simple pattern matching filter. We'll make what Unix geeks call a "pipe" by separating the two commands with the "|" symbol, which causes the output of the first command to be fed to the second command as its input. Put them together:

$ ifconfig | grep "inet"
        inet6 ::1 prefixlen 128 
        inet6 fe80::1 prefixlen 64 scopeid 0x1 
        inet 127.0.0.1 netmask 0xff000000 
        inet6 fe80::230:65ff:fe03:25bc prefixlen 64 scopeid 0x5 
        inet 10.0.0.104 netmask 0xffffff00 broadcast 10.0.0.255

A lot better already! Now, let's narrow it down to just the "inet" fields, not the "inet6" (which is actually IPv6, but that's beyond the scope of this discussion) by adding a space to the pattern:

$ ifconfig | grep "inet "
        inet 127.0.0.1 netmask 0xff000000 
        inet 10.0.0.104 netmask 0xffffff00 broadcast 10.0.0.255

Almost done. The second line has the real IP information for my computer because the IP address 127.0.0.1 is special, it's called your "loopback" address and always refers to your own computer, regardless of if you are running a Mac, PC, Linux or any other sort of machine. Just part of the definition of the underlying TCP/IP protocol.

To get rid of that spurious match, I'll use grep again, but this time I'll include the '-v' flag, which reverses the logic of the search (that is, it'll match all lines that do not match the specified pattern):

$ ifconfig | grep "inet " | grep -v 127.0.0.1
        inet 10.0.0.104 netmask 0xffffff00 broadcast 10.0.0.255

That's short and sweet. One more step, just a little one, to remove the stuff we don't really care about in the output, okay? For this, I'm going to use cut, a great command line utility, to show me just the second field in the line, using spaces as a delimiter:

$ ifconfig | grep "inet " | grep -v 127.0.0.1 | cut -d\  -f2
10.0.0.104

Perfect! Now, final step, save this as a Bash alias by typing in the following (exactly):

$ echo 'alias myip="ifconfig | grep 'inet ' | grep -v 127.0.0.1 | cut -d\   -f2"' >> ~/.bashrc

Exit your Terminal window and launch a new one, and from this time forward you'll always be able to simply type myip to find out what your current IP address is, all within the terminal!

Hope this helps you out!









Subscribe!
Never miss another Q&A article! Click to subscribe: Add to Google Reader Add to My Yahoo! Subscribe in NewsGator RDF XML
Comments

Three other ways to get your IP Address on OS X:

1. Run the Network Utility (under Applications | Utilities ). Your IP address is in the Info tab, on the left, next to IP Address(es). You may need to change the "Network Interface"

2. Open up "Network Preferences" on the Location menu (under the Apple). Your IP Address will be in the List of different connections.

3. Use the "Net monitor" utility available from http://homepage.mac.com/rominar/net.html . It can be set to put a graph in your menu bar. Move your mouse over the graph and it will show a window with your IP Address.

Posted by: Michael Clark at April 13, 2005 1:59 AM

Assuming you have a single ethernet interface and an airport interface. Using Terminal.app you can use ipconfig.

If you're connected via ethernet:
$ ipconfig getifaddr en0

If you're connect via airport:
$ ipconfig getifaddr en1

Posted by: John Bales at April 16, 2005 6:42 AM

Or just use this supremely easy URL:
http://www.whatismyip.com/

Posted by: erika at November 10, 2006 1:55 AM

i need internet on my psp! but it dnt work because the IP address is timed out how do i untime it out

Posted by: dfcsdcf at February 18, 2007 5:40 AM

dave, on my PSP my IP address times out.... so I type it in manually, but I dont know how to fine my Primary DNS and my Default router.

P.S.(I have a Mac OS X)

Posted by: michael m. at March 12, 2007 6:02 PM

my aautomatic ip address,dns won't work how do i make it work.do i go to manual ip address and dns and make one? its for my psp...please help me!!!

Posted by: Jasmine at January 28, 2009 5:43 PM

I am getting emails from a cyberstalker.... I have a restraining order against him and he continues to violate the restraining order. How do I find the ip address of the location of where he is sending these emails from ? and how do I trace the email address that he is using back to his orginal email address which has his information all over it???

Posted by: Kimberley at March 3, 2009 9:00 PM

Thanks for the information about the ip address I know my own ip address from the site www.ip-details.com

Posted by: madhu at July 23, 2009 1:09 AM

Could you please just explain in two sentences or less what an IP address is? I am getting a notice everyday that I must upgrade my IP address

Posted by: willa at July 24, 2009 10:28 AM

Sure, Willa. IP = Internet Protocol. It's the bottom part of "TCP/IP" (TCP = Transmission Control Protocol). As I understand it, IP relates to how information is sent point-to-point from one computer to another on the network, and TCP is how that information is "packaged up" for transmission and receipt. So IP is like a phone line, while a fax machine's message is like TCP.

Posted by: Dave Taylor at July 26, 2009 12:08 AM

Well Dave, I followed your directions in the Terminal and while the grep command is nifty, I not sure your instructions yield a user's actual IP address. In my case, I also got the 10.0.X.X result, and this is confirmed in the Network Utility application's Info tab. But my actual IP address apparently begins with 75. This is confirmed with the URLs provided by Erika and Madhu, and by simply editing in Wikipedia without logging in (they capture and display it). So, I'm confused now -- are there two IP addresses?

Posted by: Anonymuss at November 21, 2009 6:14 PM

Hi Dave and Anonymuss,
I have the same problem than Anonymuss.
When i get my IP address with your command i get something like: 192.168.etc
And when I check with what www.whatismyip.com or www.ip-details.com gives, I get something like 79.85.etc.
Why is it different? Shouldn't I get the ame result?
Cheers

Posted by: fif at November 26, 2009 1:25 AM

fif, most likely what's happening is that you have a router or gateway that's also acting as a NAT and DHCP server. That is, the box is protecting your individual machines from the Internet by mapping all of your internal addresses (for multiple machines, for example) into a single address. That's usually 192.* and it's all normal. Basically you can just use the router's address (what whatismyip.com reports) and you should be fine.

Posted by: Dave Taylor at November 26, 2009 12:16 PM

Ok, I don't know why you have to make it so hard for people new to IPs. For those asking about having two different IPs. Your address in 10.x.x.x or 192.x.x is your private address. It is what your home dsl box, router, or cable modem gives you. To go out on the internet you need a different kind of IP address, one that isn't private, but public.

Your public address is what whatismyip.com will show you, this is 99% of the time not the address of your computer, but the address your router/dsl/cable modem has.

Public addresses are unique, no two are the same at the same time. Private addresses can repeat (for example, your friend might have the exact same ip address on his PC, but never the same public address).

Anyways, cool article author!

Posted by: LEBATO at April 20, 2010 5:03 PM

Thanks, extremely useful!

Although John Bales' hint (2nd commentary) might beat yours out in simplicity...

Posted by: Thul Dai at May 27, 2010 4:17 AM

Everything worked as described in this article except this part:

echo alias myip="ifconfig | grep 'inet ' | grep -v 127.0.0.1 |
cut -d\ -f2" >> ~/.bashrc


When I did this, typing myip returned:

-bash: myip: command not found

Is this related to the path to bash?

Posted by: Darin Marshall at July 21, 2010 4:06 PM

Darin, if you don't to create an alias for "myip" then you don't have to worry about it, otherwise you've got your quotes wrong OR you're typing this in on two lines. :-)

Posted by: Dave Taylor at July 27, 2010 9:44 AM

$ echo alias myip="ifconfig | grep 'inet ' | grep -v 127.0.0.1 |
cut -d\ -f2" >> ~/.bashrc

this command does not work

$ alias myip="ifconfig | grep 'inet ' | grep -v 127.0.0.1 |
cut -d\ -f2" >> ~/.bashrc

but this one does!

i dont know why he had echo? but remove it and type excactly this and it should work.

alias myip="ifconfig | grep 'inet ' | grep -v 127.0.0.1 |
cut -d\ -f2" >> ~/.bashrc

Posted by: dennis bracamonte at November 21, 2010 11:49 AM

@dave taylor

he echo'ed the command so it will be appended (that's what >> is all about) to the .bashrc configuration file that the alias will load every time you use the command line. If you only type alias name="command -flag -flag | pipe | pipe" you will just set the alias for the current session.

I am even using a different approach (I have a fairly frequent use of aliases) in importing the aliases from another file so it won't clutter the .bashrc file. I called it .bash_aliases and import it with the following lines (I add to the .bashrc or .bash_profile):

if [ -f ~/.bash_aliases ]; then
. ~/.bash_aliases
fi

This will cause the file .bash_aliases being loaded (if it exists) every time .bashrc or .bash_profile is executed.

you can also add the alias to the .bashrc or .bash_profile with opening the desired file with nano, and editing it directly:

nano ~/.bashrc

Posted by: brainbashing at December 28, 2010 6:13 PM

Running OS10.6.8

shoeis-computer:~ shoei$ echo alias myip="ifconfig | grep 'inet ' | grep -v 127.0.0.1 | cut -d\ -f2" >> ~/.bashrc
shoeis-computer:~ shoei$ myip
-bash: myip: command not found
shoeis-computer:~ shoei$ cat .bashrc

alias myip=ifconfig | grep 'inet ' | grep -v 127.0.0.1 | cut -d\ -f2
shoeis-computer:~ shoei$ vi .bashrc
(I removed the blank first line)
shoeis-computer:~ shoei$ myip
-bash: myip: command not found
shoeis-computer:~ shoei$

Posted by: Tom at September 14, 2011 10:24 AM

That's all correct, Tom. Either log out and log in again to your Terminal window (xterm, etc) or you have to force the shell to interpret the new rc file *within its own process*. By just using something like "sh .bashrc" you spawn a subshell, that processes the new alias, then the subshell is killed because it's done with its work.

Instead, try this:

. .bashrc

or, if that complains it can't find the rc file:

. ~/.bashrc

Now type

aliases

and you should see "myip" show up.

Posted by: Dave Taylor at September 14, 2011 11:16 AM

Still no go...

shoeis-computer:~ shoei$ . ~/.bashrc
shoeis-computer:~ shoei$ aliases
-bash: aliases: command not found
shoeis-computer:~ shoei$ . .bashrc
shoeis-computer:~ shoei$ aliases
-bash: aliases: command not found
shoeis-computer:~ shoei$ alias
shoeis-computer:~ shoei$ ifconfig | grep 'inet ' | grep -v 127.0.0.1 | cut -d\ -f2
180.168.10.54
shoeis-computer:~ shoei$

Posted by: Tom at September 14, 2011 12:46 PM

Tom, what do you get when you do this:

tail -5 ~/.bashrc

and I admit, the command to see all the aliases is "alias", not "aliases" as I suggested earlier. If only I could edit these darn comments! :-)

Posted by: Dave Taylor at September 14, 2011 11:04 PM

$ tail -5 ~/.bashrc
alias myip=ifconfig | grep 'inet ' | grep -v 127.0.0.1 | cut -d\ -f2

Posted by: Tom at September 15, 2011 10:50 AM

Ah, I understand now, Tom. The problem is that what the shell's seeing is:

alias myip=ifconfig

then the rest of it is a command it actually executes, which is definitely wrong. Add double quotes, one before "ifconfig" and one after "-f2" -- in the .bashrc -- and see if that works better. :-)

Posted by: Dave Taylor at September 15, 2011 1:52 PM

I have something to say, now that you mention it, but ...
Starbucks coffee cup I do have a lot to say, and questions of my own for that matter, but first I'd like to say thank you for all your efforts on this Web site by buying you a cup of coffee!

I do have a comment, now that you mention it!











Remember personal info?


Please note that I will never send you any unsolicited email. Ever.

While I'm at it, please note that by submitting a question or comment you're agreeing to my terms of service, which are: you relinquish any subsequent rights of ownership to your material by submitting it on this site.









Recent Entries


Search
I Need Help!
Need Help? Ask Dave Taylor!


© 2002 - 2012 by Dave Taylor. All Rights Reserved.

Note: This web site is for the purpose of disseminating information for educational purposes, free of charge, for the benefit of all visitors. We take great care to provide quality information. However, we do not guarantee, and accept no legal liability whatsoever arising from or connected to, the accuracy, reliability, currency or completeness of any material contained on this web site or on any linked site.

[whiteboard marker tray]
"Ask Dave Taylor®" is a registered trademark of Intuitive Systems, LLC.