31

I want to get a list of all available Network-Device Names on my Linux server. I figured that

ifconfig

would do the job, however ifconfig produces quite much output:

eth0      Link encap:Ethernet  Hardware Adresse 08:00:27:fc:5c:98  
          inet Adresse:192.168.2.222  Bcast:192.168.2.255  Maske:255.255.255.0
          inet6-Adresse: fe80::a00:27ff:fefc:5c98/64 Gültigkeitsbereich:Verbindung
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metrik:1
          RX packets:329 errors:0 dropped:0 overruns:0 frame:0
          TX packets:177 errors:0 dropped:0 overruns:0 carrier:0
          Kollisionen:0 Sendewarteschlangenlänge:1000 
          RX bytes:41496 (40.5 KiB)  TX bytes:32503 (31.7 KiB)

eth1      Link encap:Ethernet  Hardware Adresse 08:00:27:e9:35:7d  
          [...]

eth2      Link encap:Ethernet  Hardware Adresse 08:00:27:ff:db:fe  
          [...]

lo        Link encap:Lokale Schleife  
          [...]

What I want to achieve is a list like

eth0
eth1
eth2
lo

or even better just

eth0
eth1
eth2

I assume that this can be done by a combination of "cat", "sed" and "grep", but I have simply no clue of how to strip the uneccessary information.

18 Answers18

41

Another alternative would be:

ip -o link show | awk -F': ' '{print $2}'

Or maybe:

ls /sys/class/net
jweyrich
  • 1,386
26

Just use /sys/class/net and strip out the path:

$ basename -a /sys/class/net/*
eth0
eth1
lo
ppp0
tun0

A more modern way would be to use the iproute json output and a parser, like:

$ ip -j link |jq -r '.[].ifname'
lo
wlp0s20f3
enp0s31f6
virbr0
virbr0-nic

Which allows you to filter out the loopback interface:

$ ip -j link |jq -r '.[].ifname | select(. != "lo")'
wlp0s20f3
enp0s31f6
virbr0
virbr0-nic
17

Give this a try:

ifconfig -a | sed 's/[ \t].*//;/^$/d'

This will omit lo:

ifconfig -a | sed 's/[ \t].*//;/^\(lo\|\)$/d'
6

Try this:

ifconfig | cut -c 1-8 | sort | uniq -u
  • cut -c 1-8 extracts the first 8 characters of each line
  • sort sorts the lines
  • uniq -u prints only unique lines which will remove the blank lines for the description lines which have only spaces in their first eight characters

This works on two linux machines I tried, but on my MacBookPro (OS X 10.6.4), ifconfig uses tabs instead of spaces, so it's a bit more complicated:

ifconfig | expand | cut -c1-8 | sort | uniq -u | awk -F: '{print $1;}'
  • expand converts tabs to spaces
  • awk -F: '{print $1;}' prints the first field before a colon.
Doug Harris
  • 28,397
6
ls /sys/class/net/
eth0  eth1  eth2  lo

or if you need only eth*

ls /sys/class/net/eth*
eth0
eth1
eth2
4

Using /sys filesystem:

