I have eth0 and wlan0 according to ifconfig and I can ping google.com.

How can I find out (with a normal user, not root) what interface is active, as in, what interface did the ping (or whatever, ping is not mandatory) use?

I am using Ubuntu 11.04 or Fedora 14

  • 3
    The below solutions seem to want you to do the inferring yourself, which doesn't seem right. (And everybody's routing tables look incredibly straightforward!) While I'm looking for the Windows equivalent, it appears that "ip route get <ip-address>" will tell you which interface would be used if you were to attempt to connect to a given ip address. – mwardm Aug 25 '16 at 9:41
  • Related on Server Fault: Find interface for route to specific host (which is what I was looking for when I wound up here). – Wildcard Jul 12 '19 at 22:19

10 Answers 10


You can use route to find your default route:

$ route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface     *        U     1      0        0 eth0
link-local      *          U     1000   0        0 eth0
default         UG    0      0        0 eth0

The Iface column in the line with destination default tells you which interface is used.

  • 1
    @Job in case we have an IP address how can we find which interface it uses. – Bionix1441 Nov 30 '17 at 9:51
  • 1
    This should not be the accepted answer. If iproute have been used to define custom routing rules route can't be used. @Torgeir have the wright answer. – Joseph Garrone Feb 20 '19 at 18:40
  • 1
    This answer is not correct. For example (and given the same routes displayed in the answer), if you ping (a pc on your local network), the entry which starts with is the route that will be used. Of course in this case it refers to the same Iface, but that is just because you have one interface on your machine. – kdehairy Apr 25 '19 at 19:27
  • 1
    @Job Even with google.com, if you have two routes each on a different interface, the match with the longer mask will be picked (not necessarily the default). All what default means, is that if no other route is found, let's consult this one :) – kdehairy Apr 27 '19 at 11:51
  • 2
    I get a line "default ... eth0" and a line "default ... wlan0". So which one is the real default? – AstroFloyd Jul 10 '19 at 7:44

My version which is basically based on this and this:

route | grep '^default' | grep -o '[^ ]*$'

And this, experimentally, for macOS:

route -n get default | grep 'interface:' | grep -o '[^ ]*$'
  • 1
    is there a way for you to translate this for OSX? – John Allard Sep 28 '17 at 21:41
  • 1
    @JohnAllard: let me know if my update now gives correct answer there for macOS. – Ebrahim Byagowi Oct 10 '17 at 21:11
  • you don't need multiple pipes, just use route |awk '/^default/{print $NF}' – P.... Apr 30 '19 at 15:00
  • I get two lines: "eth0" and "wlan0", so I still don't know which one is active... – AstroFloyd Jul 10 '19 at 7:46

On GNU/Linux systems:


# host we want to "reach"

# get the ip of that host (works with dns and /etc/hosts. In case we get  
# multiple IP addresses, we just want one of them
host_ip=$(getent ahosts "$host" | awk '{print $1; exit}')

# only list the interface used to reach a specific host/IP. We only want the part
# between dev and src (use grep for that)
ip route get "$host_ip" | grep -Po '(?<=(dev )).*(?= src| proto)'
  • 1
    Please explain what these commands are doing. Also, you're probably pretty safe here, because you know what your values are, but, generally, you should quote shell variables references (unless you have a good reason not to, and you’re sure you know what you’re doing). – G-Man Says 'Reinstate Monica' Oct 30 '14 at 18:51
  • 1
    Done, to some extent. Only posted this because I could not find anything that did exactly this. Am using this as a custom fact in a puppet manifest... – Torgeir Oct 30 '14 at 20:15
  • Your answer is more correct if the host is an input argument to the script. As the route (and hence the interface) might be different for different ips. – kdehairy Apr 25 '19 at 19:52

Running ifconfig will give you the information you need.

The active interface will have an inet addr and will show a record of transmitted data, like so:

RX bytes:1930741 (1.8 Mb)  TX bytes:204768 (199.9 Kb)

You can also use the ip addr command and any inactive interfaces will be designated as having: NO-CARRIER.

  • Since OP doesn't want to be root, maybe you should give full path to the executable? – tshepang Jun 14 '11 at 7:21
  • Neither command requires elevated privileges. – jasonwryan Jun 14 '11 at 7:24
  • 1
    Ubuntu and Arch: both run with normal privileges (although on Ubuntu it is /sbin) – jasonwryan Jun 14 '11 at 7:50
  • 1
    @Shadur Not on my install of Ubuntu 10.10 and according to help.ubuntu.com/community/EnvironmentVariables the default $PATH is /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games: – jasonwryan Jun 14 '11 at 18:32
  • 2
    This won't necessarily help on a system with multiple interfaces, as the poster indicated having. A better answer is using the route command though that wouldn't necessarily be foolproof either due to routing. The default route does not have to be the one to get to the Internet though it may often be. – user1801810 Aug 10 '16 at 18:36

Get the default network interface typically used to route to the "remaining" internet in opposite to DMZ, private network, VM host etc. which are usually routed explicitly.

$ ip -4 route ls | grep default | grep -Po '(?<=dev )(\S+)'
  • it is not always the default route that gets you to where you want. Although the OP most probably is using the default route to get to the internet, this is a generalization and might not always hold true. A word of caution – MelBurslan Aug 10 '16 at 17:35
  • Sure, it is only default network interface. If there are multiple connections, one should not really speak about "connecting to the internet", but rather about network interface used for routing to the exact destination. – EugeneP Aug 22 '16 at 11:06
  • You do not need to use multiple pipes/greps , ip -4 route ls |awk '/^default/{print $5}' – P.... Apr 30 '19 at 15:01

One liner:

ip route get | sed -n 's/.*dev \([^\ ]*\) table.*/\1/p'

  • There's a dangling quote there. Doesn't work at all as-is – Krease Jan 24 '18 at 19:23
  • @Krease Author updated his answer – Vadim Kotov Jul 10 '18 at 16:07

The command ip route ls will give a list of active routes and their sources:

caleburn: ~/ >ip route ls dev eth0  proto kernel  scope link  src 
default via dev eth0 

Use this command:

$ route | grep default | awk '{print $8}'
  • [root@linux1 network-scripts]# route|grep default | awk '{print $8}' enp0s3 [root@linux1 network-scripts]# – Larry Catt Aug 17 '16 at 16:46

use the below one:

ip r | grep default

If your machine has multiple interfaces (which I assume), there is no one interface that will be used for connecting to the internet.

Depending on the destination you are connecting to, your system will consult the ip table (the one shown when you do route command) to find the next hope/router, when it does find one, it will use the interface associated with it.

That said, please refer to @torgeir's answer as it does exactly that:

  1. Given a domain (hardcoded as google.com, which is the only bad point in the answer)
  2. Resolves it's ip
  3. Consults the ip table for the route to use (and hence the interface).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.