7

I am purchasing another RPi because my original is broken, but not entirely, so I would like to chain them together via and ethernet cable. Each Pi will also have access to the internet via wifi, but can I take the wifi adapter out of the first and use ssh via the local connection made by the ethernet cable? Is there any software I need for this, and how whould I go about doing this?

Patrick Cook
  • 6,365
  • 7
  • 37
  • 63

3 Answers3

5

Yes, you can do it.

You can use a crossover cable, but you don't need one (most modern interfaces automatically detect).

You don't need any special software. (At least if you are running a recent version of Raspbian which uses dhcpcd).

The hard part is finding the IP of the "slave". I have avahi on my Pi so I can "discover" with hostname.local. (You probably want to make sure each has a unique hostname). I am pretty sure avahi starts automatically on newer kernels, or by dhcpcd.

I connect with ssh pi@hostname.local

dhcpcd allows ssh to work over a link-local address, otherwise you may need to use dhcp or assign static IP.

One drawback of the above is that the date will not be set. You can copy the date from the host by running ssh pi@hostname.local sudo date -s$(date -Ins) before connection.

Milliways
  • 59,890
  • 31
  • 101
  • 209
  • Is a crossover cable the same as a male to male ethernet cable? – Patrick Cook Oct 23 '15 at 17:33
  • @PatrickCook No. In the bad old days there were several wiring standards for Ethernet connections. 4, 6 or 8 wires and the connections were different on uplink and downlink. Crossover cables were to connect 2 peers (such as 2 computers). Most Ethernet chips now autonegotiate which pairs are used. – Milliways Oct 23 '15 at 23:31
  • I haven't tried this (I'm guessing niether have you), but I don't think it will work the way you think it will work if both pis have wifi enabled, as per the question. They will simply connect to each other over wifi, not the ethernet cable. – goldilocks Oct 24 '15 at 13:07
  • @goldilocks it definitely does. I currently have a model B connected to a B+ via Ethernet cable with the B+ connected via WiFi. I believe, from some of your other posts, that you don't use dhcpcd - in which case it won't unless you use static IP. – Milliways Oct 24 '15 at 21:54
  • @goldilocks Of course the method I suggested DOES NOT give the slave internet access, but that was not the question. I use this often. I can just plug my Pi into any computer (that supports zeroconf), regardless of IP range, not just a Pi, and access it. – Milliways Oct 24 '15 at 22:17
  • That's not what I meant. The OP seemed to me to be asking about having two pis, both of which already have inet access via wifi, and establishing an independent connection between them with the ethernet cable -- although reading it again maybe I have misinterpreted an ambiguity there (esp. since doing so would be of dubious value). Anyway, no doubt this will work if one of the pis is offline...which is the more commonsense application. Probably I am too prone to assuming "commonsense" isn't the context ;) – goldilocks Oct 25 '15 at 00:28
  • @goldilocks I probably misinterpreted your comment but to me "can I take the wifi adapter out of the first and use ssh via the local connection" implies no WiFi – Milliways Oct 25 '15 at 00:31
  • Well, hopefully Patrick has enough to go on no matter what at this point. – goldilocks Oct 25 '15 at 00:36
4

