{"id":111,"date":"2020-04-04T18:12:14","date_gmt":"2020-04-04T16:12:14","guid":{"rendered":"https:\/\/mic.st\/blog\/?p=111"},"modified":"2023-07-07T19:54:12","modified_gmt":"2023-07-07T17:54:12","slug":"minimal-setup-of-raspberry-pi-zero-w-with-ssh-over-wifi-connection","status":"publish","type":"post","link":"https:\/\/mic.st\/blog\/minimal-setup-of-raspberry-pi-zero-w-with-ssh-over-wifi-connection\/","title":{"rendered":"Minimal setup of Raspberry Pi Zero W with ssh over wifi connection."},"content":{"rendered":"\n<p><strong>Update July, 2023:<\/strong><\/p>\n\n\n\n<p>Nowadays, I would recommend using the &#8220;Raspberry\u00a0Pi\u00a0Imager&#8221;. You can download it here: <a href=\"https:\/\/www.raspberrypi.com\/software\/\" target=\"_blank\" rel=\"noopener\">https:\/\/www.raspberrypi.com\/software\/<\/a>. It&#8217;s much faster than doing all the manual work. Just make sure that you select as OS the minimal version without any desktop.<\/p>\n\n\n\n<p>The tool can download the image itself, format your SD-card and install it correctly on it. Also it has options for presetting the wifi connection and other stuff.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What is special about this minimal setup?<\/h2>\n\n\n\n<p>Basically, you could just install any of the preconfigured Linux distribution available. However, these often include features you do not necessarily need and also setup is often dependent on input devices.<\/p>\n\n\n\n<p>When following this instruction, you do not need to connect any input devices for setting up your Raspberry Pi. You just need a wifi connection as you will set up everything over ssh. All kinds of projects can be done with this setup as most of them do not require any external input devices or HDMI monitors.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Let&#8217;s get started!<\/h2>\n\n\n\n<p>Download Raspbian Lite Image:<br><a href=\"https:\/\/www.raspberrypi.org\/downloads\/raspbian\/\" target=\"_blank\" rel=\"noopener\">https:\/\/www.raspberrypi.org\/downloads\/raspbian\/<\/a><\/p>\n\n\n\n<p>Find your SD-card with:<br><code>diskutil list<\/code><\/p>\n\n\n\n<p>Unmount SD-card with:<br><code>diskutil unmountDisk \/dev\/diskX<\/code> (<em>X<\/em> is the number of your SD-card you found out in the step before)<\/p>\n\n\n\n<p>Copy the image to your SD-card with:<br><code>sudo dd bs=1m if=theImagePath.img of=\/dev\/rdiskX conv=sync<\/code> <br><strong>Please make absolutely sure, that the number (&#8220;X&#8221;) belongs to your SD-card!<\/strong><\/p>\n\n\n\n<p>To connect to your wifi network, add file called <code>wpa_supplicant.conf<\/code> to partition &#8220;boot&#8221; (e.g. via finder), with the following content:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">ctrl_interface=DIR=<span class=\"hljs-regexp\">\/var\/<\/span>run\/wpa_supplicant GROUP=netdev\nupdate_config=<span class=\"hljs-number\">1<\/span>\ncountry=DE\n\nnetwork={\n  ssid=<span class=\"hljs-string\">\"YOUR WIFI'S SSID\"<\/span>\n  psk=<span class=\"hljs-string\">\"YOUR WIFI PASSWORD\"<\/span>\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Add an empty file called <code>ssh<\/code> without any content to the same partition to enable ssh.<\/p>\n\n\n\n<p>Navigate via terminal to the boot partition on your SD-card:<\/p>\n\n\n\n<p><code>cd \/Volumes\/boot<\/code><\/p>\n\n\n\n<p>Add an empty file called <code>ssh<\/code>:<\/p>\n\n\n\n<p><code>touch ssh<\/code><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Insert SD and boot up your RPi<\/h2>\n\n\n\n<p>Insert the SD-card into your Raspberry pi, connect a power source and wait for it to connect to your wifi. This takes about 30 seconds. You should see your raspberry pi in your wifi routers web interface. Its hostname should be <code>raspberrypi<\/code><\/p>\n\n\n\n<p>If your Raspberry pi is connected, you should be able to establish an ssh connection. The defautl username is <code>pi<\/code>, the default hostname is <code>raspberrypi<\/code> (alternatively you can use its IP adress if your router does not resolve its hostname) and the default password is <code>raspberrypi<\/code> as well. Go to a terminal and type:<\/p>\n\n\n\n<p><code>pi@raspberrypi<\/code><\/p>\n\n\n\n<p>You are asked for a password, the default one is: <code>raspberrypi<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Troubleshoot ssh connection failure<\/h2>\n\n\n\n<p>If you cannot connect to the raspberry because of some ssh warning like this:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css\">@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n@       <span class=\"hljs-attribute\">WARNING:<\/span> POSSIBLE DNS SPOOFING DETECTED!          @\n@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\nThe ECDSA host key for raspberrypi has changed,\n<span class=\"hljs-keyword\">and<\/span> the key for the corresponding IP address xxxxxxx\nis unknown. This could either mean that\nDNS SPOOFING is happening <span class=\"hljs-keyword\">or<\/span> the IP address for the host\n<span class=\"hljs-keyword\">and<\/span> its host key have changed at the same time.\n@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n@    <span class=\"hljs-attribute\">WARNING:<\/span> REMOTE HOST IDENTIFICATION HAS CHANGED!     @\n@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\nIT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!\nSomeone could be eavesdropping on you right now (man-in-the-middle attack)!\nIt is also possible that a host key has just been changed.\nThe fingerprint for the ECDSA key sent by the remote host is\nSHA256:xxxxxxx\nPlease contact your system administrator.\nAdd correct host key in \/Users\/user\/.ssh\/known_hosts to get rid of this message.\nOffending ECDSA key in \/Users\/user\/.ssh\/known_<span class=\"hljs-attribute\">hosts:<\/span><span class=\"hljs-number\">37<\/span>\nECDSA host key for raspberrypi has changed <span class=\"hljs-keyword\">and<\/span> you have requested strict checking.\nHost key verification failed.<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>You probably had another Raspberry pi connected via ssh before. Just follow the hint in the error message and edit the file <code>known_hosts<\/code>.<br>You can edit it very easy via the text editor <code>nano<\/code> from within the terminal:<\/p>\n\n\n\n<p><code>nano ~\/.ssh\/known_hosts<\/code><\/p>\n\n\n\n<p>Search for the line saying <code>raspberrypi<\/code>, press CTRL+K (to cut the line), then CTRL+X to quit, press Y accept your changes and finally press ENTER to save. You can make a backup of your existing <code>known_hosts<\/code> file if you save the changed file under another name before overwriting it.<\/p>\n\n\n\n<p>If everything worked fine you should now be able to connect via ssh to your raspberry.<\/p>\n\n\n\n<p>You should see <code>pi@raspberrypi:~ $<\/code> in your terminal, saying that you are logged in as the user <code>pi<\/code> on your Raspberry Pi.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Disable Wifi Powersaving<\/h2>\n\n\n\n<p>At first you should deactivate the wifi power saving of the raspberry. Otherwise the Raspberry would power down the wifi module after some time of inactivity making the Raspberry inaccessible. You deactivate it with:<\/p>\n\n\n\n<p><code>sudo iw wlan0 set power_save off<\/code><\/p>\n\n\n\n<p>In addition to that you have to make sure that the power saving is deactivated whenever the Raspberry is rebooted. For that you have to edit another configuration file:<\/p>\n\n\n\n<p><code>sudo nano \/etc\/network\/interfaces<\/code><\/p>\n\n\n\n<p>Add at the end of the file the following code:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">auto wlan0\niface wlan0 inet dhcp\n    wpa-conf \/etc\/wpa_supplicant\/wpa_supplicant.conf\n    wireless-power off<\/code><\/span><\/pre>\n\n\n<p>Now you are ready to go! Install a webserver, mailserver, connect sensors, etc. \ud83d\ude82<\/p>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity is-style-default\"\/>\n\n\n\n<p>Sources:<br><a href=\"https:\/\/www.heise.de\/select\/ct\/2017\/22\/1508780300482172\" target=\"_blank\" rel=\"noopener\">https:\/\/www.heise.de\/select\/ct\/2017\/22\/1508780300482172<\/a><br><a href=\"https:\/\/www.raspberrypi.org\/documentation\/installation\/installing-images\/mac.md\" target=\"_blank\" rel=\"noopener\">https:\/\/www.raspberrypi.org\/documentation\/installation\/installing-images\/mac.md<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Update July, 2023: Nowadays, I would recommend using the &#8220;Raspberry\u00a0Pi\u00a0Imager&#8221;. You can download it here: https:\/\/www.raspberrypi.com\/software\/. It&#8217;s much faster than doing all the manual work. Just make sure that you select as OS the minimal version without any desktop. The tool can download the image itself, format your SD-card and install it correctly on it.&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[19],"tags":[21,22,23],"class_list":["post-111","post","type-post","status-publish","format-standard","hentry","category-raspberry-pi","tag-raspberry","tag-raspbian-lite","tag-ssh"],"_links":{"self":[{"href":"https:\/\/mic.st\/blog\/wp-json\/wp\/v2\/posts\/111","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mic.st\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mic.st\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mic.st\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mic.st\/blog\/wp-json\/wp\/v2\/comments?post=111"}],"version-history":[{"count":2,"href":"https:\/\/mic.st\/blog\/wp-json\/wp\/v2\/posts\/111\/revisions"}],"predecessor-version":[{"id":791,"href":"https:\/\/mic.st\/blog\/wp-json\/wp\/v2\/posts\/111\/revisions\/791"}],"wp:attachment":[{"href":"https:\/\/mic.st\/blog\/wp-json\/wp\/v2\/media?parent=111"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mic.st\/blog\/wp-json\/wp\/v2\/categories?post=111"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mic.st\/blog\/wp-json\/wp\/v2\/tags?post=111"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}