2

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.

  • That is posible. And not hard to do. So what did you already try and where did you run into problems? – Hennes Jun 7 '15 at 13:44
  • I edit erh0 to ipv4=85.185.254.69,255.255.255.0,85.185.254.66 which have internet from network 1 and want to share this internet connection to eth1 into another network with ip range 185.185.185.1 – sajadsarlak Jun 7 '15 at 14:05
  • Step 1: Configure eth1 with the new network range. Step 2: Confitgure other computers on this network and test/ping. They will use NIC2 on computer 1 (185.185.185.1) as gateway. Step 3) Now you need to forward packets between the two interfaces. Your computer does this based on its routing table. If that does not for check if ipforwarding is enabled. (sysctl net.ipv4.ip_forward should return 1. If it returns 0 then use echo 1 > /proc/sys/net/ipv4/ip_forward or sysctl -w net.ipv4.ip_forward=1). – Hennes Jun 7 '15 at 14:08
  • [SOLVED] step 1: eth0 configurated to ipv4=85.185.254.69,255.255.255.0,85.185.254.66,dns=4.2.2.4. step 2: eth1 configurated to ipv4=185.185.185.1,255.255.255.0,85.185.254.69. step 3: Restart ubuntu. step 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 – sajadsarlak Jun 7 '15 at 16:04
  • @sajadsarlak - if you add that as a answer so it readable, it will help future users who need help - it can also get upvoted too :) – Wilf Jun 7 '15 at 16:14
3

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'
    
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
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.