Bash Shell Command to Find or Get IP address
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:
- How to use bash shell
- Linux: Find my IP address using Perl at a shell prompt
- How To Find BASH Shell Array Length ( number of elements )
- FreeBSD Install BASH Shell
- How to find out Router mac address
Discussion on This FAQ
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!
Tags: 192, base address, collisions, encap, ethernet interface, ethernet ip address, ethernet_interface, ethernet_ip_address, ifconfig_command, inet addr, ipconfig command, ipconfig_command, kib, Linux, loopback, mib, mtu, overruns, rx packets, scope, subnet mask, subnet_mask, tx packets, UNIX




March 1st, 2006 at 7:24 am
ipconfig usually doesn’t work outside of windows
March 1st, 2006 at 1:07 pm
Opps! Just corrected typo since I work on both UNIX and Windows Server and sometime I get confused :(.
Thanks, I appreciate your post
July 23rd, 2007 at 2:17 pm
i worked in a company and the I.T man close the face book site what can i do
April 25th, 2008 at 12:28 pm
Use hostname -i
also find other parameters using man hostname.
June 29th, 2008 at 6:32 am
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
June 29th, 2008 at 9:27 am
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
July 2nd, 2008 at 4:32 pm
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 #
July 8th, 2008 at 8:27 pm
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 .
July 8th, 2008 at 8:30 pm
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.
July 25th, 2008 at 10:25 am
Is great!
Thanks a lot, Alejandro