mobile wallpaper 1mobile wallpaper 2mobile wallpaper 3mobile wallpaper 4
332 words
2 minutes
Use NAT on Ubuntu to Forward Network Access to a Router
2024-12-15

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 forever

Identify the interfaces connected to the Internet and to the router

Network Configuration#

1. Modify Netplan configuration#

  1. Remove all existing Netplan configuration files:

    sudo rm -rf /etc/netplan/*.yaml
  2. Create a new base configuration file:

    sudo nano /etc/netplan/01-netcfg.yaml
  3. 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: 2
    renderer: networkd
    ethernets:
    enp1s0:
    addresses:
    - 192.168.1.1/24
    dhcp4: false
    gateway4: 192.168.1.254
    nameservers:
    addresses:
    - 8.8.8.8
    - 8.8.4.4
    enx5a5f0a205236:
    dhcp4: true
  4. 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 enp1s0 has a static IP address:

      ip addr show enp1s0

      The 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#

  1. Temporarily enable:

    sudo sysctl -w net.ipv4.ip_forward=1
  2. Permanently enable: Edit the /etc/sysctl.conf file:

    sudo nano /etc/sysctl.conf

    Ensure the following line is not commented:

    net.ipv4.ip_forward=1
  3. Apply the configuration:

    sudo sysctl -p

3.2 Configure NAT forwarding#

  1. Add NAT forwarding rules:

    sudo iptables -t nat -A POSTROUTING -o enx5a5f0a205236 -j MASQUERADE
    sudo iptables -A FORWARD -i enx5a5f0a205236 -o enp1s0 -m state --state RELATED,ESTABLISHED -j ACCEPT
    sudo iptables -A FORWARD -i enp1s0 -o enx5a5f0a205236 -j ACCEPT
  2. Save the rules:

    sudo apt install iptables-persistent
    sudo netfilter-persistent save
    sudo 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 update
sudo apt install isc-dhcp-server

4.2 Configure DHCP#

Edit /etc/dhcp/dhcpd.conf file:

sudo nano /etc/dhcp/dhcpd.conf

Add 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-server

Set:

INTERFACESv4="enp1s0"

4.3 Start DHCP Service#

Start the DHCP service and check its status:

sudo systemctl restart isc-dhcp-server
sudo systemctl status isc-dhcp-server

5. Validate network sharing#

  1. Check NAT and IP forwarding are active:

    sudo iptables -t nat -L -v
    cat /proc/sys/net/ipv4/ip_forward
    • Ensure the NAT rules exist.
    • cat /proc/sys/net/ipv4/ip_forward should return 1.
  2. Test network connectivity on devices connected to enp1s0:

    • Ensure devices obtain IP addresses via DHCP.
    • Test whether devices can access the Internet.
Share

If this article helped you, please share it with others!

Use NAT on Ubuntu to Forward Network Access to a Router
https://dreaife.tokyo/en/posts/ubuntu-nat-routing/
Author
dreaife
Published at
2024-12-15
License
CC BY-NC-SA 4.0

Some information may be outdated

Related Posts Smart
1
Install oh-my-zsh and Its Components on Ubuntu
prog-side Installing oh-my-zsh and its components on Ubuntu includes first installing zsh and git, then installing oh-my-zsh via wget. Next, clone the powerlevel10k theme and required plugins, and update the .zshrc file to apply the theme and plugins. Finally, install additional plugins such as zsh-bat and you-should-use, and update the system to ensure everything works properly.
2
Configure Docker + code-server on Alibaba Cloud to Build an Online Compiler
cs-base Set up an online compiler by installing Docker and code-server. The process includes installing Docker, configuring an Alibaba Cloud mirror, running Nginx, installing and configuring code-server, and setting up a C/C++ build environment, then successfully running test code.
3
Experiment 8: Deployment and Application of a Web Server
cs-base This experiment aims to understand the structure of email systems, client-server communication, and SMTP/POP3 protocols. By installing and deploying Nginx and Apache on Alibaba Cloud, it demonstrates access to static and dynamic web pages, resolves dependency package installation issues, and improves understanding of Linux software configuration and programming ability.
4
Experiment 1: Common Network Commands
cs-base This experiment aims to understand the use of Linux and Windows command lines and become familiar with common network commands such as ping, ifconfig, traceroute, arp, and netstat. It includes testing network connectivity, displaying TCP/IP configuration, measuring packet paths, viewing the ARP cache, and checking active TCP connections. Through the experiment, command-line usage was mastered, differences in parameters of equivalent commands across operating systems were recognized, and programming ability was improved.
5
Web Crawling Basics
spider A web crawler is an automated program used to obtain information from web pages. Its basic workflow includes sending HTTP requests to retrieve page source code, extracting the required data, and saving it. Since web pages are built from HTML, CSS, and JavaScript, crawlers need to handle both static and dynamic pages. Sessions and cookies maintain user state, while proxy servers can hide the real IP address. Common request methods include GET and POST, and response status codes indicate request results. Crawlers should follow anti-scraping constraints and use proxies and proper headers to improve efficiency.

Table of Contents