Ubuntu通过命令行连接WIFI

  1. 云栖社区>
  2. 博客>
  3. 正文

Ubuntu通过命令行连接WIFI

paulandcode 2019-06-09 08:38:17 浏览3183
展开阅读全文

1. 查看无线网卡的信息

iwconfig

输入类似如下结果

rmnetctl  no wireless extensions.

wlan0     IEEE 802.11abgn  ESSID:off/any
          Mode:Managed  Access Point: Not-Associated   Tx-Power=31 dBm
          Retry  long limit:7   RTS thr:off   Fragment thr:off
          Power Management:on

dummy0    no wireless extensions.

enx00044b6697cd  no wireless extensions.

ip6tnl0   no wireless extensions.

sit0      no wireless extensions.

lo        no wireless extensions.

tunl0     no wireless extensions.

注意: 需要在你的无线网卡存在, 且无线网卡驱动安装后, 才会检测到类似上面的wlan0的网卡项. 如果没有网卡项, 请先确认您的机子上存在无线网卡, 且驱动正确安装.

2. 启动无线网卡项

sudo ip link set wlan0 up

3. 扫描所检测到的无线网络

sudo iw dev wlan0 scan | less

输出如下

BSS f0:b4:29:61:92:ef(on wlan0)
        TSF: 9516218618 usec (0d, 02:38:36)
        freq: 2447
        beacon interval: 100 TUs
        capability: ESS Privacy ShortSlotTime APSD (0x0c11)
        signal: -58.00 dBm
        last seen: 10 ms ago
        Information elements from Probe Response frame:
        SSID: Xiaomi_517
        Supported rates: 1.0* 2.0* 5.5* 11.0* 9.0 18.0 36.0 54.0
        DS Parameter set: channel 8
        ERP: Barker_Preamble_Mode
        Extended supported rates: 6.0 12.0 24.0 48.0
        HT capabilities:
                Capabilities: 0x6c
                        HT20
                        SM Power Save disabled
                        RX HT20 SGI
                        RX HT40 SGI
                        No RX STBC
                        Max AMSDU length: 3839 bytes
                        No DSSS/CCK HT40
                Maximum RX AMPDU length 65535 bytes (exponent: 0x003)
                Minimum RX AMPDU time spacing: 4 usec (0x05)
                HT RX MCS rate indexes supported: 0-15
                HT TX MCS rate indexes are undefined
        HT operation:
                 * primary channel: 8
                 * secondary channel offset: no secondary
                 * STA channel width: 20 MHz
                 * RIFS: 0
                 * HT protection: no
                 * non-GF present: 0
                 * OBSS non-GF present: 0
                 * dual beacon: 0
                 * dual CTS protection: 0
                 * STBC beacon: 0
                 * L-SIG TXOP Prot: 0
                 * PCO active: 0
                 * PCO phase: 0
        WPA:     * Version: 1
                 * Group cipher: TKIP
                 * Pairwise ciphers: TKIP CCMP
                 * Authentication suites: PSK
        RSN:     * Version: 1
                 * Group cipher: TKIP
                 * Pairwise ciphers: TKIP CCMP
                 * Authentication suites: PSK
                 * Capabilities: 1-PTKSA-RC 1-GTKSA-RC (0x0000)
        WMM:     * Parameter version 1
                 * u-APSD
                 * BE: CW 15-1023, AIFSN 3
                 * BK: CW 15-1023, AIFSN 7
                 * VI: CW 7-15, AIFSN 2, TXOP 3008 usec
                 * VO: CW 3-7, AIFSN 2, TXOP 1504 usec
        BSS Load:
                 * station count: 0
                 * channel utilisation: 19/255
                 * available admission capacity: 31250 [*32us]
        Country: CN     Environment: Indoor/Outdoor
                Channels [1 - 13] @ 16 dBm
        WPS:     * Version: 1.0
                 * Wi-Fi Protected Setup State: 2 (Configured)
                 * Response Type: 3 (AP)
                 * UUID: 28802880-2880-1880-a880-f0b4296192ef
                 * Manufacturer: xiaomi
                 * Model: R1CM
                 * Model Number: 0002
                 * Serial Number: 12345678
                 * Primary Device Type: 6-0050f204-1
                 * Device name: XiaoMiRouter
                 * Config methods: Label, Display, Keypad
                 * RF Bands: 0x1
                 * Unknown TLV (0x1049, 6 bytes): 00 37 2a 00 01 20
