1

I need to get all intefaces names in Arch linux. If I issue command ifconfig, I get following response:

[root@pi ~]# ifconfig

eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.0.154  netmask 255.255.255.0  broadcast 192.168.0.255
        ether b8:27:eb:3c:03:fe  txqueuelen 1000  (Ethernet)
        RX packets 119099  bytes 96958556 (92.4 MiB)
        RX errors 0  dropped 8  overruns 0  frame 0
        TX packets 18304  bytes 5456443 (5.2 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        loop  txqueuelen 0  (Local Loopback)
        RX packets 285  bytes 88221 (86.1 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 285  bytes 88221 (86.1 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

That is everything ok, but I just need interface names. How do I get just interface names?

5

One way could be to use ifconfig with -s (short list), and cut out the part you need:

$ ifconfig -a -s
Iface   MTU Met   RX-OK RX-ERR RX-DRP RX-OVR    TX-OK TX-ERR TX-DRP TX-OVR Flg
eth0       1500 0  1374267176      0 116420 0      2848281091      0      0      0 BMRU
lo        65536 0  761767047      0      0 0      761767047      0      0      0 LRU
vboxnet0   1500 0         0      0      0 0             0      0      0      0 BM
virbr0     1500 0         0      0      0 0             0      0      0      0 BMU
$ ifconfig -s -a | awk '$1 !~ /Iface/ {print $1}'
eth0
lo
vboxnet0
virbr0

Or a similar method with ip:

$ ip -o link show | awk -F': ' '{print $2}'
lo
eth0
virbr0
vboxnet0
  • Ok, that is solved now, but I have another question: How do I detect only ACTIVE interfaces? – KernelPanic Dec 11 '14 at 1:41
  • 1
    @MarkoFrelih Remove the -a in the ifconfig commands, and for the ip command, add up: ip -o link show up – muru Dec 11 '14 at 7:06
  • thank you, but at the moment I am little confused: Approach you propsed can be used in virtually any linux distribuation. How is this correlated with archlinux netctl network profiles (i.e., does archlinux netctl profiles realtime "image" of present, connected and disconnected internet hardware)? – KernelPanic Dec 11 '14 at 7:22
  • 1
    @MarkoFrelih And that is something you should put in the question. How am I supposed to read your mind about netctl? And as far as Arch is concerned, I use Network Manager on Arch. – muru Dec 11 '14 at 8:55
4

You can parse the output of ifconfig. This works on many unix variants.

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

If you don't mind being strictly Linux-only, you can use the simpler command

ls /sys/class/net/

If you don't want to list interfaces that are down, use ifconfig instead of ifconfig -a. With /sys, it's more obscure: you need to check whether the IFF_UP flag is set for this interface. Since IFF_UP is the lowest-order bit, you need to test whether flags is an odd number.

grep -l '[13579bdf]$' /sys/class/net/*/flags | sed 's!.*/!!'

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.