LogicalUnit
Posts: 14
Joined: Thu Jan 03, 2013 9:01 am

SSH to a local Raspberry Pi

Tue Aug 04, 2020 8:15 am

Hi everyone, I have a Raspberry Pi 1 running the latest version of Raspberry Pi OS lite. I would like to SSH into it so I can work on the command line. My home network has DHCP, and I can easily connect the Pi to that and determine the IP address from my router.

I would like to be able to work on the command line on my laptop, away from my home network. I have a USB ethernet adaptor, and have connected the Pi to it using an ethernet cable. My laptop has Windows 10. According to ipconfig the IP address of the ethernet adaptor is 169.254.119.14 but I don't know how to get the IP address of the Pi.

Previously I have had luck using raspberrypi.local as an IP address for my Raspberry Pi Zero, but that isn't working here.

Can someone tell me how to find the IP address of my locally-connected RPi? TIA

PS I have created a file called ssh in the boot partition, so it isn't that.
Last edited by LogicalUnit on Tue Aug 04, 2020 12:11 pm, edited 1 time in total.

hippy
Posts: 7911
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: SSH to a local Raspberry Pi

Tue Aug 04, 2020 11:28 am

Without any DHCP provider your laptop is getting a local link address. Not sure what the Pi would get. The easiest solution would be to set your laptop and Pi to have static IP addresses in the same network range; eg 192.168.123.1 on the laptop, 192.168.123.2 on the Pi.

GlowInTheDark
Posts: 744
Joined: Sat Nov 09, 2019 12:14 pm

Re: SSH to a local Raspberry Pi

Tue Aug 04, 2020 11:45 am

First, note that using the 169.254.x.y addresses is perfectly fine for networking. I do it all the time for RPis that are directly connected to other RPis. But, as noted, the trick is to figure out what that address is. You can figure it out on your end, of course, but how to know what is on the other end?

Now, of course, the easiest is to hookup a monitor and keyboard to the target machine, then you can do the (one-time) step below with no further fanfare [*]. But if you absolutely must do it completely headless, then you can use "nmap". nmap will scan for and report which 169.254.x.y IP address the target machine has; then you can ssh into it. Either way, you now have access to the machine.

Now, on the target machine, edit /etc/rc.local and put in:

ifconfig usb0 169.254.X.Y

where usb0 is the device (obviously) and X and Y are any two numbers (between 10 and 250) you like. Be sure to write these numbers (X and Y) down somewhere.

Now, you're done. Whenever you need to ssh in, you will know which IP address to use.

[*] Or do it via ssh when you have the machine hooked up to a "normal" network.
GitD's list of things that are not ready for prime time:
1) IPv6
2) 64 bit OSes
3) USB 3
4) Bluetooth

User avatar
thagrol
Posts: 3178
Joined: Fri Jan 13, 2012 4:41 pm
Location: Darkest Somerset, UK
Contact: Website

Re: SSH to a local Raspberry Pi

Tue Aug 04, 2020 12:17 pm

@GlowInTheDark:

Would it not be easier to ensure both machines have unique hostnames and use, say, ssh hostname.local? Or does that not work with the link local subnet?
Arguing with strangers on the internet since 1993.

User avatar
DougieLawson
Posts: 39304
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: SSH to a local Raspberry Pi

Tue Aug 04, 2020 3:40 pm

thagrol wrote:
Tue Aug 04, 2020 12:17 pm
@GlowInTheDark:

Would it not be easier to ensure both machines have unique hostnames and use, say, ssh hostname.local? Or does that not work with the link local subnet?
It will work as long as avahi-daemon is running. It works on the 169.254.xxx.xxx/16 subnet.
The other way is to scan with nmap.

sudo nmap -v -sn 169.254.0.0/16
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

GlowInTheDark
Posts: 744
Joined: Sat Nov 09, 2019 12:14 pm

Re: SSH to a local Raspberry Pi

Tue Aug 04, 2020 3:57 pm

I mentioned "nmap" in my post.
GitD's list of things that are not ready for prime time:
1) IPv6
2) 64 bit OSes
3) USB 3
4) Bluetooth

