How to use the command 'iw dev' (with examples)
- Linux
- December 17, 2024
The iw dev
command is an invaluable tool in the world of Linux networking, particularly for managing and configuring wireless network interfaces. It’s part of the iw
suite of wireless management tools and allows users to show and manipulate information about wireless devices from the command line. This can include configuring wireless modes, channels, and frequencies, as well as managing virtual interfaces. The iw dev
command is essential for system administrators and network engineers who need to configure or troubleshoot wireless networks on Linux systems.
Use case 1: Set device to monitor mode
Code:
sudo iw dev wlp set type monitor
Motivation:
Switching a wireless device into monitor mode is primarily used for tasks such as monitoring network traffic, capturing packets for analysis, or performing security assessments and penetration testing. In monitor mode, the wireless device can capture all wireless frames in the vicinity, not just those addressed to it.
Explanation:
sudo
: This command requires superuser privileges since it involves altering the network interface state.iw
: The program being used for managing wireless connections.dev wlp
: Specifies the wireless device to be manipulated. Replacewlp
with your actual device identifier (e.g.,wlp2s0
).set type monitor
: Changes the device mode to monitor, allowing it to capture packets on all wireless networks.
Example Output:
There may not be a visible output in the terminal. However, you can verify the change by using iw dev wlp info
, which shows the device’s current mode as monitor.
Use case 2: Set device to managed mode
Code:
sudo iw dev wlp set type managed
Motivation:
The managed mode is a common operating mode for wireless network interfaces, allowing the device to connect to wireless access points and handle normal network traffic. Switching back to managed mode is necessary for regular internet usage after having been in monitor mode for tasks such as network diagnostics or security assessments.
Explanation:
sudo
: Required to execute the command with elevated privileges.iw
: Indicates the use of the iw tool for managing wireless devices.dev wlp
: Identifies the specific wireless device, which should be replaced with your network interface name.set type managed
: Sets the interface operating mode to managed, re-enabling normal connectivity functions.
Example Output:
No explicit output will be displayed, but iw dev wlp info
will confirm the device’s change to managed mode.
Use case 3: Set device WiFi channel
Code:
sudo iw dev wlp set channel channel_number
Motivation:
Changing the WiFi channel can be useful for avoiding interference with other networks or devices operating on the same channel. This is particularly beneficial in environments with multiple access points in close proximity.
Explanation:
sudo
: Elevates the command to superuser status to make changes to the network interface.iw dev
: Utilizing the iw command with dev specification to target a device.wlp
: The specific wireless device identifier.set channel channel_number
: Sets the wireless device to the specified channel. Replacechannel_number
with the desired channel (e.g., 6).
Example Output:
No direct output will be displayed. You must be in monitor mode for this command to take effect and confirm the change using iw dev wlp info
.
Use case 4: Set device WiFi frequency in MHz
Code:
sudo iw dev wlp set freq freq_in_mhz
Motivation:
Adjusting the WiFi frequency allows the device to operate on a specific part of the spectrum, which can aid in minimizing interference or testing network behavior at different frequencies. This is crucial for network optimization and improved wireless performance.
Explanation:
sudo
: Required to execute the command with necessary permissions.iw dev
: Engages the iw tool to modify the wireless device properties.wlp
: Specifies the target wireless interface, which should be adapted to your actual device.set freq freq_in_mhz
: Changes the operating frequency to the given value in MHz (e.g., 2412 for channel 1).
Example Output:
Example feedback is not immediately available. The change can be checked with the iw dev wlp info
after confirming that the device is in the correct mode and up.
Use case 5: Show all known station info
Code:
iw dev wlp station dump
Motivation:
Gathering information on all connected stations (devices) can help network administrators track device connectivity, diagnose issues, and optimize network performance. This is particularly useful on networks with numerous clients where detailed individual link statistics are needed.
Explanation:
iw
: The command-line tool for wireless management and monitoring.dev wlp
: Targets a specific wireless interface for querying (replacewlp
with your actual device name).station dump
: Outputs data such as MAC addresses, signal strength, tx bitrate, and other statistics for all known stations connected to the interface.
Example Output:
Station 12:34:56:aa:bb:cc (on wlp2s0)
inactive time: 20 ms
rx bytes: 11234560
...
Use case 6: Create a virtual interface in monitor mode with a specific MAC address
Code:
sudo iw dev wlp interface add "vif_name" type monitor addr 12:34:56:aa:bb:cc
Motivation:
Creating virtual interfaces allows users to set up multiple network interfaces on the same physical hardware. This can be invaluable in testing scenarios, where a separate virtual interface with a specific MAC address can be used to isolate traffic or simulate various client conditions.
Explanation:
sudo
: Required for elevated privileges to perform interface configuration.iw dev wlp
: Specifies the base device to extend with a virtual interface.interface add "vif_name"
: Adds a new virtual interface, wherevif_name
is the desired name.type monitor
: Designates the virtual interface to work in monitor mode.addr 12:34:56:aa:bb:cc
: Sets the MAC address for the virtual interface.
Example Output:
The system might not provide direct output, but you can confirm the addition by listing interfaces with iw dev
.
Use case 7: Delete virtual interface
Code:
sudo iw dev "vif_name" del
Motivation:
Deleting a virtual interface is essential for cleaning up configurations or terminating a specific instance of interface use. It may help reduce system complexity by removing interfaces that are no longer necessary.
Explanation:
sudo
: Necessary to authorize the removal of a system network interface.iw dev
: Command to identify and manage wireless interfaces."vif_name" del
: Targets the virtual interface namedvif_name
for deletion.
Example Output:
There won’t be any direct terminal feedback. Check using iw dev
to confirm that the virtual interface was removed successfully.
Conclusion:
The iw dev
command is a robust tool for the management and configuration of wireless networks on Linux systems. Its ability to manipulate wireless modes, channels, frequencies, and manage virtual interfaces makes it indispensable for network administrators, security professionals, and Linux enthusiasts interested in networking. Understanding the variety of configurations available through iw dev
helps users optimize performance, troubleshoot problems, and ensure effective network operation.