|
|
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!
Categorized:
Mac OS X Help
(Article 3963,
Written by Dave Taylor)
Tagged: command line interface, dhcp, ifconfig, ip address, mac os x, terminal Previous: How do I stop Windows XP screensaver asking for my password? Next: Does Google, Yahoo and MSN index Flash content? Reader Comments To Date: 28John Bales said, on April 16, 2005 6:42 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: If you're connect via airport: erika said, on November 10, 2006 1:55 AM:
Or just use this supremely easy URL: dfcsdcf said, on February 18, 2007 5:40 AM:
i need internet on my psp! but it dnt work because the IP address is timed out how do i untime it out michael m. said, on March 12, 2007 6:02 PM:
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) Jasmine said, on January 28, 2009 5:43 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!!! Kimberley said, on March 3, 2009 9:00 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??? madhu said, on July 23, 2009 1:09 AM:
Thanks for the information about the ip address I know my own ip address from the site www.ip-details.com willa said, on July 24, 2009 10:28 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 Dave Taylor said, on July 26, 2009 12:08 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. Anonymuss said, on November 21, 2009 6:14 PM:
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? fif said, on November 26, 2009 1:25 AM:
Hi Dave and Anonymuss, Dave Taylor said, on November 26, 2009 12:16 PM:
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. LEBATO said, on April 20, 2010 5:03 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! Thul Dai said, on May 27, 2010 4:17 AM:
Thanks, extremely useful! Although John Bales' hint (2nd commentary) might beat yours out in simplicity... Darin Marshall said, on July 21, 2010 4:06 PM:
Everything worked as described in this article except this part: echo alias myip="ifconfig | grep 'inet ' | grep -v 127.0.0.1 |
-bash: myip: command not found Is this related to the path to bash? Dave Taylor said, on July 27, 2010 9:44 AM:
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. :-) dennis bracamonte said, on November 21, 2010 11:49 AM:
$ echo alias myip="ifconfig | grep 'inet ' | grep -v 127.0.0.1 | this command does not work $ alias myip="ifconfig | grep 'inet ' | grep -v 127.0.0.1 | 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 | brainbashing said, on December 28, 2010 6:13 PM:
@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 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 Tom said, on September 14, 2011 10:24 AM:
Running OS10.6.8 shoeis-computer:~ shoei$ echo alias myip="ifconfig | grep 'inet ' | grep -v 127.0.0.1 | cut -d\ -f2" >> ~/.bashrc alias myip=ifconfig | grep 'inet ' | grep -v 127.0.0.1 | cut -d\ -f2 Dave Taylor said, on September 14, 2011 11:16 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. Tom said, on September 14, 2011 12:46 PM:
Still no go... shoeis-computer:~ shoei$ . ~/.bashrc Dave Taylor said, on September 14, 2011 11:04 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! :-) Tom said, on September 15, 2011 10:50 AM:
$ tail -5 ~/.bashrc Dave Taylor said, on September 15, 2011 1:52 PM:
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. :-) Sam said, on May 6, 2012 8:16 AM:
I keep on trying but it always says, -bash: myip: command not found. Any help? Dave Taylor said, on May 6, 2012 8:32 AM:
Sam, did you add the line alias myip="ifconfig | grep 'inet ' | grep -v 127.0.0.1 | cut -d\ -f2" to your ~/.profile or ~/.bashrc, then quit your Terminal window and open up a new one? yui said, on April 15, 2013 5:52 AM:
Thank you for your help with this. It's a little task, but nice to know how to do it correctly when I need my IP address.
I do have a comment, now that you mention it!Check This Out Too... |
Recent Entries
Look for Answers
Recommended
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 Facebook Help Google Gmail Help Google Plus Help HTML, JavaScript and Web Site Programming 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 Pinterest Help 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+ |
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.