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).
Asked
Active
Viewed 8,378 times
5
G-Man Says 'Reinstate Monica'
- 22,130
- 27
- 68
- 117
aurelien
- 2,057
- 1
- 11
- 24
-
The name change depending of the computer you are running. – aurelien Mar 15 '16 at 18:12
-
1What 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
-
1The 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
-
more than one is possible?? well that is not important ... I need just one ... only one name in the output ... of a working one. – aurelien Mar 15 '16 at 18:14
-
`ip route | awk '/^default/{print $5}'` is that what you want ? :) – MAQ Mar 15 '16 at 18:16
-
yes, more than one is possible and common. My home gateway/wifi AP box has 7 network interfaces active - two of which don't have IP addresses (because they have bridges configured on them): br0, br1, eth0, eth1, lo, ppp0, and wlan0. That `ip addr show | awk ...` command you mention lists `wlan0` on my system which is not what I would consider to be very useful (i only have wifi for household phones and tablets and my laptop). Even getting rid of the `exit` from the awk script lists only wlan0, br0, & br1, ignoring ppp0 (pppoe connection to my ISP) because it doesn't have a brd address. – cas Mar 15 '16 at 21:34
-
hum, on all my machines on only have one working at a time :-/ – aurelien Mar 16 '16 at 04:02
-
@terdon is this question seriously duplicated or not? I have find the other one and tried it, it does not works in my code, I have ask this one, and your response and the one of (@) KWubbufetowicz (one guy by post) fit perfectly in my code. And better you tell me that it is not deprecated style so ... I am new and do not understand that people, what is wrong? – aurelien Mar 16 '16 at 04:18
2 Answers
11
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 127.0.0.1/8 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 192.168.1.4/24 brd 192.168.1.255 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
link/none
inet 123.167.217.2/24 brd 123.167.217.255 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}'
wlp3s0b1
tun0
If you want only one, you can get the first one (only) with:
$ ip addr show | awk '/inet.*brd/{print $NF; exit}'
wlp3s0b1
The exit statement tells awk to stop searching
after it finds the first match.
G-Man Says 'Reinstate Monica'
- 22,130
- 27
- 68
- 117
terdon
- 234,489
- 66
- 447
- 667
-
2@aurelien If this answer solved your issue, please take a moment and [accept it](http://unix.stackexchange.com/help/someone-answers) 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
-
-
@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
-
@cas ah. Damn. I haven't used one of those in a long time. Care to post an answer? I'm sure you sysadmin types have a good way of doing this. – terdon Mar 15 '16 at 22:57
-
i don't think there is an answer that isn't specific to the machine and especially to the user's requirements - what's the most important single active interface on any given machine? on my machine, it could be either `br0` or `ppp0` depending on whether i want to know about LAN or internet link (which is also dependent on `eth1` because that's what my bridged-mode ADSL2 modem is connected to...and it will be `eth1` when/if i get FTTP) and ALL of these interfaces (and more) are active at the same time on my machine. On yours it's `wlp3s0b1` or maybe `en0` if your ethernet is plugged in – cas Mar 15 '16 at 23:06
-
BTW, us sysadmin types are good at thinking of ways that apparently simple and obvious solutions might fail or just be inadequate as a generic solution :) – cas Mar 15 '16 at 23:06
-
@terdon do you mind explaining what's going on in the awk statement? You specifically mentioned what exit does but how does the `/init.*brd/{print $NF}` part even work? Thanks! – harperville Oct 23 '18 at 12:28
-
@harperville that just means "if this line contains the string `inet` and then `brd`, print the last field". In awk, the variable `NF` is the number of fields, so `$NF` is the last field. – terdon Oct 23 '18 at 12:35
2
ifconfig | sed 's/[ \t].*//;/^\(lo\|\)$/d'
Maslov Anton
- 161
- 1
- 1
- 4
-
1The problem with that approach is that `ifconfig` [is being replaced](http://serverfault.com/q/458628/155817) 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