Creating the most basic network ever (how to link two computers)
Some time ago, I decided to buy a raspberry pi, as it was fully capable of handling my day to day work needs in cases where I needed to travel. It’s cheap, portable, and runs all linux with all the basic tools that I needed, which is what matters.
But what happens if I need to connect my raspberry pi to my PC, and the internet is out? This article will describe the process of connecting two linux computers together.
The ethernet cable
A simple crossover cable will be needed to connect the two computers. In practice, this would mean any ethernet cable that you’ve got lying around. This also means that there will be no help provided by any device to create a network route between the two machines, and it will have to be done manually.
The first thing to do is to identify the correct interface, in my situation this is obvious, as I only have one ethernet port:
root@linuxpc:~# ip addr show enp42s0
2: enp42s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
link/ether d8:43:ae:ba:51:1f brd ff:ff:ff:ff:ff:ff
Different interfaces might show up with different names, but you will need something like ethX, enX or enpXsX for your ethernet ports.
The next thing here is to use these helpful commands to check whether the physical connection is working:
root@linuxpc:~# cat /sys/class/net/enp42s0/carrier
1
root@linuxpc:~# cat /sys/class/net/enp42s0/operstate
up
The output is laconic, but here’s what it roughly says:
- ->
carrier: 1 ~ the hardware detects an active electrical signal from the network port - ->
operstate: up ~ the interface is active and ready to transfer data
This means the cable is the right type and plugged in well.
Setting up the interface
The first thing we need is a static IP for the two computers. The important thing to know is that the block reserved for self-assigned IPs is 169.254.*.*, so we’ll just go with this.
This needs to be done, because as mentioned, no device is helping with management. When a computer connects to a wifi, the wifi box includes something called a dhcp server which automatically assigns IP address to new member devices of the network. Here, since we just use a simple ethernet cable, that wouldn’t possibly happen.
So let’s assign the IPs.
The desktop PC:
root@linuxpc:~# ip addr add 169.254.0.1 dev enp42s0
The raspberry:
root@pi:~# ip addr add 169.254.0.2 dev eth0
Now the two computers can exchange information (via the crossover ethernet cable), and be uniquely identifiable on a hypothetical network via these IP addresses.
However, at this point, neither machine knows that it should be part of a network.
Dealing with routing
Basically, even though it is a simple cable between machine 1 and machine 2, there is no formal definition of this path available to either system at the moment, so neither machine knows where to send packets to, even if this physical layout feels obvious.
The route tells Linux which interface should be used when sending packets to that address range. Without a route, the operating system can understand the destination IP, but not where to send the packets to reach it.
So lets add the route manually:
root@linuxpc:~# ip route add 169.254.0.0/24 dev enp42s0 proto static
This command adds a route to an address range of 169.254.0.* over the enp42s0 interface.
The static flag indicates that the route was created manually, not by the system, as an intentional override for any dynamic routing.
Checking it:
root@linuxpc:~# ip route | grep enp42s0
169.254.0.0/24 dev enp42s0 proto static scope link
We should be able to now ping the raspberry pi:
root@linuxpc:~# ping -c3 169.254.0.2
PING 169.254.0.2 (169.254.0.2) 56(84) bytes of data.
64 bytes from 169.254.0.2: icmp_seq=1 ttl=64 time=0.229 ms
64 bytes from 169.254.0.2: icmp_seq=2 ttl=64 time=0.225 ms
64 bytes from 169.254.0.2: icmp_seq=3 ttl=64 time=0.226 ms
--- 169.254.0.2 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2069ms
rtt min/avg/max/mdev = 0.225/0.226/0.229/0.001 ms
Or even probe the ssh port:
root@linuxpc:~# nc -vz 169.254.0.2 22
Connection to 169.254.0.2 22 port [tcp/ssh] succeeded!
Conclusion
This is a nice alternative to looking for the tiny USB dongle somewhere in the abyss of the drawer.
However, if you have more than two computers, this gets messy, as most computers do not have a lot of ethernet ports (for example, a raspberry typically has only one), at which point you’d need an external device, like a network switch to distribute messages. An unmanaged network switch is cheap, and it can solve this problem if you encounter it.