Linux 无线网卡配置

关闭/开启网卡节能模式

临时关闭

sr@srs:~$ iw wlan0 set power_save off
sr@srs:~$ iw wlan0 set power_save on

永久关闭

sudo vim /etc/NetworkManager/conf.d/default-wifi-powersave-on.conf

找到下面的
[connection]
wifi.powersave = 33换成2并重新启动。

成功
然后运行sudo iwconfig,就能看到Power Management:off。

iwlist

## 获取指定网卡搜索的ESSID
sr@srs:~$ iwlist wlan0 scanning | grep ESSID
                    ESSID:"Seer-Robotics-5G"
                    ESSID:"Seer-Robotics-Guest"
##当前网络连接信息
sr@srs:~$ iw dev
phy#0
        Unnamed/non-netdev interface
                wdev 0x2
                addr 10:f0:05:2b:23:8f
                type P2P-device
                txpower 0.00 dBm
        Interface wlan0
                ifindex 4
                wdev 0x1
                addr 10:f0:05:2b:23:8e
                ssid Seer-Robotics-5G
                type managed
                channel 11 (2462 MHz), width: 20 MHz, center1: 2462 MHz
                txpower 22.00 dBm
##指定网卡信息
sr@srs:~$ iw dev wlan0 info
Interface wlan0
        ifindex 4
        wdev 0x1
        addr 10:f0:05:2b:23:8e
        ssid Seer-Robotics-5G
        type managed
        wiphy 0
        channel 11 (2462 MHz), width: 20 MHz, center1: 2462 MHz
        txpower 22.00 dBm
## 查看指定网卡支持的信道
sr@srs:~$ sudo iwlist wlan0 channel
wlan0     32 channels in total; available frequencies :
          Channel 01 : 2.412 GHz
          Channel 02 : 2.417 GHz

ifconfig

查看当前网卡

