july
july
发布于 2024-04-28 / 13 阅读
1

Debian通过网线分享无线网络

一直用着中兴的小方糖路由器连接房东家的WIFI桥接使用,最近老是出现没有网络的情况,但是直接连接房东家的WIFI却没有问题,重启虽好,但是每天都要重启实在不是办法。最后想到了在小主机上面连接好WIFI ,然后再将WIFI网络通过网线分享给路由器。

连接WIFI

安装wpa用于连接wifi

sudo aptitude install wpasupplicant

查看系统网卡名

root@localhost:~# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host noprefixroute 
       valid_lft forever preferred_lft forever
2: enp1s0: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN group default qlen 1000
    link/ether xx:xx:xx:xx:xx:xx brd ff:ff:ff:ff:ff:ff
3: enp2s0: <BROADCAST,MULTICAST> mtu 1500 qdisc fq_codel state UP group default qlen 1000
   link/ether xx:xx:xx:xx:xx:xx brd ff:ff:ff:ff:ff:ff
4: wlo1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
    link/ether xx:xx:xx:xx:xx:xx brd ff:ff:ff:ff:ff:ff
    altname wlp3s0

# 无线网口名wlo1,有线网口名enp2s0

配置/etc/network/interfaces

# 根据需要修改下面内容
sudo vim /etc/network/interfaces 

allow-hotplug wlo1
# 配置wl01静态IP
iface wlo1 inet static
        address 192.168.0.119
        netmask 255.255.255.0
        gateway 192.168.0.1
        wpa-ssid "WIFI_NAME"
        wpa-psk "WIFI_PASSWORD"

auto enp2s0
allow-hotplug enp2s0
iface enp2s0 inet static
        address 192.168.3.1
        netmask 255.255.255.0

重启networking

sudo systemctl restart networking
#查看日志检查wifi是否连接成功
sudo journalctl -fu networking
#显示下面信息表示已经连接成功(CTRL-EVENT-CONNECTED)

wpa_supplicant[545318]: wlo1: SME: Trying to authenticate with xx:xx:xx:xx:xx:xx (SSID='CMCC-wang' freq=5805 MHz)
wpa_supplicant[545318]: wlo1: Trying to associate with xx:xx:xx:xx:xx:xx (SSID='CMCC-wang' freq=5805 MHz)
wpa_supplicant[545318]: wlo1: Associated with xx:xx:xx:xx:xx:xx
wpa_supplicant[545318]: wlo1: CTRL-EVENT-SUBNET-STATUS-UPDATE status=0
wpa_supplicant[545318]: wlo1: WPA: Key negotiation completed with xx:xx:xx:xx:xx:xx [PTK=CCMP GTK=CCMP]
wlo1: CTRL-EVENT-CONNECTED - Connection to xx:xx:xx:xx:xx:xx completed [id=0 id_str=]

安装isc-dhcp-server

enp2s0网口插上网线时自动进行分配自动获取 IP 地址、子网掩码、网关和 DNS 服务器等网络配置信息等,安装好后修改/etc/dhcp/dhcpd.conf

sudo vim /etc/dhcp/dhcpd.conf 

#增加下面配置
subnet 192.168.3.0 netmask 255.255.255.0 {
  range 192.168.3.151 192.168.3.200;
  option routers 192.168.3.1;
  option broadcast-address 192.168.3.255;
  option domain-name-servers 223.5.5.5;
  option domain-name "www.huangdf.xyz";
  default-lease-time 600;
  max-lease-time 7200;
  interface enp2s0;
}

# 重启
sudo systemctl restart isc-dhcp-server

配置转发

配置IP转发

sudo vim /etc/sysctl.conf 
#找到net.ipv4.ip_forward=1将#注释掉
net.ipv4.ip_forward=1

#执行生效
sudo sysctl -p

配置防火墙NAT转发

sudo iptables -t nat -A POSTROUTING -o wlo1 -j MASQUERADE
sudo iptables -A FORWARD -i wlo1 -o enp2s0 -m state —state RELATED,ESTABLISHED -j ACCEPT
sudo iptables -A FORWARD -i enp2s0 -o wlo1 -j ACCEPT

将网线连接小主机enp2s0网口和路由器WAN口(路由器后台需要修改获取网络方式),查看isc-dhcp-server日志,是否正确分配IP

sudo journalctl -fu isc-dhcp-server