For personal use, I first obtain the network on the host, then forward it over the network, sharing the network with the router via an Ethernet cable.
Here is the solution.
P.S.: This configuration environment is a freshly installed Ubuntu. If your environment has configurations you need to preserve, please back them up first to avoid loss.
View current network status
ip addr# 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,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000# link/ether 68:1d:ef:4a:41:4e brd ff:ff:ff:ff:ff:ff# inet6 fe80::85cf:33e6:14a0:3af6/64 scope link noprefixroute# valid_lft forever preferred_lft forever#3: enp3s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000# link/ether 68:1d:ef:4a:41:4f brd ff:ff:ff:ff:ff:ff# inet 192.168.0.148/24 brd 192.168.0.255 scope global dynamic noprefixroute enp3s0# valid_lft 7094sec preferred_lft 7094sec#4: wlp2s0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default qlen 1000# link/ether bc:2b:02:7c:27:a7 brd ff:ff:ff:ff:ff:ff#5: enx5a5f0a205236: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UNKNOWN group default qlen 1000# link/ether 5a:5f:0a:20:52:36 brd ff:ff:ff:ff:ff:ff# inet 192.168.9.107/24 brd 192.168.9.255 scope global dynamic noprefixroute enx5a5f0a205236# valid_lft 3421sec preferred_lft 3421sec# inet6 fe80::86cb:2037:bfdc:9300/64 scope link noprefixroute# valid_lft forever preferred_lft foreverIdentify the interfaces connected to the Internet and to the router
Network Configuration
1. Modify Netplan configuration
-
Remove all existing Netplan configuration files:
sudo rm -rf /etc/netplan/*.yaml -
Create a new base configuration file:
sudo nano /etc/netplan/01-netcfg.yaml -
Add the following content to enable DHCP on the interfaces and configure a static IP address for the NIC connected to the router:
network:version: 2renderer: networkdethernets:enp1s0:addresses:- 192.168.1.1/24dhcp4: falsegateway4: 192.168.1.254nameservers:addresses:- 8.8.8.8- 8.8.4.4enx5a5f0a205236:dhcp4: true -
Save the file and exit, then apply the configuration:
sudo netplan apply
2. Validate network configuration
Run the following commands to check whether each interface has successfully obtained an IP address:
ip addr- Target state:
-
The configured interfaces should obtain IP addresses via DHCP.
-
The router-facing interface should be assigned a static address.
Run the following command to check whether
enp1s0has a static IP address:ip addr show enp1s0The output should include:
inet 192.168.1.1/24 scope global enp1s0
-
3. Configure router network sharing
According to my network layout, the Internet connection is provided by enx5a5f0a205236 and is shared to the router via enp1s0:
3.1 Enable IP forwarding
-
Temporarily enable:
sudo sysctl -w net.ipv4.ip_forward=1 -
Permanently enable: Edit the
/etc/sysctl.conffile:sudo nano /etc/sysctl.confEnsure the following line is not commented:
net.ipv4.ip_forward=1 -
Apply the configuration:
sudo sysctl -p
3.2 Configure NAT forwarding
-
Add NAT forwarding rules:
sudo iptables -t nat -A POSTROUTING -o enx5a5f0a205236 -j MASQUERADEsudo iptables -A FORWARD -i enx5a5f0a205236 -o enp1s0 -m state --state RELATED,ESTABLISHED -j ACCEPTsudo iptables -A FORWARD -i enp1s0 -o enx5a5f0a205236 -j ACCEPT -
Save the rules:
sudo apt install iptables-persistentsudo netfilter-persistent savesudo netfilter-persistent reload
4. Configure DHCP Service
The router’s WAN needs to obtain an IP address via enp1s0, which requires configuring a DHCP service.
4.1 Install DHCP Service
Install isc-dhcp-server:
sudo apt updatesudo apt install isc-dhcp-server4.2 Configure DHCP
Edit /etc/dhcp/dhcpd.conf file:
sudo nano /etc/dhcp/dhcpd.confAdd the following content:
subnet 192.168.1.0 netmask 255.255.255.0 { range 192.168.1.10 192.168.1.100; option routers 192.168.1.1; option domain-name-servers 8.8.8.8, 8.8.4.4;}Specify the interface for the DHCP service:
sudo nano /etc/default/isc-dhcp-serverSet:
INTERFACESv4="enp1s0"4.3 Start DHCP Service
Start the DHCP service and check its status:
sudo systemctl restart isc-dhcp-serversudo systemctl status isc-dhcp-server5. Validate network sharing
-
Check NAT and IP forwarding are active:
sudo iptables -t nat -L -vcat /proc/sys/net/ipv4/ip_forward- Ensure the NAT rules exist.
cat /proc/sys/net/ipv4/ip_forwardshould return1.
-
Test network connectivity on devices connected to
enp1s0:- Ensure devices obtain IP addresses via DHCP.
- Test whether devices can access the Internet.
If this article helped you, please share it with others!
Some information may be outdated





