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?
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
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!
More Useful Mac OS X Help Articles:
✔ Audacity can't find LAME library, I can't save Mp3?
Hey Dave. I read your article Audacity can't save mp3 audio files and am still puzzled because I downloaded the LAME Mp3 converter...✔ How to remove Dashboard as a "space" in Mac OS X Spaces?
I'm a big fan of the Spaces utility in Mac OS X that lets me have multiple virtual screens [see Set Up Mac...✔ Best place to buy a cheap MacBook laptop?
Hi Dave. I am looking for two gently used MacBook laptops for my teen daughters. Personal computers would greatly facilitate their studies as...✔ File too big error copying to USB flash drive on my Mac?
I'm baffled. I have a 16GB Kingston USB flash drive that I use on my Mac system and I'm trying to copy a...✔ Stealth image capture photo from webcam on my Mac?
Someone sneaks into my cubicle while I'm at lunch and takes candy out of my desk. Petty, but stupid too. I want to...
Let's stay in touch!
Sign up for my weekly AskDaveTaylor Newsletter and you'll receive even more tech and gadget help right to your inbox, along with exclusive news and industry updates. It's good stuff. I promise!
I do have a comment, now that you mention it!
Check This Out Too...
Look for Answers
All Our Categories
Apple iPad Help
Articles and Reviews
Auctions and Online Shopping
Blogs and Blogging
Building Web Site Traffic
Business and Management
Computer and Internet Basics
d) None of the Above
Google Gmail Help
Google Plus Help
Industry News and Trade Shows
iPhone and Cell Phone Help
iPod, Sony PSP and MP3 Player Help
Kindle Fire Help
Mac OS X Help
Pay Per Click (PPC) Advertising
Search Engine Optimization (SEO)
Shell Script Programming
Tech Support Video Help
The Writing Business
Twitter, LinkedIn and Social Network Help
Unix and Linux Help
Video Game Tips and Help
Windows PC Help
Find Me on Google+
ADT on G+