Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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

4 Answers 4

up vote 17 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
192.168.1.0     *               255.255.255.0   U     1      0        0 eth0
link-local      *               255.255.0.0     U     1000   0        0 eth0
default         192.168.1.1     0.0.0.0         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
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

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

caleburn: ~/ >ip route ls
192.168.10.0/24 dev eth0  proto kernel  scope link  src 192.168.10.7 
default via 192.168.10.254 dev eth0 
share|improve this answer
#!/bin/bash

# host we want to "reach"
host=google.com

# 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
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 Oct 30 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 at 20:15

Your Answer

 
discard

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.