How to find out the number of network interfaces available in a linux system?
12,446
You can get a list of these interfaces on most systems from the following:
ls -A /sys/class/net
But beware of parsing the output from ls
in your script.
Edit
To get a total number of network interfaces pipe the output of this command into wc
as recommended in Nikolay's comment as in:
ls -A /sys/class/net | wc -l
Related videos on Youtube
Author by
user1762571
Updated on September 18, 2022Comments
-
user1762571 over 1 year
I need to loop over the network interfaces available in Linux. I'm interested in all kinds of interfaces (loopback, ethernet, vlan, bridge) - whatever shows up in
ifconfig -a
.Is there a way to enumerate the interfaces in Linux? By any command or by reading a file?
-
Admin almost 9 yearsWell this works...
ifconfig -a | grep 'flags' | wc -l
-
Admin almost 9 years@orion, I would recommend using
ip
instead as it is a successor ofifconfig
-ip -o link show | wc -l
. See this question -
Admin almost 9 yearsYou might look at
netdevice(7)
). -
Admin almost 9 years@Nikolay of course, that's what I would use, and others already posted that answer (and removed it). I just wanted to point out that from what OP did to the answer to his question isn't very far.
-
Admin about 7 yearsJust in case it helps anyone, adding the option "1" to the ls command will list output one item per line, e.g:
ls -A1 /sys/class/net eth0 lo usb0 wlan0
In fact, I was surprised to find the count using "wc -l" still works on the single line version of the command (ls -A). But there are likely other situations where the item-per-line output from ls will be preferable, or necessary.
-
-
Stéphane Chazelas almost 9 yearsLinux doesn't allow creating network interfaces with newline in their name, but it allows network interfaces whose name starts with
.
, so you'd needls -A
here. -
111--- almost 9 yearsNoted sir , ty :)
-
user1762571 almost 9 yearsThis shows all the interfaces. But i am interested in the count.(total no of interfaces)