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: - - - 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:

eth0      Link encap:Ethernet  HWaddr ac:2f:f1:14:28:d3  
          UP BROADCAST MULTICAST  MTU:1500  Metric:1
          collisions:0 txqueuelen:1000 
lo        Link encap:Local Loopback  
          inet addr:  Mask:
          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:  Bcast:  Mask:
          inet6 addr: fe80::2a10:7bff:fe42:3e82/64 Scope:Link
          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
  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, 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}'

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 
    inet 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 brd 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:


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:


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

Or if you prefer curl:

#curl http://ipecho.net/plain


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

So as you can see both commands return the IP 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!

  9 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.


  2. x fedora:
    # /sbin/ifconfig | grep “netmask” | awk ‘{print $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

    It doesn’t actually connect to (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



