35

Similar to a previous question about finding network device names, I would like to get a (reliable) list of device names but just for Wi-Fi devices. So that it looks like the following depending on your naming structure:

wlan0
wlan1

or

wlp5s0
wlp5s1
33

On linux you have iw (show/manipulate wireless devices and their configuration) and when used with the dev command:

Commands:
    dev
        List all network interfaces for wireless hardware.

that is

iw dev

you'll get something like:

phy#0
    Interface wlan0
        ifindex 3
        wdev 0x1
        addr 00:12:32:e4:18:24
        type managed
phy#1
    Interface wlan1
        ifindex 4
        wdev 0x2
        addr 00:12:22:c6:b2:0a
        type managed

If you want to extract only interfaces names you can always process the output e.g.

iw dev | awk '$1=="Interface"{print $2}'

just keep in mind the help page clearly states:

Do NOT screenscrape this tool, we don't consider its output stable.
4
  • I like this answer, it solves the problem, but the bit about not scraping concerns me and makes me think there may be a better way. I'd like to leave this open for now. Jun 2 '16 at 16:51
  • The 'Do NOT screenscrape' comment only seems to appear when help is shown, due to invalid input or an explicit help command, but not in the output of valid results. Aug 19 '20 at 3:31
  • Just a reminder that you may need to run this as root i.e. sudo iw dev, because running it as a non-privileged user may return no results. Dec 3 '20 at 8:02
  • What to "screenscrape" then?
    – Angel
    Jun 14 at 9:59
14

On Ubuntu at least, there is the /proc/net/wireless file that contains details about the Wi-Fi interfaces. Which outputs for me:

$ cat /proc/net/wireless
Inter-| sta-|   Quality        |   Discarded packets               | Missed | WE
 face | tus | link level noise |  nwid  crypt   frag  retry   misc | beacon | 22
wlp5s0: 0000   36.  -74.  -256        0      0      0     16  33004        0

It's a little messy but the device name is in there.

To get just the interface name:

cat /proc/net/wireless | perl -ne '/(\w+):/ && print $1'

The perl code prints the string of word characters preceding the colon.

2
  • This did not work for me –probably because I have my wireless card disabled (but detectable).
    – imbuedHope
    Jan 17 '18 at 20:51
  • 1
    This only shows connected networks.
    – Janos
    Nov 1 '20 at 13:20
10

If you have sysfs mounted at /sys then the following commands work:

$ find /sys/class/net -follow -maxdepth 2 -name wireless | cut -d / -f 5
wlan0
$ find /sys/class/net -follow -maxdepth 2 -name phy80211 | cut -d / -f 5
wlan0

Or, without find:

for dev in `ls /sys/class/net`; do
    [ -d "/sys/class/net/$dev/wireless" ] && echo "$dev"
done

The first finds all devices in /sys/class/net with a wireless directory (which may be more than just WiFi devices) and the second finds devices that are 802.11 compatible

Tested on kernel 4.4

3
  • Even though sysfs probably has sane file names, parsing the output of ls is bad practice and a common pit fall in bash. Jan 27 at 23:51
  • Good point @StephanHenningsen — this is why I prefer the first solution which uses find
    – Josh
    Jan 27 at 23:58
  • I like the find solution too; it only finds files that actually exist which solves the awkward * that other solutions here get. I found that I needed a slightly different set of parameters to reduce the amount of noise cause by file system loops. The following works nicely for me, but does spawn an extra subshell for cut and relies on assumptions about indexes: find -H /sys/class/net/* -name wireless | cut -d / -f 5 Furthermore I needed an additional | head -n1 for my particular use-case. Jan 28 at 0:15
8

Universal way (non root) tested on Android 4, Android 7.1 and Android 9 and ArchLinux.

ls /sys/class/ieee80211/*/device/net/
1
  • 1
    Great solution.Your solution also works in SailfishOS Linux and some Ubuntu.
    – SebMa
    Nov 12 '20 at 15:52
8

Building on Josh's answer, I'll use a shell glob to identify the /sys/class/net directories with a wireless directory inside, and cut to grab the device name:

# find the directories
$ printf '%s\n' /sys/class/net/*/wireless # substitute with phy80211 if desired
/sys/class/net/wlp4s0/wireless
# filter out the "device" part
$ printf '%s\n' /sys/class/net/*/wireless | cut -d/ -f5
wlp4s0
5
  • Note that this will still print * if no wireless device exists, instead of an empty result. Instead, try this with only POSIX shell built-ins: printf '%s\n' /sys/class/net/*/wireless | while IFS='/' read -r -a p;do [ -e "/sys/class/net/${p[4]}" ] && echo "${p[4]}";done.
    – Yeti
    Sep 30 '20 at 5:18
  • 1
    @Yeti posix-sh doesn’t have arrays though? As for printing the glob versus an empty result, I think (at least for some shells) that depends on nullglob. Sep 30 '20 at 12:01
  • Why not just filter the unwanted * out: printf '%s\n' /sys/class/net/*/wirelessREMOVETHIS | cut -d / -f 5 | grep -v '*' Jan 27 at 22:08
  • @StephanHenningsen good use of cut instead of awk, edited. I'm not convinced on the * part though; I really think it depends on what you're doing, what the shell options are, and whether or not your device might have an asterisk in it (shudders) Jan 27 at 22:10
  • It's just a little star; what could go wrong? Jan 27 at 22:14
2

Here's a simple and very effective loop that answers the question:

for dev in /sys/class/net/*; do
    [ -e "$dev"/wireless ] && echo ${dev##*/}
done

This one picks the first wireless interface and stores it in $iwdev, which was what I needed:

for dev in /sys/class/net/*; do
    if [ -e "$dev"/wireless ]; then
        iwdev=${dev##*/};
        break;
    fi
done

It's a rewrite of Josh's answer which I found nice and simple. The above approach handles spaces and other oddities in file names. It also doesn't make too many assumptions about file paths being sane and relies on a minimum of slashes and indexing. Finally it uses built-in parameter expansion for extracting the base device name instead of spawning a subshell for cut or awk.

1

Use find command tool

This finds the actual file that contains the name, it should work in most cases and it's really a short command:

# For WiFi
find /sys/class/net/ -name "wlp*" -exec basename \{} \; 
# For Ethernet
find /sys/class/net/ -name "enp*" -exec basename \{} \;
0

Here's the little bash function I wrote to cover more exceptions :

lswifiInterfaces () {
    set -o pipefail # I use this to preserve the return code of the first command in the pipe
    if \ls /sys/class/ieee80211/*/device/net 2> /dev/null; then
        :
    elif \ls -d /sys/class/net/*/wireless 2> /dev/null | awk -F/ '{print$5}'; then
        :
    fi
    set +o pipefail
}

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.