3

I have two wired connections of which the first has internet via static IPv4 and gateway and the second connects to another network.

I want to share internet from first connection over second connection with a different IP-address.

Zanna
  • 72,312

3 Answers3

5

I managed to solve this myself. Here are the steps:

  1. eth0 configured like this

    ipv4=85.185.254.69,255.255.255.0,85.185.254.66,dns=4.2.2.4.
    
  2. eth1 configured like this:

    ipv4=185.185.185.1,255.255.255.0,85.185.254.69. 
    
  3. Restart ubuntu.
  4. Open terminal and run:

    sudo bash -c 'echo 1 > /proc/sys/net/ipv4/ip_forward'
    sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
    sudo iptables -A FORWARD -i eth0 -o eth1 -m state --state RELATED,ESTABLISHED -j ACCEPT
    sudo iptables -A FORWARD -i eth1 -o eth0 -j ACCEPT
    
    sudo bash -c 'iptables-save > /etc/iptables/rules.v4'
    sudo bash -c 'iptables-restore < /etc/iptables/rules.v4'
    
Zanna
  • 72,312
3

Edit /etc/sysctl.conf (for example using sudoedit /etc/sysctl.conf) and search for the following lines:

# Uncomment the next line to enable packet forwarding for IPv4
#net.ipv4.ip_forward=1

And uncomment net.ipv4.ip_forward=1 by removing the # at the start of the line:

# Uncomment the next line to enable packet forwarding for IPv4
net.ipv4.ip_forward=1

Or, to do this in a one line command:

sudo sysctl -w net.ipv4.ip_forward=1
Zanna
  • 72,312
0

You could've done it easier than this. If your internet connection is established via some sort of routing device you can just make a VLAN for the connection you want to share internet with to make it to have access to the router subnet.

https://wiki.ubuntu.com/vlan

You can do this in Network Manager GUI easily. Afaik it is available outta box.

Add a virtual connection in NM. Pick VLAN as a connection type.

enter image description here

Parent device is the interface you want to share internet with. After that go to IPv4 settings and set an IP address for it from the router DHCP range. Voila. You got yourself a shared internet connection.