Networking optimize

How to find IP addresses and use them in Linux

Marco Fioretti reviews some basics about IP addresses, tells how to find them, and gives an example of why they're important for some tasks in Linux.

The Internet is a "network of networks," on which all kinds of data travel in the same way: wrapped in packets formatted according to the Internet Protocol (IP). Each IP packet carries inside the numeric IP addresses of its source and destination. Internet routers use those numbers to ship each packet to the right computer.

Here are some facts about IP addresses that many Internet users don't know, or don't take in to proper account:

  • IP addresses are all computers actually need to exchange data over the Internet1. Domain names like TechRepublic.com were created to make life for human users easier, but they are always translated to those numbers.
  • IP addresses are associated to single interfaces, not whole computers. A workstation with 6 Ethernet cards, for example, can and will very likely have six different IP addresses, one per port.
  • It is possible to intercept traffic for a certain IP address and translate it in real time to another one. This process, called port forwarding, is what ADSL or Wi-Fi routers normally do before sending their users' packets out on the Internet.
  • IP addresses can be static or dynamic. Wireless and most Fixed ADSL private users get a different IP address every time they connect to the Internet.

Why do IP addresses matter (especially on Linux)?

Linux systems come with piles of Free Software, already bundled or just a few clicks away, to offer all kinds of services over the Internet. I'm not talking only of the basic, popular stuff like email or websites. With Linux, you can provide to all your friends and coworkers custom social bookmarks, encrypted chat sessions, automatic remote backups, multiplayer games, video streaming, and much more, even from your home computer.

Unfortunately, there is a problem here, which is a direct corollary of the properties listed above. You may offer all those services even if none of the involved parties (including your own computer) has a domain name, but not without knowing all their IP addresses. Since all of those addresses may change at every session, what should you do? Collect and insert them manually, every time? Of course not! The solution is to have all the interested computers determine their own IP address, every time they boot, and send it by email to the other parties, all with a script. But...

How do I find my IP address on Linux?

I decided to write this post when I realized that most of the first links returned by search engines as answers to this question are simply useless. Strictly speaking, the tips in all those pages are correct. However, in our scenario of (mainly) home computers connected via some ADSL/Wi-Fi router, they will return your local, private IP address. This is an address that is only visible and valid before port forwarding, that is, for your router and all the local computers connected to it. People in other places won't be able to send packets to that IP address (in their local network, it may even correspond to their own PC!).

What you want to know and broadcast is the (always different) public IP address that your ISP assigns to your router every time you turn it on. Here are two ways to find it from inside a script.

Ask ifconfig.me

There is a dynamic page at ifconfig.me that extracts your current IP number from your connection request, then (click on the link to see for yourself) returns it as its own content.

Wget and Curl are Unix command line utilities to download single web pages or mirror whole websites (Curl can do much more, including filling HTML forms automatically, but we don't need that today). w3m is a textual Web browser.

The simplest method2 to load your public IP address into a shell variable is to download that page with any of these three utilities:
  MY_PUBLIC_IP_ADDRESS=`curl ifconfig.me/ip`
  MY_PUBLIC_IP_ADDRESS=`wget -qO- ifconfig.me/ip`
  MY_PUBLIC_IP_ADDRESS=`w3m -no-cookie -dump ifconfig.me/ip`

Scrape other websites

The limit of the ifconfig.me approach is that... should that website go offline, it would stop working. Luckily, there are many other Web pages, like whatismyip.com or whatismyipaddress.com that will tell you your public IP address. They just need a bit of Web scraping because, unlike ifconfig.me, they contain much more text. If you don't know what Web scraping is, please read my introduction to it. In our case, you can use Web scraping as follows:

  MY_PUBLIC_IP_ADDRESS=`w3m -no-cookie -dump http://whatismyip.com | grep '\''^Your IP Address Is:'\'' | cut -c21-`

Here, the w3m text browser fetches the Web page and prints it on its standard output. Grep discards everything but the one line containing the answer you want, and cut leaves only the actual number inside MY_PUBLIC_IP_ADDRESS. At this point, you can do whatever you want with that variable in the script, for example, sending it via email to any other script, user or computer that would need to know it.

Notes:

  1. Yes, of course Internet packets normally go to combinations of IP addresses and TCP ports. I deliberately left that part out because it wouldn't make any difference with respect to the goal of this post.
  2. Source: Command Line FU

About

Marco Fioretti is a freelance writer and teacher whose work focuses on the impact of open digital technologies on education, ethics, civil rights, and environmental issues.

4 comments
pradhikshana
pradhikshana

There are two types of ip addresses in the computer. 1. Private or internal ip address and 2. Public or external ip address. To find an internal ip address in Linux type ifconfig in the terminal and press enter then it shows the ip address,localhost etc.. To find an external ip address visit the site http://www.ip-details.com.

dosida
dosida

One of the reasons why one should track their IP address is perhaps dynamic DNS. It might be easier to get a generic dynamic DNS name associated with your IP so you can run your FTP SSH http server without having to worry about your IP changing all the time. With a bit of scripting or with a router that supports Dynamic DNS and doesn't forget to update it whenever the ADSL/Cable modem loses its settings (like mine does) Dynamic DNS can work miracles. There are lots of sites that can get your IP and spit it back...www.whatismyip.com isn't the cleanest one of all ... try checkip.dyndns.org with the following command: curl -X GET http://checkip.dyndns.org You'll get the following: Current IP Address: x.x.x.x (Where x represents each number of your external Ip address. All this is wrapped in http and body tags but the tags will dissapear once I paste them here) For local IP addresses check this out: ip -f inet addr will give you the local IP addresses for your nic and localhost in the following format: 1: lo: mtu 16436 qdisc noqueue state UNKNOWN inet 127.0.0.1/8 scope host lo 2: eth0: mtu 1500 qdisc cbq state UP qlen 1000 inet 192.168.0.10/24 brd 192.168.0.255 scope global eth0 then there's the old standby: ifconfig eth0 But for some distributions this requires higher privileges (ie root) Also look at your distribution's repositories for a Dynamic DNS client that can update your Dynamic DNS Settings every time they detect a change in the external IP address :) Hope this helps

Deadly Ernest
Deadly Ernest

however, I sincerely doubt many people will need to go about doing what you say and further doubt they'll get much use from doing so. The reason being, unless the US ISPs work a LOT different to those in Australia, every time your modem router has to be reset for any reason your public IP address is very likely to change because the ISP will assign you a new one from its group of IP addresses when you reconnect. Also, a good router will handle all this for you as part of its Network Address Translation (NAT) process and doing it in an invisible seamless manner. I see no real reason to send out your IP address as soon as you boot the system. If you need to connect to another system for the reasons stated, simply activating the appropriate application will see the connection established as and when needed. I see no point in doing that until you want it.

steamIngenious
steamIngenious

To all who are interested in this sort of thing, Google now does this. Simply type "what is my ip" into the search and see what you get back.