1

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
    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?). – Gilles Feb 23 '17 at 22:42
6

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
  • Perfect, cheers :) – iago-lito Feb 23 '17 at 10:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.