Linux无线网络管理命令(ip/iw/iwconfig/iwlist/ifconfig/wpa_supplicant)

  • Post author:
  • Post category:linux


前言

以下各小节的前半部分介绍无线网络相关的各种命令的背景以及适用范围。

后半部分演示常用命令搭配和相关操作。

iw命令

man手册中可以找到说明 iw – show / manipulate wireless devices and their configuration

这意味着iw既可以查看信息,又可以管理无线网络设备,还能更改配置,属于比较全能的命令。

缺点:实测对USB网卡支持性很差,几乎只适用于物理接入设备(PCI板载设备)。

常用命令列表:

iw list  # 查看本机支持的无线特性,such as band information (2.4 GHz, and 5 GHz), and 802.11n information
iw dev wlan0 scan # 扫描无线网络,列表的内容都是实时更新的
iw dev wlan0 link # 获取设备连接状态信息(实测不包含IP地址)
iw wlan0 info # 获取设备工作状态信息
iw event # 获取所有网络设备的工作日志信息

英文原文介绍:https://wireless.wiki.kernel.org/en/users/Documentation/iw

译文:https://blog.csdn.net/robertsong2004/article/details/40044947

ifconfig命令

ifconfig – configure a network interface 配置网络接口命令

通用的接口操作命令,但通用意味着只能做一些少量配置,例如打开关闭设备接口,配置IP、子网掩码,查看IP地址等。

ifconfig eth0 down # 关闭eth0接口
ifconfig wlan0 up  # 打开wlan0接口
ifconfig eth0 192.168.1.3 netmask 255.255.255.0 # 设置IP地址和子网掩码
route add default gw 192.168.1.1 # 顺便附带设置网关命令

man ifconfig 可以看到详情。

ip命令

ip – show / manipulate routing, devices, policy routing and tunnels

ip命令用于显示/操作路由、设备、策略路由和隧道,和iw一样是较为综合而强大的命令,与之相似的原型命令可以看作是ifconfig。

[root@localhost ~]# ip --help
Usage: ip [ OPTIONS ] OBJECT { COMMAND | help }
       ip [ -force ] -batch filename
where  OBJECT := { link | address | addrlabel | route | rule | neigh | ntable |
                   tunnel | tuntap | maddress | mroute | mrule | monitor | xfrm |
                   netns | l2tp | macsec | tcp_metrics | token }
       OPTIONS := { -V[ersion] | -s[tatistics] | -d[etails] | -r[esolve] |
                    -h[uman-readable] | -iec |
                    -f[amily] { inet | inet6 | ipx | dnet | bridge | link } |
                    -4 | -6 | -I | -D | -B | -0 |
                    -l[oops] { maximum-addr-flush-attempts } |
                    -o[neline] | -t[imestamp] | -ts[hort] | -b[atch] [filename] |
                    -rc[vbuf] [size] | -n[etns] name | -a[ll] }

最最常用搭配

ip link set wlan0 [up] [down]  # 设置网络接口的开启关闭

其他常用命令格式搭配可以参阅此文:https://blog.csdn.net/radkitty/article/details/3022181

iwlist命令

iwlist  – Get more detailed wireless information from a wireless interface

DXCyber409@DXCyber409:/etc/wpa_supplicant$ iwlist --help
Usage: iwlist [interface] scanning [essid NNN] [last]
              [interface] frequency 
              [interface] channel 
              [interface] bitrate 
              [interface] rate 
              [interface] encryption 
              [interface] keys 
              [interface] power 
              [interface] txpower 
              [interface] retry 
              [interface] ap 
              [interface] accesspoints 
              [interface] peers 
              [interface] event 
              [interface] auth 
              [interface] wpakeys 
              [interface] genie 
              [interface] modulation 

可以通过该命令来获取最后一次扫描的

缓存信息

:iwlist wlan0 scanning

对于USB网卡,要扫描周围的SSID广播,该命令几乎是唯一选项,因为iw支持不好。

iwconfig命令

和ifconfig是同级别的用户级管理工具,但专注于无线网络管理。

DXCyber409@DXCyber409:/etc/wpa_supplicant$ iwconfig --help
Usage: iwconfig [interface]
                interface essid {NNN|any|on|off}
                interface mode {managed|ad-hoc|master|...}
                interface freq N.NNN[k|M|G]
                interface channel N
                interface bit {N[k|M|G]|auto|fixed}
                interface rate {N[k|M|G]|auto|fixed}
                interface enc {NNNN-NNNN|off}
                interface key {NNNN-NNNN|off}
                interface power {period N|timeout N|saving N|off}
                interface nickname NNN
                interface nwid {NN|on|off}
                interface ap {N|off|auto}
                interface txpower {NmW|NdBm|off|auto}
                interface sens N
                interface retry {limit N|lifetime N}
                interface rts {N|auto|fixed|off}
                interface frag {N|auto|fixed|off}
                interface modulation {11g|11a|CCK|OFDMg|...}
                interface commit 
       Check man pages for more details.

可以使用此命令来连接开放(未加密)的WI-FI网络,和查看接入点的情况。

iwconfig wlan0 essid "WIFI名称" # 连接开放的WI-FI网络
iwconfig wlan0 # 查看wlan0连接情况,如果成功连接,将在Access Point显示下一路由的MAC地址,否则表示尚未连接。

