How to display the IP address of the default Interface with Internet connection?

16,342

Solution 1

Here's another slightly terser method using procfs (assumes you're using Linux):

default_iface=$(awk '$2 == 00000000 { print $1 }' /proc/net/route)
ip addr show dev "$default_iface" | awk '$1 ~ /^inet/ { sub("/.*", "", $2); print $2 }'

This returns both the IPv4 and (if available) the IPv6 address of the interface. You can change the test if you only want one or the other (look for inet for IPv4, and inet6 for IPv6).


$ default_iface=$(awk '$2 == 00000000 { print $1 }' /proc/net/route)
$ ip addr show dev "$default_iface" | awk '$1 ~ /^inet/ { sub("/.*", "", $2); print $2 }'
10.0.2.15
fe80::a00:27ff:fe45:b085
$ ip addr show dev "$default_iface" | awk '$1 == "inet" { sub("/.*", "", $2); print $2 }'
10.0.2.15
$ ip addr show dev "$default_iface" | awk '$1 == "inet6" { sub("/.*", "", $2); print $2 }'
fe80::a00:27ff:fe45:b085

Solution 2

Lots of good answers here, but wanted to throw in my usual approach:

The simplest solution is to get the route for a public internet address:

$ ip route get 1.1.1.1 | grep -oP 'src \K\S+'
192.168.0.20

Another solution is to get the default gateway, and then get the IP addr used to communicate with that gateway:

$ ip route get $(ip route show 0.0.0.0/0 | grep -oP 'via \K\S+') | grep -oP 'src \K\S+'
192.168.0.20

Solution 3

Here's what I wrote:

  1. Get the default interface from the "route" command.

It will print out which interface is the "default". For my host, I need to get the last column of the default line.

[root@pppdc9prd3ga mdesales]# route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.4.0     *               255.255.252.0   U     0      0        0 bridge0
10.132.60.0     *               255.255.252.0   U     0      0        0 eth4
link-local      *               255.255.0.0     U     1002   0        0 eth4
link-local      *               255.255.0.0     U     1003   0        0 bridge0
default         10.132.60.1     0.0.0.0         UG    0      0        0 eth4
  1. Use "ifconfig" to retrieve the IP address of that interface.

Just getting the addr: value.

[root@pppdc9prd3ga mdesales]# ifconfig eth4
eth4      Link encap:Ethernet  HWaddr 00:50:56:01:42:91  
          inet addr:10.132.63.191  Bcast:10.132.63.255  Mask:255.255.252.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:1346288 errors:0 dropped:0 overruns:0 frame:0
          TX packets:438844 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:276243478 (263.4 MiB)  TX bytes:116188062 (110.8 MiB)

So here's the script I came up with.

/app/myPublicIp.sh 
defaultInterface=$(route | grep default | awk '{print $(NF)}')
ifconfig $defaultInterface | grep 'inet addr:' | cut -d: -f2 |  awk '{ print $1}'

Here's it executing:

/app/ipFor.sh 
10.132.63.191

Solution 4

My favorite one is following.

Get the default interface:

$ ip r | grep -oP 'default .* \K.+'
eth0

Get the ip of an interface:

$ ip a show eth0 | grep -oP 'inet \K[\d\.]+'
10.33.44.135

Combined:

$ ip a show $(ip r | grep -oP 'default .* \K.+') | grep -oP 'inet \K[\d\.]+'
10.33.44.135

Solution 5

One other approach is:

ip a|awk /$(ip r|awk '/default/ { print $5 }')/|awk '/inet/ { print $2 }'| cut -f1 -d"/"

the benefit of this is that uses only ip a and ip r that is available by default at all linux systems

Share:
16,342

Related videos on Youtube

Marcello de Sales
Author by

Marcello de Sales

Passionate Software Engineer working with different technologies. Professional info at https://linkedin.com/in/marcellodesales

Updated on September 18, 2022

Comments

  • Marcello de Sales
    Marcello de Sales over 1 year

    I need to create a script that outputs the internal IP address, that is configured as the default Interface.

    • Admin
      Admin over 9 years
      Do you mean the one used for sending to the default gateway?
    • Admin
      Admin over 9 years
    • Admin
      Admin over 9 years
      @jasonwryan not a dupe (or not a dupe of that one anyway), the OP wants the internal IP, not the external.
    • Admin
      Admin over 9 years
      Hauke, this might be it, if the interpretation of the "default" value from the route command is that that is.
    • Admin
      Admin over 9 years
      yeah, Jason that has dependencies to external libraries. I was looking for the output without dependencies.
    • Admin
      Admin over 9 years
      @MarcellodeSales no, it has no dependencies. It is just looking for something different. You are showing how to get the IP of a machine in the internal network while the dupe is about getting the external IP. Two very different things. Jason was confused because your original question was asking for a "public" IP which is not what your answer returns.
  • terdon
    terdon over 9 years
    Nice, +1. You could simplify (well, shorten it anyway) that to ifconfig $(route | grep -oP '^default.*\s+\K.*') | grep -oP 'inet addr:\K[^\s]+')
  • clerksx
    clerksx over 9 years
    If you're going to do this on Linux, you might consider using procfs instead of parsing :-)
  • Marcello de Sales
    Marcello de Sales over 9 years
    @ChrisDown... would that be via /proc/net? Yeah, this is for a Docker cluster.
  • clerksx
    clerksx over 9 years
    @MarcellodeSales Yeah, /proc/net/route. I've posted an answer to that effect.
  • Andreas Foteas
    Andreas Foteas over 2 years
    the issue is that you need to have installed ifconfig
  • Admin
    Admin almost 2 years
    Unfortunately these fail on RHEL/CentOS 8 as the ip r output has changed slightly, with the metric being the last entry on the line and not the dev ensNNN or dev ethN.
  • Admin
    Admin almost 2 years
    Unfortunately this fails on RHEL/CentOS 8 as the ip r output has changed slightly, with the metric being the last entry on the line and not the dev ensNNN or dev ethN so it prints the whole line instead of just the IP.
  • Admin
    Admin almost 2 years
    @dragon788 - Please read my updated answer. Hope this should work!!