2

Question: How can I set up two machines, so that when I directly connect them together with an ethernet cable, one machine can connect to the other? I'm looking for a solution that does not involve setting static IP addresses.

Motivation: I have a Raspberry Pi that is taking measurements with sensors attached to it in a location without ethernet access to the internet. I'd like to be able to plug a laptop into the Pi and transfer the data off of it without having to turn off the Pi. I'd like to avoid static IP's so that I don't have to change the configuration of the laptop each time it needs to plug into the Pi. Also, I want to occasionally bring the Pi back to a wired ethernet connection, plug it in, and run software updates on it without having to change the set up on the Pi.

What I tried: I followed the steps listed on the Arch wiki and this blog post for setting up Avahi to use the .local TLD. Currently, the laptop is using Arch Linux and the Pi is using Ubuntu Mate. I have avahi-daemon running on both systems with the nss-mdns library installed. On the laptop, I edited the hosts line in /etc/nsswitch.conf to read

hosts: files mdns_minimal [NOTFOUND=return] dns myhostname

What I see: When the ethernet cable is first plugged in, avahi-browse -alr shows

+ enp1s0 IPv6 rpi2b0 [b8:27:eb:65:2e:43]                    Workstation          local
= enp1s0 IPv6 rpi2b0 [b8:27:eb:65:2e:43]                    Workstation          local
   hostname = [rpi2b0.local]
   address = [fe80::ba27:ebff:fe65:2e43]
   port = [9]
   txt = []

After a few minutes, avahi-browse -alr does not find the Pi when it is rerun. While the Pi is found by avahi-browse, it also can be found by avahi-resolve, but once it is not found by avahi-browse it no longer is found by avahi-resolve. Running ping rpi2b0.local shows that ping resolves rpi2b0.local to the IPv6 address listed by avahi-browse but results in Address unreachable. ssh gives the error: debug1: connect to address fe80::ba27:ebff:fe65:2e43 port 22: Invalid argument.

  • One option would be to run a dhcp server on the laptop, with a static MAC entry for the raspberry pi (so your laptop doesn't answer for other hosts when used elsewhere, which network admins hate), and then set the rpi to be a dhcp client, regardless. – thrig Mar 9 '16 at 19:52
  • As @thrig suggested, you just need a DHCP running your laptop. Assigning a specific IP to your raspberry pi based on its MAC address will also helps if you want to setup an automount for the data partition on the rasp-pi. – Bichoy Mar 9 '16 at 19:59
  • Your suggestions helped, thanks! Right now I am using the method here to share my wireless connection over ethernet. A byproduct of this is that it sets up dhcp on the ethernet interface with dnsmasq. I plug in the pi and look at /var/lib/msic/dnsmasq.leases to see its ip address. Then I can ssh into that ip address and it works. I also tried setting up dhcp manually but I couldn't quite get it configured properly and then I found this worked so I stopped trying. – ws_e_c421 Jul 19 '16 at 4:40
2

You can connect your PC and RPi together via an Ethernet cable, that will create a private network between those two.

Wiki page on such network:

In IPv4, link-local addresses are codified in RFC 6890 and RFC 3927. Their utility is in self-autoconfiguration by network devices when Dynamic Host Configuration Protocol (DHCP) services are not available and manual configuration by a network administrator is not desirable.

The block 169.254.0.0/16 is reserved for this purpose, with the exception of the first and the last /24 subnets in the range. If a host on an IEEE 802 (ethernet) network cannot obtain a network address via DHCP, an address from 169.254.1.0 to 169.254.254.255 may be assigned pseudorandomly. The standard prescribes that address collisions must be handled gracefully.

That means both of your machines should be in the 169.254.0.0 address space.

Then I found a couple of blog post that take you through the approach of getting these two to communicate:

https://pihw.wordpress.com/guides/direct-network-connection/

http://embeddedday.com/projects/raspberry-pi/basics/direct-connection-to-pc/

On your PC, select on the Ethernet card, DHCP, you should get assigned a private IP address, like below: enter image description here

In which basically you need to edit on the RPi the file /boot/cmdline.txt and add at the end of the config line the IP you would like to use for the RPi. enter image description here

Then, restart the RPi and you should be able to connect onto it via ssh, if you have it setup of course.

  • Thanks for your answer. I was looking for a solution that did not involve setting a static ip address for the pi (editing /boot/cmdline.txt in your answer). Setting up dhcp on the ethernet interface seems to be the way to do this. The goal is to have the pi running and walk up, plug in, transfer data, and unplug without needing any shared magic numbers between the pi and the laptop. – ws_e_c421 Jul 19 '16 at 4:43
  • This is bad advice. You should not be assigning 169.254 manually. Use anything else from RFC1918. – Habbie Feb 28 '17 at 20:31

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.