mobile wallpaper 1mobile wallpaper 2mobile wallpaper 3mobile wallpaper 4
417 words
2 minutes
Forwarding Network Traffic to a Router via NAT on Ubuntu
2024-12-15

For my setup, the host first obtains an internet connection, then forwards and shares it with the router via an Ethernet cable. The solution is described below. PS: This configuration was performed on a fresh Ubuntu installation. If your environment contains configurations that need to be retained, back them up first to avoid losing them.

Check the 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
# inet6 fe80::8590:b5db:d80c:eae8/64 scope link noprefixroute
# valid_lft forever preferred_lft forever
#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 network interfaces connected to the internet and the router.

Network Configuration#

1. Modify the Netplan Configuration#

  1. Remove all existing Netplan configuration files:

sudo rm -rf /etc/netplan/*.yaml 2. Create a new basic configuration file: shell sudo nano /etc/netplan/01-netcfg.yaml 3. Add the following content to enable DHCP on the internet-facing interface and configure a static IP address for the network interface connected to the router: yaml 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: shell sudo netplan apply ```#

2. Verify the Network Configuration#

Run the following command to check whether each interface has successfully obtained an IP address:

ip addr
  • Expected state:
    • The internet-facing interface should obtain an IP address via DHCP.
    • The router-facing interface should be assigned a static address. Run the following command to check whether enp1s0 has been assigned a static IP address:

ip addr show enp1s0 The output should include: plain text inet 192.168.1.1/24 scope global enp1s0 ```#

3. Configure Network Sharing for the Router#

In my network layout, enx5a5f0a205236 provides the internet connection, which is shared with the router through enp1s0:

3.1 Enable IP Forwarding#

  1. Enable it temporarily:

sudo sysctl -w net.ipv4.ip_forward=1 2. Enable it permanently by editing the **`/etc/sysctl.conf`** file: shell sudo nano /etc/sysctl.conf Ensure that the following line is uncommented: plain text net.ipv4.ip_forward=1 3. Apply the configuration: shell 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: shell sudo apt install iptables-persistent sudo netfilter-persistent save sudo netfilter-persistent reload ```#

4. Configure the DHCP Service#

The router’s WAN interface needs to obtain an IP address through enp1s0, which requires configuring a DHCP service.

4.1 Install the DHCP Service#

Install isc-dhcp-server:

sudo apt update
sudo apt install isc-dhcp-server

4.2 Configure DHCP#

Edit the /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 used by the DHCP service:

sudo nano /etc/default/isc-dhcp-server

Set:

INTERFACESv4="enp1s0"

4.3 Start the DHCP Service#

Start the DHCP service and check its status:

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

5. Verify Network Sharing#

  1. Check whether NAT and IP forwarding are working:

sudo iptables -t nat -L -v cat /proc/sys/net/ipv4/ip_forward ``` - Ensure that the NAT rules are present. - cat /proc/sys/net/ipv4/ip_forward should return 1. 2. **Test the network connection on a device connected to **enp1s0****: - Ensure that the device obtains an IP address via DHCP. - Test whether the device can access the internet.

Share

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

Forwarding Network Traffic to a Router via NAT on Ubuntu
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
Installing Oh My Zsh and Its Plugins on Ubuntu
cs-base To install Oh My Zsh and its components on Ubuntu, first install Zsh and Git, then install Oh My Zsh using wget. Next, clone the Powerlevel10k theme and required plugins, and update the .zshrc file to enable them. 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
CSAPP Chapter 1: A Tour of Computer Systems
cs-base A computer system consists of hardware and system software and runs applications through a program life cycle of creation, execution, output, and termination. Information is made of bits and context, and programs are transformed into executables by the compilation system. The processor reads and executes instructions, using caches to improve performance. The operating system manages hardware and provides abstractions such as processes and virtual memory to support concurrency and parallelism. Abstraction is a core concept in computer science, and virtual machines provide an abstraction of an entire computer.
4
About Errors When Using pandas.to_datetime with Different Time Formats
cs-base When using the pandas to_datetime function, errors may occur if date values use different formats. Setting the format parameter to 'mixed' can solve issues caused by inconsistent formats. Example code shows how to handle invalid date formats and successfully convert values to datetime.
5
missing-semester-class01
cs-base Introduces the basic features and usage of the shell, including running programs, navigating paths, managing file permissions, redirecting input and output streams, and administering root privileges. It also provides multiple Bash command examples and exercises to reinforce the material.

Table of Contents