BSS b0:c5:54:81:0d:aa(on wlan0)
        TSF: 9516218240 usec (0d, 02:38:36)
        freq: 2412
        beacon interval: 100 TUs
        capability: ESS Privacy ShortPreamble ShortSlotTime (0x0431)
        signal: -65.00 dBm
        last seen: 10 ms ago
        Information elements from Probe Response frame:
        SSID: CSR
        Supported rates: 1.0* 2.0* 5.5* 11.0* 6.0 9.0 12.0 18.0
        DS Parameter set: channel 1
        ERP: <no flags>
        Extended supported rates: 24.0 36.0 48.0 54.0
        HT capabilities:
                Capabilities: 0x186e
                        HT20/HT40
                        SM Power Save disabled
                        RX HT20 SGI
                        RX HT40 SGI
                        No RX STBC
                        Max AMSDU length: 7935 bytes
                        DSSS/CCK HT40
                Maximum RX AMPDU length 32767 bytes (exponent: 0x002)
                Minimum RX AMPDU time spacing: 16 usec (0x07)
                HT RX MCS rate indexes supported: 0-15
                HT TX MCS rate indexes are undefined
        HT operation:
                 * primary channel: 1
                 * secondary channel offset: above
                 * STA channel width: any
                 * RIFS: 0
                 * HT protection: nonmember
                 * non-GF present: 0
                 * OBSS non-GF present: 1
                 * dual beacon: 0
                 * dual CTS protection: 0
                 * STBC beacon: 0
                 * L-SIG TXOP Prot: 0
                 * PCO active: 0
                 * PCO phase: 0
        WPA:     * Version: 1
                 * Group cipher: TKIP
                 * Pairwise ciphers: TKIP CCMP
                 * Authentication suites: PSK
        RSN:     * Version: 1
                 * Group cipher: TKIP
                 * Pairwise ciphers: TKIP CCMP
                 * Authentication suites: PSK
                 * Capabilities: 1-PTKSA-RC 1-GTKSA-RC (0x0000)
        WMM:     * Parameter version 1
                 * u-APSD
                 * BE: CW 15-1023, AIFSN 3
                 * BK: CW 15-1023, AIFSN 7
                 * VI: CW 7-15, AIFSN 2, TXOP 3008 usec
                 * VO: CW 3-7, AIFSN 2, TXOP 1504 usec
        WPS:     * Version: 1.0
                 * Wi-Fi Protected Setup State: 2 (Configured)
                 * Response Type: 3 (AP)
                 * UUID: 63041253-1019-2006-1228-b0c554810daa
                 * Manufacturer: D-Link Systems
                 * Model: DIR-619L
                 * Model Number: DIR-619L
                 * Serial Number: 20070413-0001
                 * Primary Device Type: 6-0050f204-1
                 * Device name: DIR-619L
                 * Config methods: Display, PBC
                 * Unknown TLV (0x1049, 6 bytes): 00 37 2a 00 01 20

上述的输出中描述了两个不同的wifi源, 我们所需要关注的项主要有两个, 一个是SSID, 这个项即为该wifi的名称, 如Xiaomi_517. 第二个项为WPA, WPA是一种网络加密协议, WPA的版本分为1和2, 从上面我们可以看到, WPA的版本号为1.

4. 连接网络

(1) 如果所连接的网络没有加密, 则可以轻松地直接连接

sudo iw dev wlan0 connect [网络 SSID]

(2) 如果网络是用较低级的协议, WEP加密的, 则也比较容易

sudo iw dev wlan0 connect [网络SSID] key 0:[WEP密钥]

(3) 如果网络使用的是WPA或者WPA2协议, 则稍微复杂

<1> 安装wpasupplicant
sudo apt install wpasupplicant
<2> 设置配置文件
sudo vim /etc/wpasupplicant/wpa_supplicant.conf

如果已有该文件, 则备份原有文件, 在新文件中加入如下内容

ctrl_interface=/var/run/wpa_supplicant

ap_scan=1

network={
        ssid="[your SSID name]"
        psk="[your WiFi password]"
        priority=1
}
<3> 以上述配置文件启动wpa_supplicant
sudo wpa_supplicant -i wlan0 -c /etc/wpa_supplicant/wpa_supplicant.conf &

在该命令中, 最后的&符号意思为将该命令挂到后台运行.
若执行上述命令后出现CONNECTED, 则说明启动成功, 若出现CHANGE或REJECT或其他的, 则说明启动失败.

5. 为本机获取IP地址

sudo dhclient wlan0

6. 检查是否成功连接WIFI

通过iwconfig命令, 查看wlan0是否已经连接上相应SSID的WIFI, 或者通过ping尝试联网.

网友评论

登录后评论
0/500
评论
paulandcode
+ 关注