linux - Connect devices plugged into Raspberry Pi ethernet to WiFi network

07
2014-07
  • Tom

    I'm just starting out on a mission to learn more about networking and I've followed a tutorial (http://raspberrypihq.com/how-to-turn-a-raspberry-pi-into-a-wifi-router/) to turn my Raspberry Pi into a wifi router.

    That worked really well so I modified it slightly so that I can use a tethered iphone for the internet connection - I just switched all "eth0" references to "eth1" (the iphone interface) and added a script to set everything up when the phone is plugged in.

    This setup has freed up the Pi's ethernet port so I'd like to try and take this a step further and allow devices plugged into it to connect to the network. If possible, I'd like to try adding a switch so I can connect multiple devices.

    I've tried fiddling around with nat & iptables with no luck so my question is, how can I connect devices on eth0 to my wlan network?


    EDIT:

    Some extra info below. These are from after the phone is plugged in.

    Output from iptables -t nat -L:

    Chain PREROUTING (policy ACCEPT)
    target     prot opt source               destination         
    
    Chain INPUT (policy ACCEPT)
    target     prot opt source               destination         
    
    Chain OUTPUT (policy ACCEPT)
    target     prot opt source               destination         
    
    Chain POSTROUTING (policy ACCEPT)
    target     prot opt source               destination         
    MASQUERADE  all  --  anywhere             anywhere
    

    Output from netstat -rn:

    Kernel IP routing table
    Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
    0.0.0.0         172.20.10.1     0.0.0.0         UG        0 0          0 eth1
    172.20.10.0     0.0.0.0         255.255.255.240 U         0 0          0 eth1
    192.168.10.0    0.0.0.0         255.255.255.0   U         0 0          0 wlan0
    

    EDIT 2:

    The following is in my dhcpd.conf file:

    subnet 192.168.10.0 netmask 255.255.255.0 {
     range 192.168.10.10 192.168.10.200;
     option broadcast-address 192.168.10.255;
     option routers 192.168.10.1;
     default-lease-time 600;
     max-lease-time 7200;
     option domain-name "local-network";
     option domain-name-servers 8.8.8.8, 8.8.4.4;
    }
    

    dhcp is configured to run on wlan0 - /etc/default/isc-dhcp-server:

    INTERFACES="wlan0"
    
  • Answers
  • MariusMatutiae

    Strictly speaking, not much else is required. I presume you have already enabled IPv4 forwarding, and possibly also this rule:

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

    which will allow you to NAT all of your connections. Then your routing table needs

      ip route add default via IP_ADDRESS_OF_YOUR_PHONE
    

    which it also presumably already has. Then you are good to go.

    EDIT:

    Now that you posted your routing table (but please, do not use obsolete, deprecated commands like *ifconfig, route,..., just the ip command, for instance, for the routing table ip route show, to see the addresses of interfaces ip addr show,...) I see that you do not have a route to your ethernet port-clients. If the network from which you draw IP addresses port cable-connect clients is 192.168.20.0/24, then you will have to add:

       ip route add 192.168.20.0/24 dev eth1
    

    You should not use the same network for wireless- and cable- clients.

    If furthermore you need to configure dhcpd, then edit the same file /etc/dhcp/dhcpd.conf as before, and add the new network:

      subnet 192.168.10.0 netmask 255.255.255.0 {
    option subnet-mask 255.255.255.0;
    option routers 192.168.10.1;
    range 192.168.10.100 192.168.10.200;
    }
    
    subnet 192.168.20.0 netmask 255.255.255.0 {
    option subnet-mask 255.255.255.0;
    option routers 192.168.20.1;
    range 192.168.20.100 192.168.20.200;
    }
    

    In the file /etc/default/isc-dhcp-server, change the line INTERFACES="wlan0" to

     INTERFACES="wlan0 eth0"
    

    and lastly give the router a good address on its eth0 NIC:

     ip link set dev eth0 down
     ip addr flush dev eth0
     ip addr add 192.168.20.1/24 dev eth0
     ip link set dev eth0 up
    

    Restart dhcp, you should be Ok.


  • Related Question

    windows - Enable networking on raspberry pi - Internet sharing with Win7
  • Kyle

    I do not have physical access to the wireless router that I use (shared living agreement), so I cannot connect my RasPi (Model B) to the router via Ethernet. However, I am running a Windows 7 64-bit machine that connects over WiFi and has an unused Ethernet port.

    I've set up Internet sharing and bound the Ethernet port to be the device that shares the Internet, and I'm running Debian Wheezy (2012-07-15 build) on my RasPi on an 8 GB SD card. However, when I plug the Ethernet cable into the RasPi and my local Win7 box, nothing seems to work on the RasPi side.

    I get tx/link lights on my Windows machine as well as the RasPi, but when I run ifconfig, I get eth0 with 12 tx packets, 0 errors, 0 dropped, etc - and no IP bound to it.

    I've tried editing /cat/networking/interfaces and tried to enable eth1-9 (this seems to be a weird bug that some users report where eth0 isn't the properly bound device), but all fail when I do sudo /etc/init.d/networking restart except eth0.

    Google has failed me so far - what's the next step in getting wired/shared Internet working? Bridging the devices on the Windows machine?

    Thanks!


  • Related Answers
  • Kyle

    Figured it out - I had to edit /etc/network/interfaces with this info:

    auto lo eth0
    iface lo inet loopback
    iface eth0 inet dhcp
    

    The first line by default was auto lo, so I added the eth0 and restarted the networking services and it worked perfectly!