User avatar
neilgl
Posts: 2226
Joined: Sun Jan 26, 2014 8:36 pm
Location: Near Aston Martin factory

Re: SSH to a local Raspberry Pi

Tue Aug 04, 2020 6:13 pm

Is the key point away from my home network. ? So take laptop to Starbucks say, and work from there over the internet?

User avatar
thagrol
Posts: 3178
Joined: Fri Jan 13, 2012 4:41 pm
Location: Darkest Somerset, UK
Contact: Website

Re: SSH to a local Raspberry Pi

Tue Aug 04, 2020 6:56 pm

neilgl wrote:
Tue Aug 04, 2020 6:13 pm
Is the key point away from my home network. ? So take laptop to Starbucks say, and work from there over the internet?
The comment about having them connected over USB implies both the Pi and laptop are in the same physical location plus how are you going to sign in to the store/coffee shop/other's network on the pi with no access to the PI until after you've signed in?

Every public hotspot I've used needs authentication via an app or web browser before it'll let you do anything.
Arguing with strangers on the internet since 1993.

User avatar
jojopi
Posts: 3274
Joined: Tue Oct 11, 2011 8:38 pm

Re: SSH to a local Raspberry Pi

Wed Aug 05, 2020 3:04 pm

GlowInTheDark wrote:
Tue Aug 04, 2020 11:45 am
where usb0 is the device (obviously) and X and Y are any two numbers (between 10 and 250) you like.
Why not between 0 and 255 ?

User avatar
rpdom
Posts: 17275
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: SSH to a local Raspberry Pi

Wed Aug 05, 2020 4:27 pm

jojopi wrote:
Wed Aug 05, 2020 3:04 pm
GlowInTheDark wrote:
Tue Aug 04, 2020 11:45 am
where usb0 is the device (obviously) and X and Y are any two numbers (between 10 and 250) you like.
Why not between 0 and 255 ?
No reason I can think of.

But you can't use X=0, Y=0 or X=255, Y=255 as 169.254.0.0 is reserved for the subnet number and 169.254.255.255 is the broadcast address.
Unreadable squiggle

GlowInTheDark
Posts: 744
Joined: Sat Nov 09, 2019 12:14 pm

Re: SSH to a local Raspberry Pi

Wed Aug 05, 2020 4:51 pm

But you can't use X=0, Y=0 or X=255, Y=255 as 169.254.0.0 is reserved for the subnet number and 169.254.255.255 is the broadcast address.
Right. I thought it was pretty obvious that various numbers at both the top and bottom of the range are (or might be at some future time) reserved for whatever reasons. I just didn't want to have to get into that, so I picked a safe subset.

But I knew somebody would nitpick it. They did not disappoint.
GitD's list of things that are not ready for prime time:
1) IPv6
2) 64 bit OSes
3) USB 3
4) Bluetooth

User avatar
mahjongg
Forum Moderator
Forum Moderator
Posts: 13142
Joined: Sun Mar 11, 2012 12:19 am
Location: South Holland, The Netherlands

Re: SSH to a local Raspberry Pi

Thu Aug 06, 2020 12:05 am

@GlowInTheDark you are getting tedious.... :twisted:
Ill keep an eye on you..

everyone, don't do personal attacks..... :roll:

LogicalUnit
Posts: 14
Joined: Thu Jan 03, 2013 9:01 am

Re: SSH to a local Raspberry Pi

Fri Aug 07, 2020 11:45 pm

Hi everyone, thanks for the suggestions. I have downloaded nmap and used it to perform a scan. However, it's taking a very long time and so far hasn't found anything. Am I using the right command?
Attachments
nmap_screenshot.png
nmap_screenshot.png (176.56 KiB) Viewed 590 times

User avatar
thagrol
Posts: 3178
Joined: Fri Jan 13, 2012 4:41 pm
Location: Darkest Somerset, UK
Contact: Website

Re: SSH to a local Raspberry Pi

Sat Aug 08, 2020 10:50 am

It's taking a long time as you've asked to scan a block of roughly 65,000 addresses.

