iw (part 1 of 2)
iw is the modern Linux tool that inspects and configures Wi-Fi devices through the kernel's nl80211 interface: list cards and capabilities, scan, join open networks, create monitor/managed interfaces, set channel and width, and run DFS radar checks (CAC). It does not handle WPA/WPA2 passwords; that is wpa_supplicant or NetworkManager.
178.1 What it does, where it runs
- Linux only; package iw (apt/dnf/pacman), preinstalled on most distros.
- Talks nl80211 to cfg80211/mac80211 in the kernel.
- Lab honesty: the WSL2 VM has no Wi-Fi radio (CONFIG_CFG80211=m, not loaded, no /sys/class/ieee80211, only lo), so only commands that exit before opening nl80211 (help, --version) were run; device/link output comes from the official docs.
178.2 Global options and objects
iw --versioniw | head -6iw help | wc -liw help | tail -9iw version 6.9Usage: iw [options] commandOptions: --debug enable netlink debugging --version show version (6.9)Commands: dev <devname> ap stop482Commands that use the netdev ('dev') can also be given the'wdev' instead to identify the device.You can omit the 'phy' or 'dev' if the identification is unique,e.g. "iw wlan0 info" or "iw phy0 info". (Don't when scripting.)Do NOT screenscrape this tool, we don't consider its output stable.| Object | Meaning | Example |
|---|---|---|
| dev | Network interface | iw dev wlan0 info |
| phy | The physical Wi-Fi card (by name or #index) | iw phy phy0 info · iw phy#0 info |
| wdev | Wireless device index | iw wdev 0x1 info |
| reg | Regulatory domain | iw reg get |
- --debug prints raw netlink messages; iw help scan filters help to matching commands.
- Short form (iw wlan0 info) works when unique, but do not use it in scripts.
178.3 Device and capability listing
| Command | Shows |
|---|---|
| iw dev | All wireless interfaces |
| iw list / iw phy | All cards with full capabilities: bands, modes, channels |
| iw phy phy0 info | One card's capability |
| iw dev wlan0 info | Interface type, channel, MAC |
| iw phy phy0 channels | Every channel with DFS / no-IR flags |
| iw commands / iw features | Kernel nl80211 commands with numbers / build features |
178.4 Scanning
- Step 1
1.iw dev wlan0 scan: laptop visits every channel
- Step 2
2.Sends Probe Request, SSID wildcard, dst ff:ff:ff:ff:ff:ff
- Step 3
3.Office AP (ch 52) replies Probe Response: SSID OfficeWiFi, freq 5260
- Step 4
4.Cafe AP (ch 6) replies too: CafeWiFi, freq 2437
- Step 5
5.Laptop measures signal: OfficeWiFi -48 dBm, CafeWiFi -71 dBm -> scan results
iw help scan dev <devname> scan [-u] [freq <freq>*] [duration <dur>] ... [ssid <ssid>*|passive] Scan on the given frequencies and probe for the given SSIDs (or wildcard if not given) unless passive scanning is requested. dev <devname> scan trigger [freq <freq>*] [duration <dur>] ... dev <devname> scan dump [-u] Dump the current scan results. If -u is specified, print unknown data in scan results. dev <devname> scan abort Abort ongoing scan| Option | Meaning |
|---|---|
| freq 2412 2437 | Only these frequencies |
| duration 100 [duration-mandatory] | Dwell time (TU) |
| ssid MyWiFi | Probe for a hidden SSID |
| passive | No probe requests, just listen to beacons |
| randomise[=a/m] | Random source MAC so you are not tracked |
| flush / lowpri / ap-force | Clear cache / low priority / scan on an AP interface |
| trigger / dump / abort | Start without waiting / print cached results / stop |
| -u | Print unknown IEs |
- Scheduled scan: scan sched_start interval 5000 (or scan_plans 10:5 60, last interval repeats) scans in the background; matches ssid X reports only matches; sched_stop ends it.
178.5 Connection (station mode)
- Step 1
1.iw dev wlan0 connect OfficeWiFi
- Step 2
2.Authentication request (open system, seq 1) -> Auth OK
- Step 3
3.Association request (SSID, rates, HT/VHT) -> Association response
- Step 4
4.AP adds the laptop to its network
- Step 5
5.iw dev wlan0 link shows BSSID, SSID, freq, signal
Open/WEP only: WPA2 needs wpa_supplicant
iw dev wlan0 linkConnected to 68:7f:74:3b:b0:01 (on wlan0) SSID: tesla-5g-bcm freq: 5745 RX: 30206 bytes (201 packets) TX: 4084 bytes (23 packets) signal: -31 dBm tx bitrate: 300.0 MBit/s MCS 15 40Mhz short GI- Example from the Linux wireless docs: freq 5745 = channel 149 (5 GHz), -31 dBm is very strong; when not associated it prints Not connected.
- connect -w waits until done; connect MyWiFi 2437 aa:bb:cc:dd:ee:ff joins a specific AP; key 0:abcde is WEP only; mfp:req|opt|no controls PMF.
- cqm rssi -70 2 raises an event when signal drops below -70 dBm; disconnect leaves the network.
178.6 Interfaces: add, delete, type
iw phy phy0 interface add mon0 type monitoriw dev wlan0 interface add wlan1 type managediw dev mon0 del$ iw help interface dev <devname> interface add <name> type <type> [mesh_id <meshid>] ... Add a new virtual interface with the given configuration. Valid interface types are: managed, ibss, monitor, mesh, wds. The flags are only used for monitor interfaces, valid flags are: none: no special flags fcsfail: show frames with FCS errors control: show control frames otherbss: show frames from other BSSes cook: use cooked mode active: use active mode (ACK incoming unicast packets)- One phy can carry several virtual interfaces (e.g. managed client + monitor sniffer).
- set type changes the mode, but bring the interface down first; 4addr on and set peer are for WDS bridging; set meshid sets the mesh name.
- phy phy0 set name wifi0 renames the card; set netns moves it to another network namespace.
178.7 Channel and frequency
iw dev mon0 set channel 6iw phy phy0 set channel 36 HT40+iw dev mon0 set freq 5180 80 5210iw dev wlan0 switch freq 5180 HT40+ beacons 5| Form | Meaning |
|---|---|
| set channel N [width] | By channel number |
| set freq F [width] | By frequency in MHz |
| set freq F 80 C1 / 80+80 C1 C2 | Control freq + width + center freq(s) |
| Widths | NOHT, HT20, HT40+, HT40-, 5MHz, 10MHz, 80MHz, 160MHz, 320MHz |
| set freq_khz 902000 2MHz | kHz frequency for 802.11ah |
| switch ... beacons N [block-tx] | AP sends a CSA and moves after N beacons; block-tx stops TX until then |
| offchannel 2437 100 / roc start 2437 200 | Briefly visit another channel |
178.8 DFS and radar detection (CAC)
- Step 1
1.Office AP on channel 52, a DFS channel shared with weather/airport radar
- Step 2
2.Before transmitting: CAC (channel availability check), only listens for radar
- Step 3
3.Network runs; then a radar pulse is detected on ch 52
- Step 4
4.AP sends Channel Switch Announcement: new channel 36, count 5
- Step 5
5.Clients move to ch 36 (5180 MHz); ch 52 goes on the blocked list (NOL)
iw dev wlan0 cac channel 52 HT20iw dev wlan0 cac freq 5260 80 5290iw dev wlan0 cac trigger channel 52 HT20iw dev wlan0 cac background channel 100 HT20- cac channel/freq starts the radar check; cac trigger starts it and returns immediately.
- cac background runs on a separate radar chain, so the AP's current channel keeps working.
178.9 Common mistakes
- Expecting iw connect to join WPA2 networks: it only does open/WEP.
- Thinking monitor mode always kills wlan0: many cards allow a separate mon0 on the same phy (check valid interface combinations in iw list).
- Screen-scraping iw output in scripts: the authors say it is not stable.
- Changing type on an interface that is still up.
178.10 Interview tips
- iw vs iwconfig: iwconfig uses old, deprecated Wireless Extensions; iw uses nl80211/cfg80211 and supports 80 MHz, mesh, DFS, scheduled scan.
- Active scan sends probe requests; passive only listens to beacons.
- DFS channels need CAC before use and a CSA move when radar appears.
- WPA2 on Linux: wpa_supplicant or NetworkManager, not iw.