Ad-hoc networking

出自 Arch Linux 中文维基

Independent Basic Service Set(獨立基本服務集),縮寫是 IBSS,也被稱為自組網絡,是一種不使用中心控制節點,就能讓一組設備相互通訊的方法。是點對點網絡的一種,網絡中的設備直接通訊,不需要中轉。自組網絡可以用來進行 網絡共享

要求[編輯 | 編輯原始碼]

所有要連接到網絡的設備都具有兼容 [1] 的無線網卡。

Wifi 鏈路層[編輯 | 編輯原始碼]

因為 IBSS 網絡是點對點網絡,所有設備都應該使用相同的 wifi 連接設置。

提示:可以設置更複雜的網絡拓撲,Linux 無線文檔 包含更高級的示例。

手動設置[編輯 | 編輯原始碼]

警吿: 此方法創建的是 非加密 自組網絡,要創建加密網絡,請參考 #wpa_supplicant.

詳細信息請參考 網絡配置/Wireless#iw。請先安裝 軟件包 iw

設置操作模式為 ibss:

# iw interface set type ibss

啟動接口(可能需要額外的 rfkill unblock wifi):

# ip link set interface up

現在可以創建自組網絡了,將 your_ssid 替換為實際的網絡,frequency 替換為以 MHz 為單位的頻率。頻道和頻率的對應關係,請參考, 這裏

# iw interface ibss join your_ssid frequency

wpa_supplicant[編輯 | 編輯原始碼]

確保已經 安裝wpa_supplicant,並進行了配置(參考 wpa_supplicant).

/etc/wpa_supplicant-adhoc.conf
ctrl_interface=DIR=/run/wpa_supplicant GROUP=wheel

# use 'ap_scan=2' on all devices connected to the network
# this is unnecessary if you only want the network to be created when no other networks are available
ap_scan=2

network={
    ssid="MySSID"
    mode=1
    frequency=2432
    proto=RSN
    key_mgmt=WPA-PSK
    pairwise=CCMP
    group=CCMP
    psk="secret passphrase"
}

所有連接到網絡的設備運行下面 wpa_supplicant 命令:

# wpa_supplicant -B -i interface -c /etc/wpa_supplicant-adhoc.conf -D nl80211,wext

網絡配置[編輯 | 編輯原始碼]

最後一步是給網絡中的所有設備分配 IP 地址,有多種方法:

要向自組網絡共享外網連接,請參考 Internet sharing.

技巧[編輯 | 編輯原始碼]

使用 NetworkManager[編輯 | 編輯原始碼]

如果使用 NetworkManager,可以使用 nm-applet 進行自組網絡配置,不用使用手動方法。詳情參考 NetworkManager#Sharing internet connection over Wi-Fi

自定義 systemd 服務(使用 wpa_supplicant 和靜態 IP)[編輯 | 編輯原始碼]

可以使用下面模板啟用無線自組網絡:

/etc/conf.d/network-wireless-adhoc@interface
addr=192.168.0.2
mask=24
/etc/systemd/system/network-wireless-adhoc@.service
[Unit]
Description=Ad-hoc wireless network connectivity (%i)
Wants=network.target
Before=network.target
BindsTo=sys-subsystem-net-devices-%i.device
After=sys-subsystem-net-devices-%i.device

[Service]
Type=oneshot
RemainAfterExit=yes
EnvironmentFile=/etc/conf.d/network-wireless-adhoc@%i

# perhaps rfkill is not needed for you
ExecStart=/usr/bin/rfkill unblock wifi
ExecStart=/usr/bin/ip link set %i up
ExecStart=/usr/bin/wpa_supplicant -B -i %i -D nl80211,wext -c /etc/wpa_supplicant-adhoc.conf
ExecStart=/usr/bin/ip addr add ${addr}/${mask} dev %i

ExecStop=/usr/bin/ip addr flush dev %i
ExecStop=/usr/bin/ip link set %i down

[Install]
WantedBy=multi-user.target

See also[編輯 | 編輯原始碼]