In Ubuntu 16.04 I made a script to dinamically configure IP address of the wired ethernet interface based on certain parameters. Now the problem is that in my script I assume that the name is eth0, but this is not always true.. assuming that I have only one wired network interface (not necessary plugged) but I can have also a wireless interface, how can I retrieve its name (just the wired one)?

with ifcommand I get:

enp7s0    Link encap:Ethernet  HWaddr fc:3f:db:a2:6d:46  
          inet addr:192.168.0.101  Bcast:192.168.0.255  Mask:255.255.255.0
          inet6 addr: fe80::149f:3103:af2f:1ec5/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:6784 errors:0 dropped:0 overruns:0 frame:0
          TX packets:7493 errors:1 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:4565079 (4.5 MB)  TX bytes:827825 (827.8 KB)

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:8037 errors:0 dropped:0 overruns:0 frame:0
          TX packets:8037 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1 
          RX bytes:2261250 (2.2 MB)  TX bytes:2261250 (2.2 MB)

wlp19s0   Link encap:Ethernet  HWaddr c8:ff:28:93:26:32  
          UP BROADCAST MULTICAST  MTU:1500  Metric:1
          RX packets:180463 errors:0 dropped:0 overruns:0 frame:0
          TX packets:121275 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:229823414 (229.8 MB)  TX bytes:15675806 (15.6 MB)

What I want is:

enp7s0
  • I think you want something like ip link | awk -F: '$0 !~ "lo|vir|^[^0-9]"{print $2a;getline}' – Valentin Bajrami Apr 6 '17 at 14:12
  • this will show also the wireless adapter – rok Apr 7 '17 at 8:35
  • #rok, it does but aren't you mentioning it in your question as well? but I can have also a wireless interface, how can I retrieve its name – Valentin Bajrami Apr 7 '17 at 10:40
  • edited with details, please see – rok Apr 7 '17 at 10:55
  • 1
    Ok then this will fix it: ip link | awk -F: '$0 !~ "lo|vir|wl|^[^0-9]"{print $2a;getline}' – Valentin Bajrami Apr 7 '17 at 12:37
up vote 2 down vote accepted

If you want to exclude interfaces like vir , loopback and wl(wireless) then the following should do the trick.

ip link | awk -F: '$0 !~ "lo|vir|wl|^[^0-9]"{print $2;getline}'

Here we use colon as delimiter -F: then check if the row $0 does not match a certain string using regular expression.

Because all interfaces would displayed under /sys/class/, such as /sys/class/net/ listing all network interfaces,

it is possible to directly use this directly search and return the exact interface name through a simple keyword like:

ls /sys/class/net | grep enp

I'd use ifconfig(bit old and deprecated) tool or ip command. Using ip route you are having a list of available routes. Then get only line containing default with grep and strip the unwanted characters off the string using sed

full command goes like:

ip route | grep default | sed -e "s/^.*dev.//" -e "s/.proto.*//"

  • It's only working if the ethernet port is connected, otherwise could also show the wireless adapter name if connected to a wifi network... – rok Apr 6 '17 at 11:00
  • @rok don't you need the default interface always regardless of Ethernet or wireless? – inckka Apr 6 '17 at 11:05
  • No.. I just need the Ethernet name – rok Apr 6 '17 at 11:08
  • This would have been my answer. @rok your question sounds like you want the interface whether it is wired or wireless. Please edit your question to clarify. – chicks Apr 6 '17 at 13:03

Assuming you got only one physical interface and not some wlan interface etc. The trick is to get the interfaces. Which are in /sys/class/net and then look where they go to.

find /sys/class/net ! -type d | xargs --max-args=1 realpath  | awk -F\/ '/pci/{print $NF}'

If you got more interfaces you would have to grep lspci first:

lspci  | awk '/Ethernet/{print $1}'

The whole thing would turn into:

pci=`lspci  | awk '/Ethernet/{print $1}'`; find /sys/class/net ! -type d | xargs --max-args=1 realpath  | awk -v pciid=$pci -F\/ '{if($0 ~ pciid){print $NF}}'
  • no.. I also have a wireless interface – rok Apr 6 '17 at 10:55

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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