LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Networking (https://www.linuxquestions.org/questions/linux-networking-3/)
-   -   How to share Internet Access through ethernet cable (https://www.linuxquestions.org/questions/linux-networking-3/how-to-share-internet-access-through-ethernet-cable-4175500704/)

prompt32 04-05-2014 04:19 PM

How to share Internet Access through ethernet cable
 
Hi, my question here is : How to share Internet Access through ethernet cable

I have connect 2 machines trhough en ETH cable by success (ping each other, up/down load files, ssh ).
So, now, i want to share my wirelless internet connection.

Here is my setup :
Code:


                                              // >> INTERNET
                                            //
                                ((*))  << //
    ------                wlan0  |            --------
    |      |                      |            |      |
    | pc-1 |                      +---------->>| pc-2 |
    |      |                                  |      |
    ------                                    --------
      || PCI eth                    onBoard ETH  ||
      || <<<=================================>>> ||
      ||  eth0        eth-X cable          eth0  ||
  -----------                                -----------
  |          |                              |          |
  -----------                                -----------
    eth0                                      eth0
      ip: 10.10.10.11/24                        ip: 10.10.10.10/24
      bc: 10.10.10.255                          bc: 10.10.10.255
                                            wlan0
                                                ip: 192.168.1.64/24
                                                bc: 192.168.1.255
                                                gw: 192.168.1.254

( pc-2 ) - Kernel IP routing table
Code:

  Destination    Gateway        Genmask        Flags Metric Ref    Use Iface
  0.0.0.0        192.168.1.254  0.0.0.0        UG    0      0        0 wlan0
  10.10.10.0      0.0.0.0        255.255.255.0  U    0      0        0 eth0
  192.168.1.0    0.0.0.0        255.255.255.0  U    0      0        0 wlan0

Some test pings :
Code:

ping -I wlan0 -c 1 10.10.10.10 : error !
ping -I wlan0 -c 1 google.com : ok.
ping -I eth0 -c 1 192.168.1.254 : error !
ping -I eth0 -c 1 google.com : error !

Can somebody give me some help here ?
PS: i would like to do it, not through the NetworkManager

thanks

Philip Lacroix 04-05-2014 06:07 PM

Hi. You will have to set up IP masquerading with NAT (Network Address Translation) on your local gateway box (i.e. "pc-2"), so that connections coming from "pc-1" and directed to hosts on the Internet will be automatically forwarded from the internal (10.10.10.10) to the external (192.168.1.64) interface. This is possible by defining rules with the "iptables" command, which is part of netfilter, the default firewalling and packet filtering software on GNU/Linux systems. There are also several front-ends for iptables that can help with setting up the rules (one or more front-ends might already be included in your distribution).

Some related documentation:
Documentation about the netfilter/iptables project
IP Masquerade and Network Address Translation
Linux IP Masquerade HOWTO
Linux 2.4 NAT HOWTO

Regards

Steve R. 04-05-2014 06:15 PM

Why are you proposing the configuration shown? Why not use a router? We have a variety of devices that can simultaneously connect to the internet through a router (24/7). One limitation of your configuration is that pc-2 would have to be on in-order for pc-1 to have Internet access. This reminds that Microsoft Windows Version ??? (circa 1990 something) which used to have the option of designating one computer as a "gateway" so that other computers on the home LAN could access the internet. Anyway, beyond the advice of using a router, I do not have the technical knowledge you are requesting.

Philip Lacroix 04-05-2014 07:12 PM

Quote:

Originally Posted by Steve R. (Post 5147325)
Why not use a router?

Apart from giving a LAN the access to another network (e.g. the Internet), a gateway box is useful whenever you want to have some additional (and controlled) filtering protection for your internal machines. B.t.w. the concept of gateway was not invented by wind0w$, but many years before, when networking was. While the configuration proposed by the OP shows only one box as part of the internal LAN, he might as well use a switch to allow more machines to connect through the gateway. Of course this makes more sense if a firewall is also configured and enabled on the gateway itself, while plugging the machine/switch directly into the (ISP) router would remove that additional and controlled filtering "layer".

jefro 04-05-2014 08:13 PM

It was a great drawing for a first time question too.

Pretty sure Philip Lacroix has the solution that you asked for.

Since you say manager, see alt solutions here maybe. https://help.ubuntu.com/community/In...nectionSharing

schneidz 04-05-2014 08:50 PM

this is what i do:
http://www.linuxquestions.org/questi...6/#post3804866

prompt32 04-10-2014 10:35 AM

1st of all, iwant to thanks all that reply.
I use Linux in both machine (Debian,kernel 3.12.6)

i still read the documentation that 'Philip Lacroix' points to, its more than full ...
because some already examples, found on net dont work in my case (or maybe i do something wrong ...)

Like, in 'schneidz's example ,
if i set the gateway of an iface, at an address on a different network, the (route, ip route,..),
dont accept this by error : "interface unreachable"

ie:
Code:

route add  -net 10.10.10.0 netmask 255.255.255.0 gw 192.168.1.254 dev eth0
SIOCADDRT: Network is unreachable

and for what i have notice so far,
i think that to enable ip4-forwarding by the echo command,
before or after the route/iptable commands, have a meaning in the way, the above commands acts...,
Is this true, or not ?


I read the posts, but i will post again when i will have something to say, or when solved,
dont think my silince as ignorance.
Thank's

prompt32 11-16-2014 06:33 PM

How to share Internet Access through ethernet cable. DONE.
 
Quote:

route add -net 10.10.10.0 netmask 255.255.255.0 gw 192.168.1.254 dev eth0
The above and similar code dont work for me on 'pc-2' machine, (maybe because 2 interfaces exists ...)

but
Code:

route add default gw 192.168.1.254
works fine, (of course this happens, after NetworkManager associated the wlan0 interface), so:

according to the settings in my 1st post;

1) set the deafult gateway on the pc-1, points to the 'eth0' interface of the 'pc-2'
2) run a small script of iptables rules, for 'pc-2'

