Raspberry pi notes: Difference between revisions

From AD7ZJ Wiki
Jump to navigation Jump to search
(Created page with "The raspberry pi is really an awesome little board. I pre-ordered one when they were first announced and although it took several months to actually ship, it was worth the wa...")
 
No edit summary
 
Line 26: Line 26:


But as long as your partition(s) were shrunk so the total is less than 4 gigs, the fact that the entire image wasn't copied shouldn't be a problem.  Or at least, it wasn't for me :-)
But as long as your partition(s) were shrunk so the total is less than 4 gigs, the fact that the entire image wasn't copied shouldn't be a problem.  Or at least, it wasn't for me :-)
==SSH behind NAT or mobile network==
Sometimes you can't forward ports through a NAT or the PI is online via a mobile network. How can you access SSH remotely? In this case, the answer is "A remote SSH tunnel". Upon bootup, the pi has to reach out to a 3rd party server you have ssh access to, then open an ssh tunnel. When you want to access that pi, you ssh into that 3rd party server, and then from there you can go backwards through the tunnel to the pi. I use these two scripts.
This goes on the pi, and crontab calls it every 5 or 10 minutes. Setup a user on the 3rd party ssh server and authenticate with pre-shared keys, no passphrase so it can login automatically. This should be a non-privileged account just to be extra safe...
<nowiki>
if ps -aux | grep -v grep | grep user@3rdpartysshserver.com
then
    echo "Process is running."
else
    echo "Process is not running, restarting..."
    ssh -t -N -R 10000:localhost:22 user@3rdpartysshserver.com -o ExitOnForwardFailure=yes -o ServerAliveInterval=60 &
fi
</nowiki>
Then on the 3rd party server, I use this script to login to the pi, through the tunnel:
<nowiki>
ssh pi@localhost -p 10000
</nowiki>
Obviously for this to work, you need ssh access to a server somewhere, that's accessible from the outside world. There are some services out there that do this, such as sshreach.me. But if you already have a server, this method works good.

Latest revision as of 19:48, 26 November 2021

The raspberry pi is really an awesome little board. I pre-ordered one when they were first announced and although it took several months to actually ship, it was worth the wait. They really have a lot of uses and I have based several of my recent projects around them.

Moving to a smaller SD card

This is just some quick documentation of how to clone the image from a larger SD card onto a smaller SD card. Now obviously this won't work if the card is actually full of data, but in my case I had an 8 gig card I wanted to reclaim use of since the application I'm using didn't need near that much space. I wanted to put it on a 4 gig card....

So first step is use gparted or something similar to shrink the partition down to a little less than 4 gig. Once it's on the other card, you can always expand it so lean towards the small side.

Then, use dd to copy the large card's image to your local hard disk.

dd if=/dev/mmcblk0 of=sd.img

That image should wind up being about the size of your large card - in my case 8 GB. You could also use the 'bs' and 'count' switches to have dd only copy the first 4 GB since that's all we'll actually make use of.

Now put the smaller card in your card reader and use dd to write the image onto it.

dd if=sd.img of=/dev/mmcblk0
[sudo] password for elijah: 
dd: writing to `/dev/mmcblk0': No space left on device
7761921+0 records in
7761920+0 records out
3974103040 bytes (4.0 GB) copied, 1962.22 s, 2.0 MB/s

But as long as your partition(s) were shrunk so the total is less than 4 gigs, the fact that the entire image wasn't copied shouldn't be a problem. Or at least, it wasn't for me :-)


SSH behind NAT or mobile network

Sometimes you can't forward ports through a NAT or the PI is online via a mobile network. How can you access SSH remotely? In this case, the answer is "A remote SSH tunnel". Upon bootup, the pi has to reach out to a 3rd party server you have ssh access to, then open an ssh tunnel. When you want to access that pi, you ssh into that 3rd party server, and then from there you can go backwards through the tunnel to the pi. I use these two scripts.

This goes on the pi, and crontab calls it every 5 or 10 minutes. Setup a user on the 3rd party ssh server and authenticate with pre-shared keys, no passphrase so it can login automatically. This should be a non-privileged account just to be extra safe...

if ps -aux | grep -v grep | grep user@3rdpartysshserver.com 
then
    echo "Process is running."
else
    echo "Process is not running, restarting..."
    ssh -t -N -R 10000:localhost:22 user@3rdpartysshserver.com -o ExitOnForwardFailure=yes -o ServerAliveInterval=60 &
fi

Then on the 3rd party server, I use this script to login to the pi, through the tunnel:

ssh pi@localhost -p 10000

Obviously for this to work, you need ssh access to a server somewhere, that's accessible from the outside world. There are some services out there that do this, such as sshreach.me. But if you already have a server, this method works good.