How can I retrieve from the command line (or a shell script) only the name of the active network interface, in Linux? If there are several active interfaces, I want just one (selected arbitrarily).

  • The name change depending of the computer you are running. – aurelien Mar 15 '16 at 18:12
  • 1
    What if there are more than one? Can you be sure your system will always have only one active? – terdon Mar 15 '16 at 18:12
  • @jasonwryan yes, but it's probably better not to close since ifconfig is getting deprecated in favor of ip and its ilk. – terdon Mar 15 '16 at 18:13
  • no it is not duplicate ... the one you link is for mac and does not fit for me. – aurelien Mar 15 '16 at 18:13
  • 1
    The answers of the suggested dupe also work for Linux. There's nothing OSX-specific there. However, there are modern alternatives. Please answer my first comment about whether we can be sure that your machine will always only have one active interface. – terdon Mar 15 '16 at 18:14

The modern way of doing this is using the ip command. For example, on my system with my wireless connection active, I get:

$ ip addr show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: eno1: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc fq_codel state DOWN group default qlen 1000
    link/ether 00:26:b9:dd:2c:28 brd ff:ff:ff:ff:ff:ff
3: wlp3s0b1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
    link/ether c4:46:19:5f:dc:f5 brd ff:ff:ff:ff:ff:ff
    inet brd scope global wlp3s0b1                            
       valid_lft forever preferred_lft forever
    inet6 fe80::c646:19ff:fe5f:dcf5/64 scope link
       valid_lft forever preferred_lft forever
16: tun0: <POINTOPOINT,MULTICAST,NOARP,UP,LOWER_UP> mtu 1500 qdisc noqueue state UNKNOWN group default qlen 100
    inet brd scope global tun0                            
       valid_lft forever preferred_lft forever

The active interface(s) have both an inet entry and a broadcast (brd) address. You can show all such interfaces with:

$ ip addr show | awk '/inet.*brd/{print $NF}'

If you want only one, you can get the first one (only) with:

$ ip addr show | awk '/inet.*brd/{print $NF; exit}'

The exit statement tells awk to stop searching after it finds the first match.

  • 2
    @aurelien If this answer solved your issue, please take a moment and accept it by clicking on the check mark to the left. That will mark the question as answered and is the way thanks are expressed on the Stack Exchange sites. – terdon Mar 15 '16 at 18:18
  • I will do ... it says to wait 7 minutes :-/ – aurelien Mar 15 '16 at 18:21
  • @aurelien no worries. I just mentioned it because you're new and might not know how the site works. Feel free to not accept and wait for another answer too, if you prefer. – terdon Mar 15 '16 at 18:22
  • Your command just do exactly what I have request for ... So I can just says thanks and accept it :-) – aurelien Mar 15 '16 at 18:24
  • not every "active" interface has a broadcast address - e.g. ppp interfaces don't. – cas Mar 15 '16 at 22:49
ifconfig | sed 's/[ \t].*//;/^\(lo\|\)$/d'
  • 1
    The problem with that approach is that ifconfig is being replaced by ip. – terdon Mar 15 '16 at 18:18
  • ip a | sed 's/[ \t].*//;/^\(lo\|\)$/d' respond an empty thing .. but +1 for this nice try :-) @Maslov-Anton – aurelien Oct 24 '18 at 12:39

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