Jan 272013
 

Both for a real server, VPS or just your desktop is useful to know which IP address your Linux system is using , this can be easily seen with some command if you are directly connected to Internet via a modem or a public IP of your company, but sometimes you are behind a router, a device that forwards data packets between computer networks, creating an overlay internetwork. A router is connected to two or more data lines from different networks, and at home it’s used to “share” your ADSL that usually has 1 public IP among all your devices, that will get each a private IP. For example at my home I’ve an ADSL Wi-Fi router that I use to get Wi-Fi connection to my 3 PC, 1 smartphone and 1 tablet (android), a printer and my kindle, every device has a private IP , it’s called private because it’s not visible on the public network that can only “see” the IP of my Wi-Fi router, but now let’s see how to check your IP on your Linux computers.

Get your computer IP

As wrote depending on your network configuration this can be a public IP or private IP, I suggest to read the Wikipedia article regarding private network to get more information about this kind of network, to make it short your IP is private if you see a number in these ranges, for the skilled people I’m talking about IPV4 only in this article: 10.0.0.0 - 10.255.255.255 172.16.0.0 - 172.31.255.255 192.168.0.0 - 192.168.255.255 There are at least 2 way to get your IP address:

1) This command is deprecated, but is still used by all system administrator: ifconfig

Just open a terminal and type /sbin/ifconfig you’ll get an output similar to this one:

#/sbin/ifconfig
eth0      Link encap:Ethernet  HWaddr ac:2f:f1:14:28:d3  
          UP BROADCAST MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)
 
lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:7305 errors:0 dropped:0 overruns:0 frame:0
          TX packets:7305 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:934870 (934.8 KB)  TX bytes:934870 (934.8 KB)
 
wlan0     Link encap:Ethernet  HWaddr 18:11:7c:12:3e:82  
          inet addr:192.168.1.4  Bcast:192.168.1.255  Mask:255.255.255.0
          inet6 addr: fe80::2a10:7bff:fe42:3e82/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:4742986 errors:0 dropped:0 overruns:0 frame:0
          TX packets:6167198 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:3913304848 (3.9 GB)  TX bytes:1211469656 (1.2 GB)

From this output i can get the following information:

  1. The computer has 2 real network devices: eth0 and wlan0 and the standard lo (loopback) device this is a special device used for tasks that involve only your machine and always uses the IP address 127.0.0.1.
  2. The network device eth0 is not connected or not configured, as you can see it don’t has any field named inet addr that should contains it’s IP address. In my case this is the 100MB ethernet device of the motherboard that I don’t use.
  3. The network device wlan0 has Ip address 192.168.1.4, so I know that it uses a private network to connect to a router.

If you just want to get your IP address without all the other information you can use this small shell script:

/sbin/ifconfig | grep "inet addr" | awk -F: '{print $2}' | awk '{print $1}'
127.0.0.1
192.168.1.4

This small script usually works on all Debian, Ubuntu and Mint computers, but on other distro that have changed the output of ifconfig this could not work (i.e. Fedora).

2) the iproute2 suite

iproute2 is a collection of utilities for controlling TCP and UDP IP networking and traffic control in Linux, in both IPv4 and IPv6 networks and it’s the natural heir of the net-tools suite.

The equivalent command for ifconfig is ip addr that gives a similar output :

#ip addr
1: lo: <loopback ,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eth0: <no -CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc pfifo_fast state DOWN qlen 1000
    link/ether bc:5f:f4:12:18:d3 brd ff:ff:ff:ff:ff:ff
3: wlan0: <broadcast ,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000
    link/ether 18:11:7c:12:3e:82 brd ff:ff:ff:ff:ff:ff
    inet 192.168.0.4/24 brd 192.168.0.255 scope global wlan0
    inet6 fe80::2a10:7bff:fe42:3e82/64 scope link </broadcast></no></loopback>
       valid_lft forever preferred_lft forever

As you can see the format is slightly different but you get the same information, to get just the Ip addresses use this small shell script:

ip addr | awk '/inet / {sub(/\/.*/, "", $2); print $2}'
127.0.0.1
192.168.0.4


Get your public IP

So now we know that we are using a private network, but how we can know our real IP address ?
The best way is to use a public website that will show the Ip of the requesting computer, this is a good way to know the Ip address that you are using to surf the net, the following are just some examples with some of the most known websites that offer this service:

http://ipecho.net/plain

This website show at the url /plain just the Ip address so it’s useful when used with in a shell script, to get this information you can use one of these commands:

#wget -qO- http://ipecho.net/plain
79.35.56.153

Or if you prefer curl:

#curl http://ipecho.net/plain
79.35.56.153

ifconfig.me

If you browse this website with your normal browser it will show how to get a lot of information from the command line, using curl, to just get the IP you can simply use:

#curl ifconfig.me
79.35.56.153

So as you can see both commands return the IP 79.35.56.153 as my public IP and it’s really easy to get your public IP with just 1 command on the linux terminal.

Popular Posts:

Flattr this!

  10 Responses to “Get your private and public IP from the Linux terminal”

  1. Hello,

    I have tested your command :
    /sbin/ifconfig | grep “inet addr” | awk -F: ‘{print $2}’ | awk ‘{print $1}’
    on fedora 15 and it works ! It has the same output as your example.

    Fatiha

  2. x fedora:
    # /sbin/ifconfig | grep “netmask” | awk ‘{print $2}’
    127.0.0.1
    192.168.55.2

  3. Here’s a useful one…. If you have multiple interfaces and switch between them sometimes (eth0 for work, wlan1 for home, tun0 for vpn) and you want to get the ip and gateway of the interface actually used for internet try this:

    ip route get 8.8.8.8

    It doesn’t actually connect to 8.8.8.8 (googles DNS server), it just shows you the ip route the connection to that ip would take.

  4. If you go to http://miniupnp.tuxfamily.org
    you will get a library which gives you upnp commands
    The command upnpc -l gives you a lot of upnp info about your dsl router, including your external IP.

    No need for an external site to feed you the data, just a simple compilation

 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

(required)

(required)

*