Linux 環境下開啟wifi熱點 hostap dhcp bash
Hostapd
hostapd is a user space daemon for access point and authentication servers.
hostapd是AP和證明伺服器的守護進程,使用hostapd可以無網卡調整為maste模式,從類比一個路由的功能,也就是軟AP(soft AP)。
簡單來講,hostapd可以通過配置建立一個軟AP,下面具體介紹一下配置過程。 環境要求
-網卡支援master模式 -linux(debian, ubuntu, archlinx) -Linux mac80211 驅動 安裝 HOSTAPD
$: sudo apt-get install hostapd
或者下載安裝。 配置 HOSTAPD
在 * /etc/hostapd/hostapd.conf * 檔案內寫入如下內容
#sets the wifi interface to use, is wlan0 in most casesinterface=wlan0#driver to use, nl80211 works in most casesdriver=nl80211#sets the ssid of the virtual wifi access pointssid=ff#sets the mode of wifi, depends upon the devices you will be using. It can be a,b,g,n. Setting to g ensures backward compatiblity.hw_mode=g#sets the channel for your wifichannel=6#macaddr_acl sets options for mac address filtering. 0 means “accept unless in deny list”macaddr_acl=0#setting ignore_broadcast_ssid to 1 will disable the broadcasting of ssidignore_broadcast_ssid=0#Sets authentication algorithm#1 - only open system authentication#2 - both open system authentication and shared key authenticationauth_algs=1#####Sets WPA and WPA2 authentication######wpa option sets which wpa implementation to use#1 - wpa only#2 - wpa2 only#3 - bothwpa=3#sets wpa passphrase required by the clients to authenticate themselves on the networkwpa_passphrase=KeePGuessinG#sets wpa key managementwpa_key_mgmt=WPA-PSK#sets encryption used by WPAwpa_pairwise=TKIP#sets encryption used by WPA2rsn_pairwise=CCMP
執行命令
$:sudo hostapd /etc/hostapd/hostapd.conf
現在hostapd的配置已經完成,手機已經可以搜尋到一個名為ff的wifi。但是還連不上這個wifi,因為現在還沒有配置dhcp伺服器。 DHCP
現在HOSTAPD已經可以正確運行了,只需要配置好DHCP伺服器就可以用行動裝置接入wifi上網了。DHCP用來給連如的行動裝置分配ip地址,首先安裝DHCP伺服器。
$:sudo apt-get install isc-dhcp-server
編輯* /etc/dhcp/dhcpd.conf *檔案,內容如下:
ddns-update-style none;ignore client-updates;authoritative;option local-wpad code 252 = text; subnet 10.0.0.0 netmask 255.255.255.0 { # --- default gateway option routers 10.0.0.1; # --- Netmask option subnet-mask 255.255.255.0; # --- Broadcast Address option broadcast-address 10.0.0.255; # --- Domain name servers, tells the clients which DNS servers to use. option domain-name-servers 10.0.0.1, 8.8.8.8, 8.8.4.4; option time-offset 0; range 10.0.0.3 10.0.0.13; default-lease-time 1209600; max-lease-time 1814400; }
BASH指令碼
配置一個用來上網的簡單bash指令碼就可以了。假設命名為wifi.sh,寫入如下內容。
#!/bin/bash#Initial wifi interface configurationifconfig $1 up 10.0.0.1 netmask 255.255.255.0sleep 2###########Start DHCP, comment out / add relevant section###########Thanks to Panji#Doesn’t try to run dhcpd when already runningif [ “$(ps -e | grep dhcpd)” == “” ]; then dhcpd $1 &fi############Enable NATiptables --flushiptables --table nat --flushiptables --delete-chainiptables --table nat --delete-chainiptables --table nat --append POSTROUTING --out-interface $2 -j MASQUERADEiptables --append FORWARD --in-interface $1 -j ACCEPT#Thanks to lorenzo#Uncomment the line below if facing problems while sharing PPPoE, see lorenzo’s comment for more details#iptables -I FORWARD -p tcp --tcp-flags SYN,RST SYN -j TCPMSS --clamp-mss-to-pmtusysctl -w net.ipv4.ip_forward=1#start hostapdhostapd /etc/hostapd/hostapd.conf
使用格式為
./wifi.sh wifi_card_interface interface_with_internet
查看自己電腦的無線網卡和有線網卡的名字,比如我的是** wlan0 *, * eth0 **。
$:ifconfig -a
執行
chmod a+x wifi.shsudo ./wifi.sh wlan0 eth0
參考:·The Linux Way to create Virtual Wifi Access Point 最新 最早 最熱