一 开启路由转发功能
sudo vim /etc/sysctl.conf
net.ipv4.ip_forward=1
sudo sysctl -p
二 安装DHCP
#更新软件包列表:
sudo apt update
#安装DHCP服务器
sudo apt install isc-dhcp-server
#修改监听网卡,根据实际修改
sudo vi /etc/default/isc-dhcp-server
INTERFACESv4="enp2s0"
INTERFACESv6="enp2s0"
#修改配置文件
sudo vi /etc/dhcp/dhcpd.conf
subnet 192.168.50.0 netmask 255.255.255.0 {
range 192.168.50.30 192.168.50.100; #IP租用地址范围
option domain-name-servers 192.168.50.1; #dns地址,域名或者IP
option domain-name "test"; #dns域名
option routers 192.168.50.1; #网关地址
option broadcast-address 192.168.50.255; #广播地址
default-lease-time 6000; #默认租约为6000秒
max-lease-time 86400; #最大租约为86400s
log-facility local7; # 日志级别
}
#启动dhcp服务
sudo systemctl enable isc-dhcp-server
sudo systemctl restart isc-dhcp-server
#查看服务
sudo systemctl status isc-dhcp-server
三 网卡配置
test@test:~$ sudo cat /etc/netplan/50-cloud-init.yaml
network:
ethernets:
enp2s0:
dhcp4: no
addresses: [192.168.50.1/24]
# gateway4: 192.168.50.1
nameservers:
addresses: [192.168.50.1]
enp3s0:
dhcp4: no
addresses: [192.168.1.8/24]
gateway4: 192.168.1.1
nameservers:
addresses: [192.168.50.1,114.114.114.114]
version: 2
#启用
sudo netplan apply
四 配置iptables防火墙,共享上网snat
iptables -t nat -A POSTROUTING -s 192.168.50.0/24 -j SNAT --to-source 192.168.1.8
iptables-save
五 客户端验证,windows 11为例
打开-->设置-->网络和Internet