This is using a regular (not cross-over) ethernet cable, although I think it is the same either way (contemporary NICs, including the one on the pi, don't require a cross-over).

I don't use normal network autoconfiguration; in order to try this you'll probably have to disable that (see here).

I attached wifi adapters to both pis so I could ssh in and configure them, since they are headless, then plugged an ethernet cable between them. After booting the wifi adapters were on and the lights came on both sides of the cable indicating a link.

All commands require root privileges or sudo.

First, the interface has to be set "up":

ip link set eth0 up

Next, set a private subnet IP address on the interface, such as:

ip addr add 10.66.66.1/24 dev eth0

This is the point where I first got snafu'ed. If you don't specify the CIDR mask explicitly (/24 in this case), the system may use /32. This would make that address the only one on the subnet, meaning you won't be able to reach anyone else this way.

To explain the CIDR address and mask briefly:

  • The address: 10.x.x.x is reserved for private networks, i.e., there are no WWW IPs here, so you can use them for your own purposes. Sometimes LAN routers use them; the other common one is 192.168.x.x. You can use that instead, but notice it has fewer x's, meaning it's smaller.

  • The mask: Each number in an IPv4 address is 8 bits wide, and the mask indicates how many bits starting from the left hand side are significant. The reason /32 isn't much good for anything is because it means all four numbers must be a match (i.e., there's only one possible address, and it's yours). /24 indicates only the first three must match, and by using 10.66.66 I've hopefully avoided any other subnets which might be set on another interface. If you don't have any 10. address on your LAN, you can use anything there, and /8 to indicate only the 10 is significant.

The other pi I set to 10.66.66.2/24. Then I tried pinging from one to the other, which should work as long as you don't have any iptables (firewall) rules preventing it.

I could then also use ssh over the eth0 interface from one pi to the other and shut wifi down and remove the adapter on the other pi. If you enabled this at boot, you could thus connect to the other pi without any wifi or other network attached.


With one of the pi's off the main LAN, i.e., just connected to to the other one via an ethernet cable, you can forward requests through the other pi that does have wifi connected. Referring to the latter as "outside" (with the .1 address) and the former "inside" (with the .2 address):

  • On the outside pi:

    sysctl -w net.ipv4.ip_forward=1
    iptables -t nat -A POSTROUTING -o wlan0 -j MASQUERADE
    

    If you already have firewall rules, you'll have to do more than that to allow traffic:

    iptables -I FORWARD -i eth0 -o wlan0 -j ACCEPT
    iptables -I FORWARD -i wlan0 -o eth0 -j ACCEPT
    
  • On the inside pi:

    ip route add default via 10.66.66.1
    

If you get a comment WRT to the address already existing, ip route del default first. I had a problem the first time I tried all this, after disabling the wifi then setting the route using ssh (from outer to inner), despite using the eth connection for ssh. However, it worked fine by setting the route, then disabling the wifi (which requires removing the default first).

You should now be able to, e.g., ping 8.8.8.8 from the inside pi and do all other internet-y things normally.

You can automate this by scripting the commands in order; this works at boot via /etc/rc.local with no wifi on inner.

goldilocks
  • 58,859
  • 17
  • 112
  • 227
  • Isnt there a failing 0 in wlan under "On the outside pi" ? Can all this not be achieved by bridging eth0 and wlan0 in /etc/network/interfaces file or one has to explicitly modify the iptables? – infoclogged Mar 16 '17 at 19:39
  • @infoclogged Thanks for catching the typo. As per the second paragraph, "I don't use normal network autoconfiguration" -> I've never used it to be more accurate, so I generally don't answer networking questions for fear of encouraging my eccentric bad habits. My /etc/network/interfaces is two lines both referring to lo (part of my attitude derives from the fact that Redhat/Fedora derived distros and Debian derived distros deal with this somewhat differently so I do the same thing on both, i.e., use neither approach)... – goldilocks Mar 17 '17 at 09:37
  • ..Bridging may be more efficient here -- the outward connection for the forwarded pi is not fast although between them it is -- but I have not tried that. – goldilocks Mar 17 '17 at 09:37
0

I would recommend using a Crossover Cable between the two pis. This would be the fastest network you could make with two pis.

Then you could create a small network between the two pis on the ethernet interfaces. The netmask, network, and broadcast needs to be the same in order for them to see eachother, but the IPs obviously needs to be different. Or, you could use a dhcp server on one of the pis to assign an ip automatically to the other one.

An alternative to using a crossover would be to use two normal ethernet cables, and plug each pi into a single switch. You will still need to static each pi, or use a dhcp server.

Ace
  • 219
  • 1
  • 2
  • 6
  • 2
    The NICs on the pi, like other modern ethernet interfaces, don't require a cross-over -- they'll automatically detect this kind of linkage and swap the lines appropriately. – goldilocks Oct 24 '15 at 17:22
  • Good to know, I didn't know they could auto switch like that. – Ace Oct 24 '15 at 19:07