Either wait for it to finish or find another way such as connecting a screen to the Pi and watching the boot messages. Or use a USB to 3.3v serial dongle and log in over that.
Arguing with strangers on the internet since 1993.

LogicalUnit
Posts: 14
Joined: Thu Jan 03, 2013 9:01 am

Re: SSH to a local Raspberry Pi

Sat Aug 08, 2020 12:06 pm

Hmm, I have tested the RPi on my home network using DHCP, and I can connect to it using raspberrypi.local

I notice in my network adaptors settings that the USB ethernet adaptor connected to the Pi is showing up with a red X next to it and says Unidentified Network. If I go into the status page, IPv4 Connectivity and IPv6 Connectivity both say No network access.

So I think I just need to provide the right settings to the USB ethernet adaptor, and I will be able to connect to the RPi using raspberrypi.local

I have no idea what those settings might be though. Any ideas?

User avatar
DougieLawson
Posts: 39304
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: SSH to a local Raspberry Pi

Sat Aug 08, 2020 12:12 pm

LogicalUnit wrote:
Sat Aug 08, 2020 12:06 pm
Hmm, I have tested the RPi on my home network using DHCP, and I can connect to it using raspberrypi.local
That should still work if you're on a 169.254.xxx.xxx/16 subnet. As long as avahi-daemon started after the IP address was assigned it will publish raspberry.local on mDNS.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

GlowInTheDark
Posts: 744
Joined: Sat Nov 09, 2019 12:14 pm

Re: SSH to a local Raspberry Pi

Sat Aug 08, 2020 12:26 pm

If that works, and meets your needs, then fine.

All I know is that in my configuration, it worked out better all around to give it a known, fixed (169.254) IP address. It is certainly easy enough to do using rc.local - and note that you don't even need any of the theatrics associated with hooking up a keyboard/monitor/etc as long as you have another Linux machine available (so that you can get in and edit stuff on the ext4 partition).

As I've said many times, all of the theatrics that we so love to discuss on these boards, really only comes about in the rare case where all the user has is Windows. Hopefully, that will soon be an ancient relic, not worthy of further discussion.
GitD's list of things that are not ready for prime time:
1) IPv6
2) 64 bit OSes
3) USB 3
4) Bluetooth

User avatar
thagrol
Posts: 3178
Joined: Fri Jan 13, 2012 4:41 pm
Location: Darkest Somerset, UK
Contact: Website

Re: SSH to a local Raspberry Pi

Sat Aug 08, 2020 12:37 pm

GlowInTheDark wrote:
Sat Aug 08, 2020 12:26 pm
If that works, and meets your needs, then fine.

All I know is that in my configuration, it worked out better all around to give it a known, fixed (169.254) IP address. It is certainly easy enough to do using rc.local - and note that you don't even need any of the theatrics associated with hooking up a keyboard/monitor/etc as long as you have another Linux machine available (so that you can get in and edit stuff on the ext4 partition).
Still "theatrics" needed as you need to shutdown the Pi, remove the SD card, etc in order to do that. Personally I've found the best tool for troubleshooting USB gadget issues is a serial link.
As I've said many times, all of the theatrics that we so love to discuss on these boards, really only comes about in the rare case where all the user has is Windows.
That's a very strange conclusion. Windows is, unfortunately, the dominant OS. It's far more likely the user of a Pi will have windows on their other computer than anything else.

Like linux, windows is a tool. While neither is the only tool, for many Windows is the right one.
Hopefully, that will soon be an ancient relic, not worthy of further discussion.
Do I detect an anti windows bias?
Arguing with strangers on the internet since 1993.

jbudd
Posts: 1446
Joined: Mon Dec 16, 2013 10:23 am

Re: SSH to a local Raspberry Pi

Sat Aug 08, 2020 2:29 pm

As hippy says:
hippy wrote:
Tue Aug 04, 2020 11:28 am
Without any DHCP provider your laptop is getting a local link address. Not sure what the Pi would get. The easiest solution would be to set your laptop and Pi to have static IP addresses in the same network range; eg 192.168.123.1 on the laptop, 192.168.123.2 on the Pi.

Does that not work for you?

