Difference between revisions of "Raspberry Pi SSH Without Logging In"

From TheBeard Science Project Wiki
Jump to: navigation, search
(5 intermediate revisions by the same user not shown)
Line 2: Line 2:
  
 
Image the SD card:
 
Image the SD card:
<source>
+
<source lang="shell">
 
sudo dd if=imagefile.img of=/dev/yourdevice conv=notrunc,noerror ; sync
 
sudo dd if=imagefile.img of=/dev/yourdevice conv=notrunc,noerror ; sync
 
</source>
 
</source>
Line 23: Line 23:
  
 
Then mount the primary partition of the SD card and run this in the terminal (assuming the SD card is mounted at <code>/media/user/uuid</code>):
 
Then mount the primary partition of the SD card and run this in the terminal (assuming the SD card is mounted at <code>/media/user/uuid</code>):
<source>
+
<source lang="shell">
 
cd /media/user/uuid/etc/ssh
 
cd /media/user/uuid/etc/ssh
 
sudo rm ssh_host_*
 
sudo rm ssh_host_*
Line 29: Line 29:
  
 
Now run these commands to set up <code>dpkg-reconfigure</code> to run on startup:
 
Now run these commands to set up <code>dpkg-reconfigure</code> to run on startup:
<source>
+
<source lang="shell">
 +
# Should create a line above "exit 0" like:
 +
#  dpkg-reconfigure openssh-server
 +
cd /media/user/uuid/etc
 +
sudo sed -i 's/^exit\ 0/dpkg-reconfigure openssh-server\nexit\ 0/' rc.local
 +
</source>
 +
 
 +
You can remove that line later, or you can make it remove itself:
 +
<source lang="shell">
 +
# Should create a line above "exit 0" like:
 +
#  dpkg-reconfigure openssh-server && sed -i '/^dpkg.*/d' /etc/rc.local &
 
cd /media/user/uuid/etc
 
cd /media/user/uuid/etc
sudo sed -i 's/exit\ 0/dpkg-reconfigure openssh-server\nexit\ 0/' rc.local
+
sed -i 's/^exit\ 0/dpkg-reconfigure openssh-server\ \&\&\ sed\ \-i\ '\''\/^dpkg\.\*\/d'\''\ \/etc\/rc\.local\ \&\nexit\ 0/' rc.local
 
</source>
 
</source>
  
 
Remove the SD card and put it into the Raspberry Pi. When you start up the Pi, you should be able to SSH into it immediately after everything runs (and once you figure out what the IP address is).
 
Remove the SD card and put it into the Raspberry Pi. When you start up the Pi, you should be able to SSH into it immediately after everything runs (and once you figure out what the IP address is).
  
Remember, it's easy to figure out the IP using:
+
Remember, if you don't have a monitor it's easy to figure out the IP using:
<source>
+
<source lang="shell">
 
sudo nmap -sn 192.168.0.0/24 | grep -A1 Raspberry
 
sudo nmap -sn 192.168.0.0/24 | grep -A1 Raspberry
 
</source>
 
</source>
  
 
Once you've logged into the Pi, you can remove the <code>dpkg-reconfigure</code> line in rc.local:
 
Once you've logged into the Pi, you can remove the <code>dpkg-reconfigure</code> line in rc.local:
<source>
+
<source lang="shell">
 
cd /etc
 
cd /etc
 
sudo sed -i 's/dpkg.*//' rc.local
 
sudo sed -i 's/dpkg.*//' rc.local

Revision as of 01:12, 13 June 2019

I'm very annoyed that I can't just throw a Raspbian image onto an SD card and SSH into it without pulling out an HDMI monitor and keyboard. I finally have all the steps necessary to get it done. These steps were done on Raspbian Jessie 2017-07-05.

Image the SD card:

sudo dd if=imagefile.img of=/dev/yourdevice conv=notrunc,noerror ; sync

Now mount the "boot" partition of the SD card (you could just remove the SD card and insert it again).

In the boot partition, create an empty file called ssh (with no file extension).

(Optional WiFi) Create a file in the boot partition called wpa_supplicant.conf and enter this content:

country=US
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
network={
        ssid="YOUR_SSID"
        psk="YOUR_PASSWORD"
        key_mgmt=WPA-PSK
}

Then mount the primary partition of the SD card and run this in the terminal (assuming the SD card is mounted at /media/user/uuid):

cd /media/user/uuid/etc/ssh
sudo rm ssh_host_*

Now run these commands to set up dpkg-reconfigure to run on startup:

# Should create a line above "exit 0" like:
#   dpkg-reconfigure openssh-server
cd /media/user/uuid/etc
sudo sed -i 's/^exit\ 0/dpkg-reconfigure openssh-server\nexit\ 0/' rc.local

You can remove that line later, or you can make it remove itself:

# Should create a line above "exit 0" like:
#   dpkg-reconfigure openssh-server && sed -i '/^dpkg.*/d' /etc/rc.local &
cd /media/user/uuid/etc
sed -i 's/^exit\ 0/dpkg-reconfigure openssh-server\ \&\&\ sed\ \-i\ '\''\/^dpkg\.\*\/d'\''\ \/etc\/rc\.local\ \&\nexit\ 0/' rc.local

Remove the SD card and put it into the Raspberry Pi. When you start up the Pi, you should be able to SSH into it immediately after everything runs (and once you figure out what the IP address is).

Remember, if you don't have a monitor it's easy to figure out the IP using:

sudo nmap -sn 192.168.0.0/24 | grep -A1 Raspberry

Once you've logged into the Pi, you can remove the dpkg-reconfigure line in rc.local:

cd /etc
sudo sed -i 's/dpkg.*//' rc.local