20

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.

17 Answers 17

17

Give this a try:

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

This will omit lo:

ifconfig -a | sed 's/[ \t].*//;/^\(lo\|\)$/d'
  • Thanks dennis, this worked like a charm^^. Perfekt. Regular Expressions are really something i should look into^^ – ftiaronsem Oct 26 '10 at 19:39
  • I realize that Linux and BSD often contain different *nix utilities, but it's unfortunate this solution doesn't work on a Mac :( – blong Sep 24 '16 at 12:54
  • ifconfig is deprected, use ip instead – pstanton Feb 26 '18 at 1:22
  • This seems to work for OS X: ifconfig -a | sed -E 's/[[:space:]:].*//;/^$/d' – Dennis Williamson Feb 26 '18 at 3:57
21

Another alternative would be:

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

Or maybe:

ls /sys/class/net
  • using ls /sys/class/net is a better solution in /etc/local.d scripts in openrc - the sed solutions above end up with an extra : at the end of each interface when run from /etc/init.d/local (but not when the script is run directly). – Stuart Cardall Oct 11 '16 at 18:48
  • +1 for -o - didn't know that – Patryk Dec 15 '17 at 10:15
16

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

$ basename -a /sys/class/net/*
eth0
eth1
lo
ppp0
tun0
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.
  • Thank you very much, especially for the very detailed answer explaining the used parameters. If I will have a similar issue in the future, I know at which post to look ;-). Unfortunatelly it was not cutting "lo", so the accepted answer goes to dennis. But this really is a very usefull answer, thank you :-) – ftiaronsem Oct 26 '10 at 19:39
  • add | grep -v lo :-) – Doug Harris Oct 27 '10 at 2:48
4
ls /sys/class/net/
eth0  eth1  eth2  lo

or if you need only eth*

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

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]'
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.

  • Your solution works equally well as dennis's. Unfortunatelly I cant accept two posts, so dennis was simply faster. But thanks nevertheless, especially for your grep explanation. – ftiaronsem Oct 26 '10 at 19:41
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}'
  • That won't work. awk will ignore the leading white space and print the first word in each subsequent line as well. – Doug Harris Oct 25 '10 at 21:41
  • Unfortunatelly, thats indeed not working, suffering the exact issue Doug pointed out. But thanks for you help, nevertheless I really appreciate you helping a newby like me. – ftiaronsem Oct 26 '10 at 19:36
1
/usr/sbin/ip addr show | awk '/^[1-9]/ {print $2}'

provides

lo:
eth0:
eth1:
eth2:

as output

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
1

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'

1

Improved:

netstat -i | grep '^[a-z]' | awk '{print $1}' | grep -v 'lo'
  • 1
    You should probably make that grep -v '^lo$';  your current command will exclude the (hypothetical example) logical and ridiculous interfaces. – Scott Jan 22 at 9:25
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
0

"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.

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 " "
-1

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

  • 1
    Could you please expand end explain what this does? One line answers are generally not a good format for Superuser. – Doktoro Reichard May 15 '14 at 0:16

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.