How to connect headless to a Raspberry Pi unit from a Linux PC using only a cable, including how to find the remote IP in several ways.
This is how to connect to your Pi with only an Ethernet cable while not knowing the IP address of the Pi. To provide an use-case for this guide the aim will be to connect to the Pi from the Linux PC using SSH.
(Edition 4)
You will need:
The guide has an extra number of ways to solve the task at hand and to avoid some common pitfalls (eg "can't find the IP")
If you want to connect to the Pi using SSH you will need to enable SSH on the Pi first:
Use the "Raspberry Config" tool, or run
# sudo raspi-config
from terminal. You will likely
figure out the rest.
Remove the SD card from the Pi and plug it into the SD card
reader on your Linux PC. Navigate to the folder/partition "/boot/"
and create a file with the name "ssh" (# touch ssh
will do). Then put the card back.
If your PC does not have a SD card reader, you will have to get one with an USB plug.
If you want to use SSH, you should probably make sure your Linux PC has SSH installed. If you have SSH below command will return a path:
# which ssh
No ssh
? - install it: # sudo apt
install ssh
. Retry.
# hostname -I
Plug in the cable on both devices. If your PC does not have an "Ethernet port" (a RJ-45 outlet) you will have to get an USB-to-RJ-45 converter plug.
Also, power up the Pi.
If/when you can run this in a terminal and get a sensible reply you already have a connection:
# ping raspberrypi.local
This may, or may not, be all you need to do.
Ping as above, and if you get a sane response you're set. Just 'head for "SSH" instructions below if you want to (it's easy).
If you're still not connected, no problem
So, no luck I guess. No big deal. The Raspberry IP-address will start with 169.254., so we're half way already.
# sudo ifconfig
Identify your Ethernet connection and look for an IP starting with "169.254". Your Ethernet connection may be called eg. "eth0", or even something as exotic as "enp1s0". It's all good.
Note this name! Below we will use "ETHNAME
" as
placeholder.
So, no ifconfig
? - Well, it's deprecated. But you
may still install it: # sudo apt install net-tools
.
Alternatively use "ip" (the replacement for ifconfig), eg. one of these:
# ip address
# ip a
# ip r
- you know what to look for.
So, no IP? No problem:
# sudo arp raspberrypi.local
Still no luck? Try:
# sudo arp -a | grep -E --ignore-case 'b8:27:eb|dc:a6:32'
This looks for the MAC-address of the Pi. Those starting with "b8" are for Pi version =< 3 and those starting with "dc" are for version 4.
So, no arp
? - install it: # sudo apt
install arping
. Retry.
Still no luck? Try:
# sudo arp-scan --interface=ETHNAME 169.254.0.0/16
- and be patient. After a while you may see the likeness of:
169.254.123.4 b1:23:cd:45:6e:7f Raspberry Pi Foundation
First block is the IP of the PI, second block is its MAC address.
So, no arp-scan
? - install it: # sudo apt
install arp-scan
. Retry.
Still no luck? Try:
# sudo tcpdump | grep 169.254
- and be patient. After a while you may see the likeness of:
11:50:45.400179 ARP, Request who-has 192.168.123.4 tell 169.254.123.4, length 46
IP after "tell" belongs to Raspberry
So, no tcpdump
? - install it: # sudo apt
install tcpdump
. Retry.
Below we will use "PI-IP
" as a
placeholder for the Raspberry Pi IP address. Ping it to see how it
responds
# ping PI-IP
The placeholder "PI-IP"" should be replaced with the IP you found above.
This is the easy part ("PI-IP" below is still just a placeholder):
# ssh pi@PI-IP
(terminal only)
# ssh -Y pi@PI-IP
(gui programs enabled)
You may even be able to do:
# ssh pi@raspberrypi.local
To close the SSH connection, enter "exit" in the connected terminal:
# exit
There are official pages on this:
Also, there are many more ways to do it, eg. by using nmap
to find IPs, using router hardware, manipulating the "config.txt",
"cmdline.txt", and/or your "interfaces" file, or setting up a
DHCP-server on your Linux box. Let's not go there.
Document URL: http://clsc.net/articles/linux-pc-to-pi-cable-ssh.php