I am using the follwing to find the mtu value

ifconfig | grep mtu
eno17832: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 9000
lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536

but in my bash script we want to know exactly the MTU value

so we can use this

MTU_VAL=` ifconfig | grep mtu | head -1 | awk '{print $NF}' `

but this may be risky and no so elegant

please let me know if we can capture the MTU for sure ?

  • exactly the MTU value - each ethernet interface has its own mtu value – RomanPerekhrest Apr 24 '18 at 12:35

Most tools leverage information present in /proc or /sys, concerning ifconfig I'd go with /sys/class/net/<interface>/* files:

To get mtu for all interfaces but loopback (lo) this would work:

cat /sys/class/net/[^l][^o]*/mtu

The exclusion is done by shell globing, anything not starting with l then o is expanded and then the mtu file within it is read. If there's only one interface other than loopback it will return its mtu as a single value, usable in script with $() to put the output in a variable.

To answer the comment:

$ awk '{split(FILENAME,arr,"/"); print arr[5],$1}' /sys/class/net/*/mtu
ens160 1500
lo 65536

Same pattern match as above to exclude lo if needed (I've kept it here to showcase multiple interface output).

  • this is nice way , can we get the interface name and mtu value on each line? – yael Apr 24 '18 at 13:15
  • @yael this highly sounds like an XY problem, what are you willing to do with this information ? – Tensibai Apr 24 '18 at 13:25

Read from its config value directly.

MTU_VAL=$(< /sys/class/net/<replace-with-interface-name>/mtu)

Just replace <replace-with-interface-name> with the interface-name you want its MTU.

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.