5

I know that I can find which network interface is currently being used by parsing the output of:

# ifconfig

or

# route

But how can I get this information as a non-root user? Is there a way I can build such a

$ magic-command

whose ouput would be none lo or wlan0 or eth0 depending on the device used.. or even enp3s0f1 or wlp2s0 on exotic systems, with no admin rights?

1
  • 1
    What do you mean by “which network interface”? What if there's more than one? Do you want the one with the default route if there is one? ifconfig and route don't require any privileges (at least not on Linux, which Unix variant are you using?). Feb 23, 2017 at 22:42

1 Answer 1

12

Something like this?

ip addr | awk '/state UP/ {print $2}'
enp0s3:

This command was issued as a "regular" (non-root) user on:

uname -a
Linux centos 3.10.0-514.el7.x86_64 #1 SMP Tue Nov 22 16:42:41 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux

If it is important to remove the trailing : from the interface name, use (for example):

ip addr | awk '/state UP/ {print $2}' | sed 's/.$//'
enp0s3
2
  • Perfect, cheers :)
    – iago-lito
    Feb 23, 2017 at 10:42
  • Does this work in a virtual machine with bridged connection ? Tried this doesn't work.. it doesn't have the text state UP
    – MaXi32
    Jul 26, 2021 at 20:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .