How to find out which interface am I using for connecting to the internet?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP












35














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



















  • 2




    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















35














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



















  • 2




    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













35












35








35


10





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















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







linux routing






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jun 14 '11 at 7:19









Tshepang

25.6k71182263




25.6k71182263










asked Jun 14 '11 at 6:25









LanceBaynes

10.2k75195322




10.2k75195322







  • 2




    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












  • 2




    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







2




2




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




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










8 Answers
8






active

oldest

votes


















42














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










  • @Job in case we have an IP address how can we find which interface it uses.
    – Bionix1441
    Nov 30 '17 at 9:51


















22














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 '[^ ]*$'





share|improve this answer


















  • 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











  • @EbrahimByagowi yes, it does! thanks!
    – John Allard
    Oct 10 '17 at 21:16


















5














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






  • 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



















3














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+)'
eth0





share|improve this answer






















  • 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


















3














On GNU/Linux systems:



#!/bin/sh

# 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 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)'





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 '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



















2














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




























    2














    One liner:



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






    share|improve this answer






















    • There's a dangling quote there. Doesn't work at all as-is
      – Krease
      Jan 24 at 19:23










    • @Krease Author updated his answer
      – Vadim Kotov
      Jul 10 at 16:07


















    1














    Use command:



    [root@linux1 network-scripts]# route|grep default | awk 'print $8'
    enp0s3





    share|improve this answer






















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










    Your Answer








    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "106"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f14961%2fhow-to-find-out-which-interface-am-i-using-for-connecting-to-the-internet%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    8 Answers
    8






    active

    oldest

    votes








    8 Answers
    8






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    42














    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










    • @Job in case we have an IP address how can we find which interface it uses.
      – Bionix1441
      Nov 30 '17 at 9:51















    42














    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










    • @Job in case we have an IP address how can we find which interface it uses.
      – Bionix1441
      Nov 30 '17 at 9:51













    42












    42








    42






    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












    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












    share|improve this answer



    share|improve this answer










    answered Jun 14 '11 at 7:04









    Job

    1,44711314




    1,44711314











    • 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










    • @Job in case we have an IP address how can we find which interface it uses.
      – Bionix1441
      Nov 30 '17 at 9:51
















    • 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










    • @Job in case we have an IP address how can we find which interface it uses.
      – Bionix1441
      Nov 30 '17 at 9:51















    On Debian at least you need to do /sbin/route.
    – Faheem Mitha
    Jun 14 '11 at 9:00




    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




    @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




    @Shadur: that's not the case in Fedora (as of FC13 if I remember correctly)
    – nico
    Jun 14 '11 at 15:57












    @Job in case we have an IP address how can we find which interface it uses.
    – Bionix1441
    Nov 30 '17 at 9:51




    @Job in case we have an IP address how can we find which interface it uses.
    – Bionix1441
    Nov 30 '17 at 9:51













    22














    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 '[^ ]*$'





    share|improve this answer


















    • 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











    • @EbrahimByagowi yes, it does! thanks!
      – John Allard
      Oct 10 '17 at 21:16















    22














    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 '[^ ]*$'





    share|improve this answer


















    • 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











    • @EbrahimByagowi yes, it does! thanks!
      – John Allard
      Oct 10 '17 at 21:16













    22












    22








    22






    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 '[^ ]*$'





    share|improve this answer














    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 '[^ ]*$'






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Dec 13 at 7:41

























    answered Sep 4 '16 at 11:43









    Ebrahim Byagowi

    32125




    32125







    • 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











    • @EbrahimByagowi yes, it does! thanks!
      – John Allard
      Oct 10 '17 at 21:16












    • 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











    • @EbrahimByagowi yes, it does! thanks!
      – John Allard
      Oct 10 '17 at 21:16







    1




    1




    is there a way for you to translate this for OSX?
    – John Allard
    Sep 28 '17 at 21:41




    is there a way for you to translate this for OSX?
    – John Allard
    Sep 28 '17 at 21:41




    1




    1




    @JohnAllard: let me know if my update now gives correct answer there for macOS.
    – Ebrahim Byagowi
    Oct 10 '17 at 21:11





    @JohnAllard: let me know if my update now gives correct answer there for macOS.
    – Ebrahim Byagowi
    Oct 10 '17 at 21:11













    @EbrahimByagowi yes, it does! thanks!
    – John Allard
    Oct 10 '17 at 21:16




    @EbrahimByagowi yes, it does! thanks!
    – John Allard
    Oct 10 '17 at 21:16











    5














    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






    • 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
















    5














    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






    • 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














    5












    5








    5






    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












    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












    share|improve this answer



    share|improve this answer










    answered Jun 14 '11 at 6:48









    jasonwryan

    49.1k14134184




    49.1k14134184











    • 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

















    • 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
















    Since OP doesn't want to be root, maybe you should give full path to the executable?
    – Tshepang
    Jun 14 '11 at 7:21




    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




    Neither command requires elevated privileges.
    – jasonwryan
    Jun 14 '11 at 7:24




    1




    1




    Ubuntu and Arch: both run with normal privileges (although on Ubuntu it is /sbin)
    – jasonwryan
    Jun 14 '11 at 7:50




    Ubuntu and Arch: both run with normal privileges (although on Ubuntu it is /sbin)
    – jasonwryan
    Jun 14 '11 at 7:50




    1




    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




    @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




    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





    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












    3














    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+)'
    eth0





    share|improve this answer






















    • 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















    3














    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+)'
    eth0





    share|improve this answer






















    • 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













    3












    3








    3






    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+)'
    eth0





    share|improve this answer














    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+)'
    eth0






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Aug 22 '16 at 12:14

























    answered Aug 10 '16 at 17:07









    EugeneP

    312




    312











    • 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
















    • 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















    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




    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




    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











    3














    On GNU/Linux systems:



    #!/bin/sh

    # 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 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)'





    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 '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
















    3














    On GNU/Linux systems:



    #!/bin/sh

    # 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 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)'





    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 '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














    3












    3








    3






    On GNU/Linux systems:



    #!/bin/sh

    # 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 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)'





    share|improve this answer














    On GNU/Linux systems:



    #!/bin/sh

    # 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 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)'






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Jun 29 '17 at 11:32









    Stéphane Chazelas

    298k54563912




    298k54563912










    answered Oct 30 '14 at 18:29









    Torgeir

    312




    312







    • 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 '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













    • 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 '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








    1




    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 '14 at 18:51




    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





    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












    2














    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

























      2














      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























        2












        2








        2






        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












        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












        share|improve this answer



        share|improve this answer










        answered Jun 14 '11 at 11:09









        Shadur

        19.4k74357




        19.4k74357





















            2














            One liner:



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






            share|improve this answer






















            • There's a dangling quote there. Doesn't work at all as-is
              – Krease
              Jan 24 at 19:23










            • @Krease Author updated his answer
              – Vadim Kotov
              Jul 10 at 16:07















            2














            One liner:



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






            share|improve this answer






















            • There's a dangling quote there. Doesn't work at all as-is
              – Krease
              Jan 24 at 19:23










            • @Krease Author updated his answer
              – Vadim Kotov
              Jul 10 at 16:07













            2












            2








            2






            One liner:



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






            share|improve this answer














            One liner:



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







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Feb 1 at 7:08

























            answered Oct 3 '17 at 18:55









            h0tw1r3

            44327




            44327











            • There's a dangling quote there. Doesn't work at all as-is
              – Krease
              Jan 24 at 19:23










            • @Krease Author updated his answer
              – Vadim Kotov
              Jul 10 at 16:07
















            • There's a dangling quote there. Doesn't work at all as-is
              – Krease
              Jan 24 at 19:23










            • @Krease Author updated his answer
              – Vadim Kotov
              Jul 10 at 16:07















            There's a dangling quote there. Doesn't work at all as-is
            – Krease
            Jan 24 at 19:23




            There's a dangling quote there. Doesn't work at all as-is
            – Krease
            Jan 24 at 19:23












            @Krease Author updated his answer
            – Vadim Kotov
            Jul 10 at 16:07




            @Krease Author updated his answer
            – Vadim Kotov
            Jul 10 at 16:07











            1














            Use command:



            [root@linux1 network-scripts]# route|grep default | awk 'print $8'
            enp0s3





            share|improve this answer






















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















            1














            Use command:



            [root@linux1 network-scripts]# route|grep default | awk 'print $8'
            enp0s3





            share|improve this answer






















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













            1












            1








            1






            Use command:



            [root@linux1 network-scripts]# route|grep default | awk 'print $8'
            enp0s3





            share|improve this answer














            Use command:



            [root@linux1 network-scripts]# route|grep default | awk 'print $8'
            enp0s3






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Aug 17 '16 at 17:13









            Tomasz

            9,19352965




            9,19352965










            answered Aug 17 '16 at 16:46









            Larry Catt

            212




            212











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
















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















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




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

















            draft saved

            draft discarded
















































            Thanks for contributing an answer to Unix & Linux Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f14961%2fhow-to-find-out-which-interface-am-i-using-for-connecting-to-the-internet%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown






            Popular posts from this blog

            How to check contact read email or not when send email to Individual?

            Displaying single band from multi-band raster using QGIS

            How many registers does an x86_64 CPU actually have?