Bash Shell Command to Find or Get IP address

September 8, 2019

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

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

eth0     Link encap:Ethernet  HWaddr 00:0F:EA:91:04:07
  inet addr:  Bcast:  Mask:
  inet6 addr: fe80::20f:eaff:fe91:407/64 Scope:Link
  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:  Mask:
  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 is IP address of eth0 Ethernet interface.

Linux ip Command

It is recommend that you use the ip command under Linux based systems. The following command will show all ip address:
# ip addr show
To see information about eth0 ip address, enter:
# ip addr show eth0
Sample outputs:

2: eth0:  mtu 1500 qdisc mq state UP qlen 1000
    link/ether b8:ac:6f:65:31:e5 brd ff:ff:ff:ff:ff:ff
    inet brd scope global eth0
    inet6 fe80::baac:6fff:fe65:31e5/64 scope link 
       valid_lft forever preferred_lft forever

For more information please see howto read UNIX/Linux system IP address in a shell script.

Posted by: SXI ADMIN

The author is the creator of SXI LLC and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

Article Tags:
Article Categories:
How To

Comments are closed.