I wrote a script that flashes the IP address on the Pi's LED. viewtopic.php?f=63&t=279419
Unfortunately it may not work for your old model of Pi.

LogicalUnit
Posts: 14
Joined: Thu Jan 03, 2013 9:01 am

Re: SSH to a local Raspberry Pi

Sun Aug 09, 2020 1:31 am

Okay, I have done some more testing, and the raspberrypi.local address works just fine with my RPi3. It resolves to an IPv6 address which I can ping and SSH into. For some reason, it doesn't work with my RPi1, even though the USB ethernet adaptor has the same address of 169.254.119.14. The other adaptor settings look the same too.

If anyone has some insight into why the older RPi1 doesn't work I would appreciate it, but I think I may just buy another RPi3.

User avatar
DougieLawson
Posts: 39304
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: SSH to a local Raspberry Pi

Sun Aug 09, 2020 9:31 am

LogicalUnit wrote:
Sun Aug 09, 2020 1:31 am

If anyone has some insight into why the older RPi1 doesn't work I would appreciate it, but I think I may just buy another RPi3.
Restart avahi-daemon on your RPi1 and it will look again for the IP addresses it needs to publish.
Use avahi-browse to see what's happening on your whole subnet.
Use avahi-resolve -4 --name raspberrypi.local; avahi-resolve -6 --name raspberrypi.local to see the published addresses for a machine called raspberrypi.

Code: Select all

dougie@apollo:/srv/templates$ avahi-resolve -4 --name pioneer.local; avahi-resolve -6 --name pioneer.local
pioneer.local   192.168.3.14
pioneer.local   2002:pppp:pppp:pppp:20f:13ff:fe40:184f
dougie@apollo:/srv/templates$
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

LogicalUnit
Posts: 14
Joined: Thu Jan 03, 2013 9:01 am

Re: SSH to a local Raspberry Pi

Mon Aug 10, 2020 7:13 am

I've done some more testing, and avahi-browse and avahi-resolve are not recognised commands on my Pi.

To make it clear what's happening: when I connect the RPi1 to my DHCP network, the address raspberrypi.local resolves to it, and I can connect without having to know an IP address. The same thing happens when I do this with my RPi3.

However, when I connect the RPi1 directly to my laptop using a USB ethernet adaptor, raspberrypi.local does not resolve to any address, and I cannot connect to the RPi1. This is surprising to me, because when I connect my RPi3 to the USB ethernet adaptor, the raspberrypi.local does resolve to an address, and I can use it to connect.

So what's the difference between the RPi1 and RPi3? Why does raspberrypi.local correctly resolve to both devices when on DHCP, but only to the RPi3 when using the ethernet adaptor?

I have performed an update and upgrade on the RPi1.
avahi_screenshot.png
avahi_screenshot.png (85.26 KiB) Viewed 283 times

User avatar
DougieLawson
Posts: 39304
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: SSH to a local Raspberry Pi

Mon Aug 10, 2020 12:54 pm

sudo apt install avahi-utils
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

LogicalUnit
Posts: 14
Joined: Thu Jan 03, 2013 9:01 am

Re: SSH to a local Raspberry Pi

Fri Aug 14, 2020 3:04 am

Okay, I have solved it. All I needed to do was tell the USB ethernet adaptor to use the following address:

192.168.137.1
255.255.255.0

And once that is done, the raspberrypi.local will resolve to the address of the locally-connected Pi.

If you do not enable internet connection sharing with the USB ethernet adaptor interface, the Pi will have an address in the 169.254.x.x range and will have no internet access. If you do enable internet connection sharing, it will have an address in the range 192.168.137.x and you will be able to use the internet.

I figured this out by watching the following demo: https://youtu.be/AJ7skYS5bjI

LogicalUnit
Posts: 14
Joined: Thu Jan 03, 2013 9:01 am

Re: SSH to a local Raspberry Pi

Fri Aug 14, 2020 3:17 am

DougieLawson wrote:
Mon Aug 10, 2020 12:54 pm
sudo apt install avahi-utils
Can you please tell me how to disable this on an RPi that I have configured with a static IP, and no longer want raspberrypi.local to resolve to its address?

Return to “Beginners”