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=8049mtu 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!
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.
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?
I keep on trying but it always says, -bash: myip: command not found. Any help?
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. 🙂
$ tail -5 ~/.bashrc
alias myip=ifconfig | grep ‘inet ‘ | grep -v 127.0.0.1 | cut -d\ -f2
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! 🙂
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$
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.
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$
@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
$ 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
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. 🙂
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?
Thanks, extremely useful!
Although John Bales’ hint (2nd commentary) might beat yours out in simplicity…
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!
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.
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 http://www.whatismyip.com or http://www.ip-details.com gives, I get something like 79.85.etc.
Why is it different? Shouldn’t I get the ame result?
Cheers
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?
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.
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
Thanks for the information about the ip address I know my own ip address from the site http://www.ip-details.com
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???
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!!!
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)
i need internet on my psp! but it dnt work because the IP address is timed out how do i untime it out
Or just use this supremely easy URL:
http://www.whatismyip.com/
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
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.