List only the device names of all available network interfaces

22

9

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.

ftiaronsem

Posted 2010-10-25T20:41:03.617

Reputation: 503

Answers

17

Give this a try:

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

This will omit lo:

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

Paused until further notice.

Posted 2010-10-25T20:41:03.617

Reputation: 86 075

I realize that Linux and BSD often contain different *nix utilities, but it's unfortunate this solution doesn't work on a Mac :( – blong – 2016-09-24T12:54:05.983

ifconfig is deprected, use ip instead – pstanton – 2018-02-26T01:22:32.710

This seems to work for OS X: ifconfig -a | sed -E 's/[[:space:]:].*//;/^$/d' – Paused until further notice. – 2018-02-26T03:57:54.887

Thanks dennis, this worked like a charm^^. Perfekt. Regular Expressions are really something i should look into^^ – ftiaronsem – 2010-10-26T19:39:45.600

25

Another alternative would be:

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

Or maybe:

ls /sys/class/net

jweyrich

Posted 2010-10-25T20:41:03.617

Reputation: 1 106

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 – 2016-10-11T18:48:55.873

+1 for -o - didn't know that – Patryk – 2017-12-15T10:15:48.077

17

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

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

teknoraver

Posted 2010-10-25T20:41:03.617

Reputation: 271

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

Posted 2010-10-25T20:41:03.617

Reputation: 23 578

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 – 2010-10-26T19:39:12.713

add | grep -v lo :-) – Doug Harris – 2010-10-27T02:48:45.730

4

ls /sys/class/net/
eth0  eth1  eth2  lo

or if you need only eth*

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

wWolfovich

Posted 2010-10-25T20:41:03.617

Reputation: 41

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

someguy

Posted 2010-10-25T20:41:03.617

Reputation: 31

1

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

provides

lo:
eth0:
eth1:
eth2:

as output

Pramod

Posted 2010-10-25T20:41:03.617

Reputation: 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.

Mogens TrasherDK

Posted 2010-10-25T20:41:03.617

Reputation: 111

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

Posted 2010-10-25T20:41:03.617

Reputation: 1 375

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'

Syberam

Posted 2010-10-25T20:41:03.617

Reputation: 11

1

Improved:

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

Bob Smith

Posted 2010-10-25T20:41:03.617

Reputation: 11

1You should probably make that grep -v '^lo$';  your current command will exclude the (hypothetical example) logical and ridiculous interfaces. – Scott – 2019-01-22T09:25:27.410

1

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

OLD SOUL

Posted 2010-10-25T20:41:03.617

Reputation: 11

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

Posted 2010-10-25T20:41:03.617

Reputation: 12 560

That won't work. awk will ignore the leading white space and print the first word in each subsequent line as well. – Doug Harris – 2010-10-25T21:41:53.400

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 – 2010-10-26T19:36:45.633

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.

Gilles 'SO- stop being evil'

Posted 2010-10-25T20:41:03.617

Reputation: 58 319

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 – 2010-10-26T19:41:11.683

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

Posted 2010-10-25T20:41:03.617

Reputation: 363

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

Joseph Steven Martinez

Posted 2010-10-25T20:41:03.617

Reputation: 1

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'

indirection

Posted 2010-10-25T20:41:03.617

Reputation: 1

-1

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

ttbakiatwoam

Posted 2010-10-25T20:41:03.617

Reputation: 1

1Could you please expand end explain what this does? One line answers are generally not a good format for Superuser. – Doktoro Reichard – 2014-05-15T00:16:21.070