About Linux FAQ

Browse More FAQs:

Advertisements

Bash Shell Command to Find or Get IP address

Posted by Vivek Gite [Last updated: November 29, 2007]

Q. How do I find out my Linux / UNIX system ip address, subnet and related networking information from a command prompt?

A. To find out IP address of Linux/UNIX/BSD/Unixish system you need to use command called ifconfig. It is used to configure the kernel-resident network interfaces. It is used at boot time to set up interfaces as necessary. After that, it is usually only needed when debugging or when system tuning is needed. If no arguments are given to ifconfig command it displays the status of the current active interfaces. It displays Ethernet IP address, Mac address, subnet mask and other information. Type /sbin/ipconfig command to display IP address:

$ /sbin/ifconfig
OR type the following command:
$ /sbin/ifconfig | less

Under Solaris and other Unixish oses you may need to type ifconfig command with -a option as follows:
$ /sbin/ifconfig -a

Output of above commands:

eth0     Link encap:Ethernet  HWaddr 00:0F:EA:91:04:07
  inet addr:192.168.1.2  Bcast:192.168.1.255  Mask:255.255.255.0
  inet6 addr: fe80::20f:eaff:fe91:407/64 Scope:Link
  UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
  RX packets:31167 errors:0 dropped:0 overruns:0 frame:0
  TX packets:26404 errors:0 dropped:0 overruns:0 carrier:0
  collisions:0 txqueuelen:1000
  RX bytes:38338591 (36.5 MiB)  TX bytes:3538152 (3.3 MiB)
  Interrupt:18 Base address:0xc000

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:1994 errors:0 dropped:0 overruns:0 frame:0
  TX packets:1994 errors:0 dropped:0 overruns:0 carrier:0
  collisions:0 txqueuelen:0
  RX bytes:188041 (183.6 KiB)  TX bytes:188041 (183.6 KiB)

In above example 192.168.1.2 is IP address of eth0 Ethernet interface. For more information please see Howto read UNIX/Linux system IP address in a shell script.

Subscribe to our free e-mail newsletter or RSS feed to get all updates. You can Email this page to a friend.

Related Other Helpful FAQs:

Discussion on This FAQ

  1. Anonymous Says:

    ipconfig usually doesn’t work outside of windows

  2. nixcraft Says:

    Opps! Just corrected typo since I work on both UNIX and Windows Server and sometime I get confused :(.

    Thanks, I appreciate your post :)

  3. saeed Says:

    i worked in a company and the I.T man close the face book site what can i do

  4. Jiten Mistry Says:

    Use hostname -i
    also find other parameters using man hostname.

  5. Oscar Says:

    hostname -i doen’t get the internet ip address:

    ?????:~ # hostname -i
    127.0.0.2

    it gives me the lo ip address.

    I want to get the internet ip address from a command.

    Regards
    Oscar

  6. Oscar Says:

    I have found this way to get my ip public address (I have two IPs in my eth0 card for NAT):
    xxxx:~ # ifconfig|sed -n “/inet addr:.*255.255.255.255/{s/.*inet addr://; s/ .*//; p}”

    Regards,
    Oscar

  7. Oscar Says:

    I’m trying OpenSuSE LiveCD 64Bits (Linux linux 2.6.25.5-1.1-default #1 SMP 2008-06-07 01:55:22 +0200 x86_64 x86_64 x86_64 GNU/Linux)

    and the previous command doesn’t work. I have different broadcast address, so I get it with the mask (that is the same):

    linux:/home/linux # ifconfig eth0 | sed -n “/inet addr:.*255.255.248.0/{s/.*inet addr://; s/.*//; p}”
    xxx.xxx.xxx.xxx
    linux:/home/linux #

  8. Alejandro Smith Says:

    Oscar, try this.

    root@azrael:~# ifconfig eth0 | grep inet | awk ‘{print $2}’ | sed ’s/addr://’

    Where eth0 is the interface you want to know the ip address. In my case was eth0. This will retrieve the Ip Address from the specified interface. If you have ipv6 active and you only use ipv4 Address, then add a grep . to the end of the sentence.
    it would be like this:
    root@azrael:~# ifconfig eth0 | grep inet | awk ‘{print $2}’ | sed ’s/addr://’ | grep .

  9. Alejandro Smith Says:

    I use the above to retrieve the Ip address (dynamic one) from my ISP in order to forward the HTTP petitions to the web server (behind a firewall). Hope it works for you.

  10. Oscar Says:

    Is great!

    Thanks a lot, Alejandro

Leave a Reply

We encourage your comments, and suggestions. But please stay on topic, be polite, and avoid spam. Thank you very much for stopping by our site!

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word

Tags: , , , , , , , , , , , , , , , , , , , , , , ,

Copyright © 2006-2008 nixCraft. All rights reserved - TOS/Disclaimer - Privacy policy - Sitemap - Powered by Open source software.