1)
Code:

route add default gw 10.10.10.10
2) let's say eth0-iptables.sh
Code:

#!/bin/bash

[ -n "$IFACE" -a "$IFACE" != "eth0" ] && exit 0


echo_iptables (){
          iptables -nvL --line-numbers
          return 0
          }

[ "$1" = "show" ] && \
        echo_iptables && \
        exit 0


echo "[i] `basename $0`, Clear IPtables ..."

        iptables -F
        iptables -X
        iptables -t nat -F
        iptables -t nat -X
        iptables -t mangle -F
        iptables -t mangle -X
        iptables -P INPUT ACCEPT
        iptables -P OUTPUT ACCEPT
        iptables -P FORWARD ACCEPT


[ "$1" = "stop" -o "$MODE" = "stop" ] && \
        echo "[*] `basename $0`, Nothing to do, exit." && \
        echo_iptables && \
        exit 0

        iptables -F INPUT
        iptables -A INPUT -j ACCEPT

        iptables -F FORWARD
        iptables -A FORWARD -j ACCEPT

        iptables -t nat -A POSTROUTING -o wlan0 -j MASQUERADE



echo "[i] `basename $0`, IPtables are now set to :" && echo_iptables


sysctl -w net.ipv4.ip_forward=1 > /dev/null 2>&1
exit 0

Code:

You can add this code to '/etc/rc.local'
BUT, I PREFFER TO, ..., make two links of this script,
  1) /etc/network/if-up.d/ - Scripts to execute when an/all interface(s) goes UP
  2) /etc/network/if-post-down.d/ - Scripts to execute when an/all interface(s) goes DOWN 

 When system calls this scripts, it sets the following vars
    IFACE - the name of the interface or all
      MODE - 'stop' when an ie : ifdown <iface> command happens
          - 'start' when an inerface goes up
  ADDRFAM - 'inet'

I know its been a long time, but it was such a long time ....
:hattip:
... really thanks, everyone, here !


All times are GMT -5. The time now is 11:04 AM.