Query network interface connected to internet

I want a command that returns the network interface connected to internet. In vms like sys-net that is connected to ethernet port or wifi, it should return sometihing like enpXsXX (or ensX). In other network enabled VMS the command should return eth0. I use below command but there is a possibility that the command might return vif interfaces.

result=ip -o link | grep ether | awk '{print $2}' | awk -F':' '{print $1}' | head -n 1

Any ideas on what command will return interface name connected to internet?

Does your “network interface connected to internet” translate to “interface used for default route”?

If so, I think you could use something like

result=$(ip route | grep default | awk '{print $5}')

to print the interface used for default route.

:slight_smile:

1 Like

Thanks. The command does not work for me. I want this command to work even when the device is not connected to internet. For e.g. the command returns empty in sys-net when it is not connected to internet.

I guess I just have to exclude interfaces named vif* from the result of my OG command.

result=ip -o link | grep ether | grep -v vif | awk '{print $2}' | awk -F':' '{print $1}' | head -n 1

What would you consider the correct response in sys-net, when there is no internet connection?

I’ve got both an ethernet port and a wireless interface … and without a connection, I don’t see how any command will be able to predict if the next internet connection will be the ethernet device or the wifi … :-/

Edit: When I try your original command in my sys-net it claims that ens7 should be my “network interface connected” even though it’s my wls6 that is connected:

$ ip -o link
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000\    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: ens7: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc fq_codel state DOWN mode DEFAULT group default qlen 1000\    link/ether 10:65:30:48:d0:43 brd ff:ff:ff:ff:ff:ff\    altname enp0s7
3: wls6: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP mode DORMANT group default qlen 1000\    link/ether 5e:70:f9:41:48:c5 brd ff:ff:ff:ff:ff:ff permaddr 5c:5f:67:c9:aa:b8\    altname wlp0s6
4: vif5.0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP mode DEFAULT group 2 qlen 1000\    link/ether fe:ff:ff:ff:ff:ff brd ff:ff:ff:ff:ff:ff
$

– so as a minimum you’ll have to take the state into account, in your command.

:slight_smile:

1 Like

hmm! good question. I would need the command to return both ethernet and wifi interface names in that case.

Then it sounds like you care more about “interfaces available” than “interface connected to internet” … and then you are right, that my suggestion isn’t the thing you are looking for.

:slight_smile:

On Qubes, the main interface is in group 1 and the vif interfaces are in group 2. It seems to be different on sys-net, the main interface is in the default group, which also includes the loopback interface.
With this in mind, I came up with the following command:

ip -o link show | awk -F': ' '/group '"$([ -e /run/qubes/this-is-netvm ] && echo "default" || echo "1")"'/ && $2 != "lo" {print $2}'
2 Likes

Thanks. Works expect for handing of leading whitespace in interface names.