Score:0

share two network interfaces on linux

cn flag

I have a Linux PC (ubuntu 22.04) with a network layout as below:

Internet <-> Router <-> PC <-> edge device <-> Switch <-> APs <-> devices

And with two Lan interfaces as below:

  • eno1: Connected to the internet (directly connected to the router), gets IP: 192.168.1.165 from the router (DHCP)
  • enx3c: connected to an edge device and that edge device is connected to a switch, also so many devices are connected to that switch.

I want clients connected to enx3c to have access to the internet on eno1. And also I need to be able to directly communicate with devices under the enx3c interface.

I have followed suggestions in here and so many other tutorials and questions to see how to forward traffic from one interface to another using iptables as follows:

echo 1 > /proc/sys/net/ipv4/ip_forward 
sudo vim /etc/sysctl.conf 
net.ipv4.ip_forward=1
sudo sysctl -p 

iptables -A FORWARD -i eno1 -o enx3c -j ACCEPT 
iptables -A FORWARD -i enx3c -o eno1 -m state –state ESTABLISHED,RELATED -j ACCEPT 
iptables -t nat -A POSTROUTING -o eno1 -j MASQUERADE

After doing so, the edge device didn't get any IP, so I set up a DHCP server on the PC with the below steps:

sudo apt install isc-dhcp-server
sudo vim /etc/dhcp/dhcpd.conf 
    subnet 192.168.137.0 netmask 255.255.255.0 { 
    default-lease-time 600; 
    max-lease-time 7200; 
    authoritative; 
    range 192.168.137.1 192.168.137.200; 
    option routers 192.168.137.254; 
    option domain-name-servers 192.168.137.1, 192.168.137.2; 
}

sudo vim  /etc/default/isc-dhcp-server 
INTERFACESv4="enx3c" 
#so the DHCP server produces Ips for clients connected to the second interface

sudo systemctl start isc-dhcp-server

And the eno1 interface is set to DHCP to get dynamic IP from the router and enx3c is set to manual to get static IP (192.168.137.1).

After starting the DHCP server, the edge device gets an IP in the range, and nmap shows it among live hosts, But there is no access to it! I cannot ping it or ssh into it. And the edge device doesn't have any internet access.

I looked everywhere and it seems that this method must work but I don't know what I am doing wrong. I appreciate any suggestions.

Update: It's worth mentioning that using the answer in here, I used the share to other networks option in the network manager in Ubuntu on the second interface, and after that, it gets IP in a different range, 10.42.0.1/24, and also the edge device gets a new IP in this range, 10.42.0.162, But again, I cannot ping or ssh into this device.

Here is the output for routing tables:

route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.1.1     0.0.0.0         UG    100    0        0 eno1
10.42.0.0       0.0.0.0         255.255.255.0   U     101    0        0 enx3c
169.254.0.0     0.0.0.0         255.255.0.0     U     1000   0        0 eno1
192.168.1.0     0.0.0.0         255.255.255.0   U     100    0        0 eno1
cn flag
Linux is a misery when you're trying to work in the GUI and in the shell at the same time. Also, what you're basically doing here is setting up your PC *as* a router, so having the router and the edge device in the mix is adding a lot of complexity. Try tweaking the NAT rule to explicity list the subnet to be NAT'd e.g: iptables -t nat -s 10.42.0.0/16 -A POSTROUTING -j MASQUERADE That's the thing that leaps out at me.
Score:0
ru flag

I have used a similar approach but for direct computer to computer.

My /etc/dhcp/dhcpd.conf is like this (substituting your in IPs):

subnet 192.168.137.0 netmask 255.255.255.0 { 
    default-lease-time 600;
    max-lease-time 7200;
    authoritative;
    range 192.168.137.1 192.168.137.200;
}

#optional; so enx3c can be set to use DHCP but still having static a ip
host someHostname {
 hardware ethernet mac-address-of-enx3c;
 fixed-address 192.168.137.1;
}

My main method of routing is through ufw, like so (127.0.0.1 being the server, 192.168.130.1 local DHCP address):

ufw route allow from 127.0.0.1 app WWW to 192.168.130.1 app WWW

External ip (192.168.130.20) isn't accessible via NIC (eno1), so I use a route fix:

route add -host 192.168.130.20 dev eno1
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.