#查看已激活网卡
sr@srs:~$ ifconfig
#sr@srs:~$ifconfig -a 查看所有网卡(包括禁用的网卡)
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.192.5  netmask 255.255.255.0  broadcast 192.168.192.255
        inet6 fe80::4262:31ff:fe07:e310  prefixlen 64  scopeid 0x20<link>
        ether 40:62:31:07:e3:10  txqueuelen 1000  (Ethernet)
        RX packets 31603147  bytes 3192164691 (3.1 GB)
        RX errors 0  dropped 130739  overruns 0  frame 0
        TX packets 15369686  bytes 1111067693 (1.1 GB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

禁用指定网卡(重启后无效)

sr@srs:~$ sudo ifconfig wlan0 down  
#注:如果你用nmcli连接了网络,则禁用网卡不会断开,需要使用nmcli connection down xxxxxxxxxxxxxxxxxxxxxxxxxxx

激活指定网卡

sr@srs:~$ sudo ifconfig wlan0 on
#注:sudo server network-manager restart 重置网络后会把已禁用的网卡刷出来

开启/关闭网卡功耗

sr@srs:~$ iw wlan0 get power_save  #查看当前网卡功耗
Power save: on
sr@srs:~$ sudo ifconfig wlan0 on  #开启低功耗
sr@srs:~$ sudo ifconfig wlan0 off  #关闭低功耗

iwconfig

iwconfig仅支持wep, wpa_supplicant支持wep、wpa、wpa2等完整的加密认证

查看指定网卡详细信息

sr@srs:~$ iw wlan0 info
Interface wlan0
        ifindex 4
        wdev 0x1
        addr 10:f0:05:2b:23:8e
        ssid SEER-GROUP
        type managed
        wiphy 0
        channel 40 (5200 MHz), width: 40 MHz, center1: 5190 MHz
        txpower 22.00 dBm

sr@srs:~$ sudo iwconfig wlan0
wlan0     IEEE 802.11  ESSID:"Seer-Robotics-5G"
          Mode:Managed  Frequency:2.462 GHz  Access Point: B4:FB:E4:74:76:74
          Bit Rate=1 Mb/s   Tx-Power=22 dBm
          Retry short limit:7   RTS thr:off   Fragment thr:off
          Encryption key:off
          Power Management:on
          Link Quality=62/70  Signal level=-48 dBm
          Rx invalid nwid:0  Rx invalid crypt:0  Rx invalid frag:0
          Tx excessive retries:0  Invalid misc:2600   Missed beacon:0

linux查看WIFI信号强弱:

$ iwconfig wlan0 | grep -i --color quality //查询网络连接质量着红色
Link Quality=47/70 Signal level=-63 dBm

$ iwconfig wlan0 | grep -i --color signal //查询信号强度着红色
Link Quality=44/70 Signal level=-66 dBm

查看指定网卡连接信息

sr@srs:~$ iw dev wlan0 link
Connected to b4:fb:e4:75:76:74 (on wlan0)
        SSID: Seer-Robotics-5G
        freq: 5180
        RX: 118103932 bytes (907474 packets)
        TX: 351452 bytes (3539 packets)
        signal: -74 dBm
        tx bitrate: 57.8 MBit/s VHT-MCS 5 short GI VHT-NSS 1

        bss flags:      short-slot-time
        dtim period:    3
        beacon int:     100
        
##当前连接的信息
sr@srs:~$ iwconfig
lo        no wireless extensions.

wlan0     IEEE 802.11  ESSID:"SEER-GROUP"
          Mode:Managed  Frequency:5.2 GHz  Access Point: 68:D7:9A:3E:6C:E1
          Bit Rate=180 Mb/s   Tx-Power=22 dBm
          Retry short limit:7   RTS thr:off   Fragment thr:off
          Power Management:on
          Link Quality=51/70  Signal level=-59 dBm
          Rx invalid nwid:0  Rx invalid crypt:0  Rx invalid frag:0
          Tx excessive retries:0  Invalid misc:28783   Missed beacon:0

eth0      no wireless extensions.

eth1      no wireless extensions.

zt3jntlp2c  no wireless extensions.

用法: iwconfig interface [essid {NN|on|off}]
[nwid {NN|on|off}]
[mode {managed|ad-hoc|…}
[freq N.NNNN[k|M|G]]
[channel N]
[ap {N|off|auto}]
[sens N]
[nick N]
[rate {N|auto|fixed}]
[rts {N|auto|fixed|off}]
[frag {N|auto|fixed|off}]
[enc {NNNN-NNNN|off}]
[power {period N|timeout N}]
[retry {limit N|lifetime N}]
[txpower N {mW|dBm}]
[commit]

说明:iwconfig是LWE最主要的工具,可以对无线网卡的大部分参数进行配置。

参数:
essid:设置无线网卡的ESSID(Extension Service Set ID)。通过ESSID来区分不同的无线网络,正常情况下只有相同ESSID的无线站点才可以互相通讯,除非想监听无线网络。其后的参数为双引号括起的ESSID字符串,或者是any/on/off,如果ESSID字符串中包含any/no/off,则需要在前面加"–"。

示例:
#iwconfig eth0 essid any 允许任何ESSID,也就是混杂模式
#iwconfig eth0 essid “My Network” 设置ESSID为"My Network"
#iwconfig eth0 essid – “ANY” 设置ESSID为"ANY"

nwid: Network ID,只用于pre-802.11的无线网卡,802.11网卡利用ESSID和AP的MAC地址来替换nwid,现在基本上不用设置。

示例:
#iwconfig eth0 nwid AB34
#iwconfig eth0 nwid off

nick: Nickname,一些网卡需要设置该参数,但是802.11协议栈、MAC都没有用到该参数,一般也不用设置。

示例:
#iwconfig eth0 nickname “My Linux Node”

mode:设置无线网卡的工作模式,可以是
Ad-hoc:不带AP的点对点无线网络
Managed:通过多个AP组成的网络,无线设备可以在这个网络中漫游
Master:设置该无线网卡为一个AP
Repeater:设置为无线网络中继设备,可以转发网络包
Secondary:设置为备份的AP/Repeater
Monitor:监听模式
Auto:由无线网卡自动选择工作模式

示例:
#iwconfig eth0 mode Managed
#iwconfig eth0 mode Ad-Hoc

freq/channel:设置无线网卡的工作频率或者频道,小于1000的参数被认为是频道,大于10000的参数被认为是频率。频率单位为Hz,可以在数字后面附带k, M, G来改变数量级,比如2.4G。频道从1开始。使用lwlist工具可以查看无线网卡支持的频率和频道。参数off/auto指示无线网络自动挑选频率。

注意:如果是Managed模式,AP会指示无线网卡的工作频率,因此该设置的参数会被忽略。Ad-hoc模式下只使用该设定的频率初始无线网络,如果加入已经存在的Ad-hoc网络则会忽略该设置的频率参数。

示例:
#iwconfig eth0 freq 2422000000
#iwconfig eth0 freq 2.422G
#iwconfig eth0 channel 3
#iwconfig eth0 channel auto

ap:连接到指定的AP或者无线网络,后面的参数可以是AP的MAC地址,也可以是iwlist scan出来的标识符。如果是Ad-hoc,则连接到一个已经存在的Ad-hoc网络。使用off参数让无线网卡不改变当前已连接的AP下进入自动模式。any/auto参数,无线网卡自动选择最好的AP。

注意:如果无线信号低到一定程度,无线网络会进入自动选择AP模式。

示例:
#iwconfig eth0 ap 00:60:1D:01:23:45
#iwconfig eth0 ap any
#iwconfig eth0 ap off

rate/bit:如果无线网卡支持多速率,则可以通过该命令设置工作的速率。小于1000的参数由具体的无线网卡驱动定义,一般是传输速率的索引值,大于1000的为速率,单位bps,可以在数字后面附带k, M, G来指定数量级。auto参数让无线网卡自动选择速率。fixed参数让无线网卡不使用自动速率模式。

示例:
#iwconfig eth0 rate 11M
#iwconfig eth0 rate auto
#iwconfig eth0 rate 5.5M auto //自动选择5.5M以下的速率

txpower:如果无线网卡支持多发射功率设定,则使用该参数设定发射,单位为dBm,如果指定为W(毫瓦),只转换公式为:

dBm=30+log(W)。参数on/off可以打开和关闭发射单元,auto和fixed指定无线是否自动选择发射功率。

示例:
#iwconfig eth0 txpower 15
#iwconfig eth0 txpower 30mW
#iwconfig eth0 txpower auto
#iwconfig eth0 txpower off

sens:设置接收灵敏度的下限,在该下限之下,无线网卡认为该无线网络信号太差,不同的网卡会采取不同的措施,一些现代的无线网卡会自动选择新的AP。正的参数为raw data,直接传给无线网卡驱动处理,一般认为是百分比。负值表示dBm值。

示例:
#iwconfig eth0 sens -80 *需网卡支持
#iwconfig eth0 sens 2 *需网卡支持

retry:设置无线网卡的重传机制。limit ‘value’ 指定最大重传次数;lifetime ‘value’指定最长重试时间,单位为秒,可以附带m和u来指定单位为毫秒和微秒。如果无线网卡支持自动模式,则在limit和lifetime之前还可以附加min和max来指定上下限值。

示例:
#iwconfig eth0 retry 16
#iwconfig eth0 retry lifetime 300m
#iwconfig eth0 retry min limit 8

rts:指定RTS/CTS握手方式,使用RTS/CTS握手会增加额外开销,但如果无线网络中有隐藏无线节点或者有很多无线节点时可以提高性能。后面的参数指定一个使用该机制的最小包的大小,如果该值等于最大包大小,则相当于禁止使用该机制。可以使用auto/off/fixed参数。

示例:
#iwconfig eth0 rts 250
#iwconfig eth0 rts off

frag:设置发送数据包的分片大小。设置分片会增加额外开销,但在噪声环境下可以提高数据包的到达率。一般情况下该参数小于最大包大小,有些支持Burst模式的无线网卡可以设置大于最大包大小的值来允许Burst模式。还可以使用auto/fixed/off参数。

示例:
#iwconfig eth0 frag 512
#iwconfig eth0 frag off

key/enc[ryption]:设置无线网卡使用的加密密钥,此处为设置WEP模式的加密key,如果要使用WPA,需要wpa_supplicant工具包。密钥参数可以是 XXXX-XXXX-XXXX-XXXX 或者 XXXXXXXX 格式的十六进制数值,也可以是s:xxxxxx的ASCII字符。如果在密钥参数之前加了[index],则只是设置该索引值对应的密钥,并不改变当前的密钥。直接指定[index]值可以设置当前使用哪一个密钥。指定on/off可以控制是否使用加密模式。open/restricted指定加密模式,取决于不同的无线网卡,大多数无线网卡的open模式不使用加密且允许接收没有加密的数据包,restricted模式使用加密。可以使用多个key参数,但只有最后一个生效。WEP密钥可以是40bit,用10个十六进制数字或者5个ASCII字符表示,也可以是128bit,用26个十六进制数字或者13个ASCII字符表示。

示例:
#iwconfig eth0 key 0123-4567-89
#iwconfig eth0 key [3] 0123-4567-89
#iwconfig eth0 key s:password [2]
#iwconfig eth0 key [2]
#iwconfig eth0 key open
#iwconfig eth0 key off
#iwconfig eth0 key restricted [3] 0123456789
#iwconfig eth0 key 01-23 key 45-67 [4] key [4]

power:设置无线网卡的电源管理模式。period ‘value’ 指定唤醒的周期,timeout ‘value’指定进入休眠的等待时间,这两个参数之前可以加min和max修饰,这些值的单位为秒,可以附加m和u来指定毫秒和微秒。off/on参数指定是否允许电源管理,all/unicast/multicast指定允许唤醒的数据包类型。

示例:
#iwconfig eth0 power period 2
#iwconfig eth0 power 500m unicast
#iwconfig eth0 power timeout 300u all
#iwconfig eth0 power off
#iwconfig eth0 power min period 2 power max period 4

commit:提交所有的参数修改给无线网卡驱动。有些无线网卡驱动会先缓存无线网卡参数修,使用这个命令来让无线网卡的参数修改生效。不过一般不需要使用该命令,因为无线网卡驱动最终都会是参数的修改生效,一般在debug时会用到。为了方便配置,可以把配置写到 /etc/network/interfaces中,这样以后就不用反复配置了。

auto lo
iface lo inet loopback
auto eth1
iface eth1 inet static
address 192.168.1.3
netmask 255.255.255.0
gateway 192.168.1.1
echo nameserver 192.168.1.1>/etc/resolv.conf
pre-up /sbin/iwconfig eth1 essid “LW HOME LINK”
pre-up /sbin/iwconfig eth1 key s:liwei

auto usb0
iface usb0 inet static
address 192.168.0.200
netmask 255.255.255.0
auto dsl-provider
iface dsl-provider inet ppp
pre-up /sbin/ifconfig eth0 up # line maintained by pppoeconf
provider dsl-provider

wpa_supplicant

wpa_supplicant是wifi客户端(client)加密认证工具,和iwconfig不同,wpa_supplicant支持wep、wpa、wpa2等完整的加密认证,而iwconfig只能支持wep。
和wpa_supplocant相对应的,ap端的加密认证工具为hostapd。
wpa_supplicant运行于后台,它需要借助控制台工具wpa_cli来进行手动操作。
开发文档: http://w1.fi/wpa_supplicant/wpa_supplicant-devel.pdf

有线切换 5G AP

优先连接 5G Wi-Fi
在/etc/wpa_supplicant/wpa_supplicant.conf 文件中添加下面一行(如果文件不存在则创建)
network={
        ssid="test"
        psk="12345678"
        key_mgmt=WPA-PSK
        freq_list=5745 5765 5785 5805 5825 5320 5300 5280 5260 5240 5220 5200 5180 2412 2417 2422 2427 2432 2437 2442 2447 2452 2457 2462 2467 2472
        }

获取当前网卡的 AP 列表

## 获取 SSID 列表及MAC地址等
sr@srs:~$ sudo wpa_cli -i wlan0 scan
[sudo] password for sr:
OK
sr@srs:~$ sudo wpa_cli -i wlan0 scan_results
bssid / frequency / signal level / flags / ssid
ec:89:14:f0:5a:e8       5765    -71     [WPA2-PSK-TKIP][ESS]    RTL8186-default
ec:89:14:20:5a:e8       5805    -63     [WPA2-PSK-CCMP][WPS][ESS]       AGV_TEST_5G
ec:89:14:20:5a:e5       2412    -41     [WPA2-PSK-CCMP][ESS]    \x00\x00\x00\x00\x00\x00\x                     00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00                     \x00\x00\x00
ec:89:14:20:5a:e4       2412    -43     [WPA2-PSK-CCMP][WPS][ESS]       AGV_TEST

切换AP

#iwconfig 中的切换AP需要网卡支持
sr@srs:~$ sudo wpa_cli -i wlan0 roam B4:FB:E4:75:76:74
OK

执行:

/system/bin/wpa_supplicant -d -Dwext -iwlan0 -c/data/misc/wifi/wpa_supplicant.conf

-d :增加调试信息
-Dwext :wext,驱动名称
-iwlan0 :wlan0,网络接口名称
/system/bin/wpa_supplicant :wpa_supplicant可执行程序path
/data/misc/wifi/wpa_supplicant.conf :wpa_supplicant的配置文件path

运行命令行工具 wpa_cli

执行

wpa_cli -iwlan0 -p/data/system/wpa_supplicant

注,-p/data/system/wpa_supplicant中的wpa_supplicant并不是可执行程序,而是控制套接字。
此时会进入交互模式。其中交互模式的命令如下表:

Full commandShort commandDescription
statusstatdisplays the current connection status
disconnectdiscprevents wpa_supplicant from connecting to any access point
quitqexits wpa_cli
terminatetermkills wpa_supplicant
reconfigurereconreloads wpa_supplicant with the configuration file supplied (-c parameter)
scanscanscans for available access points (only scans it, doesn’t display anything)
scan_resultscan_rdisplays the results of the last scan
list_networkslist_ndisplays a list of configured networks and their status (active or not, enabled or disabled)
select_networkselect_nselect a network among those defined to initiate a connection (ie select_network 0)
enable_networkenable_nmakes a configured network available for selection (ie enable_network 0)
disable_networkdisable_nmakes a configured network unavailable for selection (ie disable_network 0)
remove_networkremove_nremoves a network and its configuration from the list (ie remove_network 0)
add_networkadd_nadds a new network to the list. Its id will be created automatically
set_networkset_nshows a very short list of available options to configure a network when supplied with no parameters.See next section for a list of extremely useful parameters to be used with set_network and get_network.
get_networkget_ndisplays the required parameter for the specified network. See next section for a list of parameters
save_configsave_csaves the configuration

启动/禁用 wpa_supplicant

注: 重启后失效

#启用
sudo systemctl enable wpa_supplicant
#禁用
sudo systemctl disable wpa_supplicant

设置网络格式

set_network <network id> <key> <parameter> [<parameter>]

显示网络信息的基本格式

get_network <network id> <key>

相应的参数如下表:

KeyDescriptionParameters
ssidAccess point namestring
id_strString identifying the networkstring
priorityConnection priority over other APsnumber (0 being the default low priority)
bssidMac address of the access pointmac address
scan_ssidEnable/disbale ssid scan0, 1, 2
key_mgmtType of key managementWPA-PSK, WPA_EAP, None
pairwisePairwise ciphers for WPACCMP, TKIP
group=TKIPGroup ciphers for WPACCMP, TKIP, WEP104, WEP40
pskPre-Shared Key (clear or encrypted)string
wep_key0WEP key (up to 4: wep_key[0123])string
eapExtensible Authentication ProtocolMD5, MSCHAPV2, OTP, GTC, TLS, PEAP, TTLS
identityEAP identity stringstring
passwordEAP passwordstring
ca_certPathname to CA certificate file/full/path/to/certificate
client_certPathname to client certificate/full/path/to/certificate (PEM/DER)
private_keyPathname to a client private key file/full/path/to/private_key (PEM/DER/PFX)

连接无加密的AP

>add_network  (It will display a network id for you, assume it returns 0)
>set_network 0 ssid "666"
>set_network 0 key_mgmt NONE
>enable_network 0
>quit

连接WEP加密AP

>add_network   (assume return 1)
>set_network 1 ssid "666"
>set_network 1 key_mgmt NONE
>set_network 1 wep_key0 "your ap password"
>enable_network 1

连接WPA-PSK/WPA2-PSK加密的AP

>add_network   (assume return 2)
>set_network 2 ssid "666"
>set_network 2 psk "your pre-shared key"
>enable_network 2

以上是通过命令行工具wpa_cli来实现wifi网络的连接。当然,也可以通过wpa_supplicant的配置文件来实现连接。
再回顾下运行wpa_supplicant时执行的命令:

/system/bin/wpa_supplicant -d -Dwext -iwlan0 -c/data/misc/wifi/wpa_supplicant.conf

我们在执行时加上了-c/data/misc/wifi/wpa_supplicant.conf,我们可以将我们要连接的AP的设置以一定的格式写入wpa_supplicant.conf配置文件中即可。

ctrl_interface=DIR=/data/system/wpa_supplicant GROUP=system update_config=1

network={
ssid="my access point"
proto=WPA
key_mgmt=WPA-PSK
psk="you pass words"
}

优先连接 5G Wi-Fi

在/etc/wpa_supplicant/wpa_supplicant.conf文件中添加下面一行

network={
        ssid="test"
        psk="12345678"
        key_mgmt=WPA-PSK
        freq_list=5745 5765 5785 5805 5825 5320 5300 5280 5260 5240 5220 5200 5180 2412 2417 2422 2427 2432 2437 2442 2447 2452 2457 2462 2467 2472
        }

Linux 网络配置(nmcli)

https://blog.csdn.net/u012020854/article/details/101052251

ubuntu18.04 安装

https://blog.csdn.net/u012020854/article/details/86551504

ubuntu18.04 卡死

https://blog.csdn.net/u012020854/article/details/106091636

ubuntu18.04 常用指令

https://blog.csdn.net/u012020854/article/details/101032734

ubuntu18.04 添加自定义分辨率

https://blog.csdn.net/u012020854/article/details/103787492

  • 1
    点赞
  • 43
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值