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?
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.
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.
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.
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}'