basename -a $(ls /sys/devices/**/net/* -d)

Using ip and Perl:

ip -o l|perl -lane'$F[1]=~s/://g;print $F[1]'
Kevin Panko
  • 7,466
someguy
  • 41
2

The easiest solution is in the man ifconfig(8)

man ifconfig(8) extract http://www.manpagez.com/man/8/ifconfig/:

The -l flag may be used to list all available interfaces on the system, with no other additional information. Use of this flag is mutually exclusive with all other flags and commands, except for -d (only list interfaces that are down) and -u (only list interfaces that are up).

So, to have the list, use :

ifconfig -l

The names will be separated by a space so you have to use sed to replace these spaces by a \n:

ifconfig -l | sed 's/ / /g'

Syberam
  • 21
2

"ifcongif -l" should do the job. its output is like this "lo0 gif0 stf0 en0 ..." with no newline.

I found it on some website but I can't find it anymore. and I'm still looking for the meaning of that "-l" Flag.

1
/usr/sbin/ip addr show | awk '/^[1-9]/ {print $2}'

provides

lo:
eth0:
eth1:
eth2:

as output

Pramod
  • 107
1

Even though an accepted solution exist, I would like to present my solution to this.

I have a bunch of virtual interfaces, and would like to get a list, usable in various bash scripts.

eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 11.22.33.137  netmask 255.255.255.192  broadcast 11.22.33.191
        inet6 fe80::a00:27ff:fe8c:6bd3  prefixlen 64  scopeid 0x20<link>
        ether 08:00:27:8c:6b:d3  txqueuelen 1000  (Ethernet)
        RX packets 2969136  bytes 2394432908 (2.2 GiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 1378821  bytes 358960701 (342.3 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

eth0:1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 11.22.33.189  netmask 255.255.255.192  broadcast 11.22.33.191
        ether 08:00:27:8c:6b:d3  txqueuelen 1000  (Ethernet)

eth0:2: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 11.22.33.190  netmask 255.255.255.192  broadcast 11.22.33.191
        ether 08:00:27:8c:6b:d3  txqueuelen 1000  (Ethernet)

eth0:3: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 11.22.33.180  netmask 255.255.255.192  broadcast 11.22.33.191
        ether 08:00:27:8c:6b:d3  txqueuelen 1000  (Ethernet)

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1  (Local Loopback)
        RX packets 673768  bytes 277972236 (265.0 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 673768  bytes 277972236 (265.0 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

I'm not interested in loopback, as I know it's there :)

This one-liner gets the job done:

ifconfig | egrep '^eth' | cut -f 1-2 -d ':' | cut -f 1 -d ' '| pcregrep -o1 "(((eth\d)(:\d))|(eth\d))"

Produces an output like:

eth0
eth0:1
eth0:2
eth0:3

Enjoy.

1

All of the above solution works fine. Still you can Try this

ifconfig | grep HW | cut -c 1-6

Since, lo which is loopback, is not assigned a HW Address, It won't show up.

Output -

root@glum:/home/amit# ifconfig | grep HW | cut -c 1-6
enp7s0
vmnet1
vmnet8
wlp6s0
C0deDaedalus
  • 2,730
1

Improved:

netstat -i | grep '^[a-z]' | awk '{print $1}' | grep -v 'lo'
1

to just print the first column:

netstat -a | awk '{print $1}'

you can incorporate other rules in awk to add or remove entries as needed.

EDIT: same goes with ifconfig (like Doug pointed out)

ifconfig | awk '{print $1}'

This is an example excluding the 'lo' interface

ifconfig | awk '{if ($1 != lo) print $1}'
MaQleod
  • 13,258
1

Here's one way to extract the interface names from the ifconfig output:

ifconfig -a | sed -n 's/^\([^ ]\+\).*/\1/p'

If you want to exclude certain names, one way is further filter with grep:

ifconfig -a | sed -n 's/^\([^ ]\+\).*/\1/p' | grep -Fvx -e lo

If you want to exclude more names, add more -e foo to the grep command.

0

None of the above solutions worked for me, here is my solution:

ifconfig -a  | egrep "^[a-z]+" | sed "s/: .*//" | grep -v "lo"
  1. List all available interfaces
  2. Extract only the lines that contains device names (no space at the beginning)
  3. Remove the unrelated trailing part which is after from first space
  4. Exclude lo interface

Outputs:

eth0
eth0:1
wlan0
ceremcem
  • 665
0

Sometime nettools is not installed by default. So, I like using built in commands that have more of a guarantee to be found within /bin,/usr/bin, and /usr/sbin without having to worry about post installed packages.

ip addr : show / manipulate routing, devices, policy routing and tunnels (address)
grep : find a space then anything after unitl :
cut : use (:) as a delimiter and get field 2
tr : delete any spaces

$ ip addr | grep -E ':\s.*?:' | cut -d ":" -f 2 | tr -d " "
0

Tested on macOS, the following worked. I needed to find all interfaces that were UP. This is based on the answer from Dennis Williamson above.

ifconfig -a | grep UP | sed 's/:.*//;/^$/d'

-1

ifconfig | grep flags | awk -F: '{print $1;}'