Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

NOTE: using Ubuntu 11.04 or Fedora 14

share|improve this question
up vote 21 down vote accepted

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.

share|improve this answer
On Debian at least you need to do /sbin/route. – Faheem Mitha Jun 14 '11 at 9:00
@Faheem Because /sbin isn't in normal users' PATH environ. This is pretty standard in most distros, even though it's generally one of the first things sysadmins alter for their 'main' user. – Shadur Jun 14 '11 at 11:07
@Shadur: that's not the case in Fedora (as of FC13 if I remember correctly) – nico Jun 14 '11 at 15:57

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.

share|improve this answer
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
I checked that ifconfig runs ok with normal privileges, but on Debian (and I suppose Ubuntu), it's in /sbin, and that's not part of normal user path. Is your system different? – Tshepang Jun 14 '11 at 7:35
Ubuntu and Arch: both run with normal privileges (although on Ubuntu it is /sbin) – jasonwryan Jun 14 '11 at 7:50
@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

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 
share|improve this answer

# host we want to "reach"

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

# 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)'
share|improve this answer
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 Oct 30 '14 at 18:51
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


By posting your answer, you agree to the privacy policy and terms of service.

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