配置AP模式
安装hostapd
sudo apt install hostapd
配置 hostapd
创建 /etc/hostapd/hostapd.conf
文件,内容如下
#5G
interface=wlan0
driver=nl80211
ssid=neardi_rk3568 //热点名称
hw_mode=a
channel=36
wmm_enabled=0
macaddr_acl=0
auth_algs=1
ignore_broadcast_ssid=0
wpa=2
wpa_passphrase=12345678 //热点密码
wpa_key_mgmt=WPA-PSK
wpa_pairwise=TKIP
rsn_pairwise=CCMP
#2.4G
interface=wlan0
driver=nl80211
ssid=neardi_rk3568 //热点名称
hw_mode=g
channel=6
wmm_enabled=0
macaddr_acl=0
auth_algs=1
ignore_broadcast_ssid=0
wpa=2
wpa_passphrase=12345678 //热点密码
wpa_key_mgmt=WPA-PSK
wpa_pairwise=TKIP
rsn_pairwise=CCMP
更改 /etc/default/hostapd
,内容如下:
# Defaults for hostapd initscript
#
# WARNING: The DAEMON_CONF setting has been deprecated and will be removed
# in future package releases.
#
# See /usr/share/doc/hostapd/README.Debian for information about alternative
# methods of managing hostapd.
#
# Uncomment and set DAEMON_CONF to the absolute path of a hostapd configuration
# file and hostapd will be started during system boot. An example configuration
# file can be found at /usr/share/doc/hostapd/examples/hostapd.conf.gz
#
#DAEMON_CONF=""
# Additional daemon options to be appended to hostapd command:-
# -d show more debug messages (-dd for even more)
# -K include key data in debug messages
# -t include timestamps in some debug messages
#
# Note that -B (daemon mode) and -P (pidfile) options are automatically
# configured by the init.d script and must not be added to DAEMON_OPTS.
#
#DAEMON_OPTS=""
DAEMON_CONF="/etc/hostapd/hostapd.conf"
最后执行如下命令:
sudo systemctl unmask hostapd
sudo systemctl restart hostapd
配置 DHCP Server
安装 DHCP Server
sudo apt install isc-dhcp-server
配置wlan0
首先设置wlan0的IPv4的地址,如下命令:
sudo ifconfig wlan0 192.168.200.1
之后更改/etc/default/isc-dhcp-server
,内容如下:
# Defaults for isc-dhcp-server (sourced by /etc/init.d/isc-dhcp-server)
# Path to dhcpd's config file (default: /etc/dhcp/dhcpd.conf).
#DHCPDv4_CONF=/etc/dhcp/dhcpd.conf
#DHCPDv6_CONF=/etc/dhcp/dhcpd6.conf
# Path to dhcpd's PID file (default: /var/run/dhcpd.pid).
#DHCPDv4_PID=/var/run/dhcpd.pid
#DHCPDv6_PID=/var/run/dhcpd6.pid
# Additional options to start dhcpd with.
# Don't use options -cf or -pf here; use DHCPD_CONF/ DHCPD_PID instead
#OPTIONS=""
# On what interfaces should the DHCP server (dhcpd) serve DHCP requests?
# Separate multiple interfaces with spaces, e.g. "eth0 eth1".
INTERFACESv4="wlan0"
INTERFACESv6=""
再更改/etc/dhcp/dhcpd.conf
,内容如下:
# dhcpd.conf
#
# Sample configuration file for ISC dhcpd
#
# option definitions common to all supported networks...
option domain-name "example.org";
option domain-name-servers 8.8.8.8;
default-lease-time 600;
max-lease-time 7200;
# The ddns-updates-style parameter controls whether or not the server will
...
...
#shared-network 224-29 {
# subnet 10.17.224.0 netmask 255.255.255.0 {
# option routers rtr-224.example.org;
# }
# subnet 10.0.29.0 netmask 255.255.255.0 {
# option routers rtr-29.example.org;
# }
# pool {
# allow members of "foo";
# range 10.17.224.10 10.17.224.250;
# }
# pool {
# deny members of "foo";
# range 10.0.29.10 10.0.29.230;
# }
#}
subnet 192.168.200.0 netmask 255.255.255.0 {
range 192.168.200.100 192.168.200.200;
option routers 192.168.200.1;
option domain-name-servers 8.8.8.8, 8.8.4.4;
}
最后启动DHCP SERVER, 如下命令:
sudo systemctl restart isc-dhcp-server
启动成功:
wlan0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.200.1 netmask 255.255.255.0 broadcast 192.168.200.255
ether 70:f7:54:87:08:76 txqueuelen 1000 (Ethernet)
RX packets 19895 bytes 4527795 (4.3 MiB)
RX errors 0 dropped 6 overruns 0 frame 0
TX packets 196 bytes 16517 (16.1 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
实现网络共享
将eth1的网络通过wlan0热点分享出去,参考以下步骤:
#开启网络转发功能
sudo echo "net.ipv4.ip_forward=1" >> /etc/sysctl.conf
#网络转发的功能生效
sudo sysctl -p
#将从eth1网卡输出的数据包进行源地址伪装,让其他设备通过eth1网卡访问外部网络,而不会被外部网络拒绝
sudo iptables -t nat -A POSTROUTING -o eth1 -j MASQUERADE
#允许从eth1网卡输入的数据包,若状态是RELATED或者ESTABLISHED,转发到wlan0网卡输出,让eth1网卡收到的响应数据包返回到wlan0
sudo iptables -A FORWARD -i eth1 -o wlan0 -m state --state RELATED,ESTABLISHED -j ACCEPT
#允许从wlan0网卡输入的数据包,无条件地转发到eth1网卡输出,让wlan0网卡的设备通过eth1网卡访问外部网络
sudo iptables -A FORWARD -i wlan0 -o eth1 -j ACCEPT