wpa_supplicant命令

wpa_supplicant  – Wi-Fi Protected Access client and IEEE 802.1X supplicant

该命令可用于WPA/WPA2-PSK/WEP加密网络的连接管理,现代WI-FI环境必备。

DXCyber409@DXCyber409:/etc/wpa_supplicant$ wpa_supplicant --help
wpa_supplicant: invalid option -- '-'
wpa_supplicant v2.6
Copyright (c) 2003-2016, Jouni Malinen <j@w1.fi> and contributors

This software may be distributed under the terms of the BSD license.
See README for more details.

This product includes software developed by the OpenSSL Project
for use in the OpenSSL Toolkit (http://www.openssl.org/)

usage:
  wpa_supplicant [-BddhKLqqstuvW] [-P<pid file>] [-g<global ctrl>] \
        [-G<group>] \
        -i<ifname> -c<config file> [-C<ctrl>] [-D<driver>] [-p<driver_param>] \
        [-b<br_ifname>] [-e<entropy file>] [-f<debug file>] \
        [-o<override driver>] [-O<override ctrl>] \
        [-N -i<ifname> -c<conf> [-C<ctrl>] [-D<driver>] \
        [-m<P2P Device config file>] \
        [-p<driver_param>] [-b<br_ifname>] [-I<config file>] ...]

drivers:
  nl80211 = Linux nl80211/cfg80211
  wext = Linux wireless extensions (generic)
  wired = Wired Ethernet driver
  none = no driver (RADIUS server/WPS ER)
options:
  -b = optional bridge interface name
  -B = run daemon in the background
  -c = Configuration file
  -C = ctrl_interface parameter (only used if -c is not)
  -d = increase debugging verbosity (-dd even more)
  -D = driver name (can be multiple drivers: nl80211,wext)
  -e = entropy file
  -f = log output to debug file instead of stdout
  -g = global ctrl_interface
  -G = global ctrl_interface group
  -h = show this help text
  -i = interface name
  -I = additional configuration file
  -K = include keys (passwords, etc.) in debug output
  -L = show license (BSD)
  -m = Configuration file for the P2P Device interface
  -N = start describing new interface
  -o = override driver parameter for new interfaces
  -O = override ctrl_interface parameter for new interfaces
  -p = driver parameters
  -P = PID file
  -q = decrease debugging verbosity (-qq even less)
  -s = log output to syslog instead of stdout
  -t = include timestamp in debug messages
  -T = record to Linux tracing in addition to logging
       (records all messages regardless of debug verbosity)
  -u = enable DBus control interface
  -v = show version
  -W = wait for a control interface monitor before starting
example:
  wpa_supplicant -Dnl80211 -iwlan0 -c/etc/wpa_supplicant.conf

接下来演示如何通过wpa_supplicant连接加密的Wi-Fi网络,详细的解释以及配套的CLI交互程序使用,如有需要请详见参考文献。

博主所使用的Linux环境为Kali-Rolling,开机是会自己启动好wpa_supplicant的,因此已经提前kill掉相关进程用于演示。

首先按照手机等连接Wi-Fi的习惯,我们需要知道扫描无线网络广播并记录需要连接的SSID名称(iw命令和iwlist命令都可以完成,请查阅本文小关小节)。

根据手册我们需要一个配置文件,填入连接配置信息,sudo vim /etc/wpa_supplicant.conf

ctrl_interface=/var/run/wpa_supplicant

network={
	ssid="Wifi Network 1" 
	psk="12345678"
}
network={
	ssid="Wifi Network 2"
	psk="87654321"
}

随后启动wpa_supplicant

sudo wpa_supplicant -B -i wlan0 -c /etc/wpa_supplicant.conf

其中守护进程启动的-B参数如果不使用,可以看到连接过程用于监控错误的发生,例如密码输入错误。

启动后通过iwconfig命令可以看到wlan0接口已经有了Access Point地址,证明已经连接。

但ifconfig wlan0没有显示IP,此时需要获取IP地址才能上网。

dhclient wlan0 之后即可打开网页测试。

补充:USB无线网卡使用wpa_supplicant时出现错误信息

DXCyber409@DXCyber409:~$ sudo wpa_supplicant -i wlan1 -c /etc/wpa_supplicant.conf
Successfully initialized wpa_supplicant
nl80211: Could not configure driver mode
nl80211: deinit ifname=wlan1 disabled_11b_rates=0
wlan1: Failed to initialize driver interface

在wpa_supplicant启动参数中补充 -D wext 参数即可。

同时在wpa_supplicant的输出中可以看到

Successfully initialized wpa_supplicant
rfkill: Cannot get wiphy information

博主的USB网卡驱动并没有使用默认的n80211驱动模式,而是换成了扩展驱动wext模式。因此并没有被归纳入系统支持设备中,这也正是iw命令不能很好的支持USB网卡的原因。


参考文献

wpa_cli命令解析:https://blog.csdn.net/jy1075518049/article/details/51015141

linux下连接无线网出现nl80211: Could not configure driver mode nl80211:

http://www.cnblogs.com/dakewei/p/7750433.html



版权声明:本文为DXCyber409原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。