How can I find active network interface from userland?

8,303

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
Share:
8,303

Related videos on Youtube

iago-lito
Author by

iago-lito

'considering leaving https://writing.meta.stackexchange.com/questions/2301/sadly-it-is-time-for-me-to-join-the-exodus

Updated on September 18, 2022

Comments

  • iago-lito
    iago-lito over 1 year

    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?

  • iago-lito
    iago-lito about 7 years
    Perfect, cheers :)
  • MaXi32
    MaXi32 almost 3 years
    Does this work in a virtual machine with bridged connection ? Tried this doesn't work.. it doesn't have the text state UP