How to setup a headless Raspberry Pi
In this post we’ll go through the step-by-step process of setting up a headless Raspberry Pi. The best part is, you can do all of this from a secondary computer, so you have no need for a monitor or keyboard and mouse!
1. Purchase a Raspberry Pi, power supply, and micro SD card.
This probably seems pretty obvious, but make sure you get a micro SD card, with a minimum of 16gb of storage. Don’t forget the power supply like I did, because those can be hard to find later on.
You can get all of these things on Amazon, or get them in all together in a kit (also from Amazon).
2. Download a Raspian system image from raspberrypi.org.
You don’t have to use Raspian, but I like to stick with this – especially as I’m getting started.
3. Use etcher to copy the system image to your SD card.
Etcher makes it super easy to copy the system image to your SD card.
4. Create an empty ssh file in the root directory
While still on your computer (not the raspberry pi), cd into the root directory of the SD card and use: touch ssh
. This will generate an ssh file, which will allow you to ssh into your raspberry pi.
5. Generate a wpa_supplicant.conf
file
While still in the root directory, create a wpa_supplicant.conf file with: touch wpa_supplicant.conf
. Then use: nano wpa_supplicant.conf
. While in this file, create a network object like so:
network={
ssid="testing"
psk="testingPassword"
}
where ssid
is the name of your network, and psk
is your password.
** Extra credit: use a passphrase to encrypt your password. **
6. Insert the SD card into the pi and plug it in.
You’re ready to go!
You’re now ready to ssh into your raspberry pi from another computer and use your pi via the command line. No monitor, keyboard or mouse required!
** Extra credit: Change the hostname of Raspberry Pi **
** Extra credit: Set up a shortcut to easily ssh into your pi. **
How to encrypt your wifi password on a Raspberry Pi
If you are using a headless setup for your Raspberry Pi, you can easily set up a wpa_supplicant.conf
file to connect to wifi. But it is very important to mask your password for security reasons. In this post, I will show you how.
Your wpa_supplicant.conf
file has network objects that look like this:
network={
ssid="testNetwork"
psk="testingPassword"
}
Where ssid
is your network name and psk
is your password. But you’ll notice that that is just a plain text password, which we all know is a no-no.
Use this command: wpa_passphrase [ssid-name] [password-name]
and be sure to wrap the network and/or password in quotes if you have spaces in either.
This command will spit out a new network object that looks like this:
network={
ssid="testNetwork"
#psk="testingPassword"
psk=575827beef2c7dbdcf817a9cd0e6b96fb0fd3f54e2c0fbf24a38eb04fb7e9aa3
}
Copy this and use sudo nano /etc/wpa_supplicant/wpa_supplicant.conf
and add this new network object to the file, or edit the old network object with the new passphrase. Just be sure to delete the commented-out plain text password from the file before you save it.
You can use the iwgetid
command to confirm which network you are connected to. And you’re good to go!
How to change the hostname on a Raspberry Pi
If you are using ssh shortcuts for your Raspberry Pi, it can be a good idea to give each pi you set up a unique hostname. If you are using the raspian distribution from raspberrypi.org, raspberrypi.local
is the default hostname.
Since IP addresses can change, it’s not good to use this in your ssh config file. So instead, I’ll show you how to change the hostname to be unique on each different pi.
First, while ssh’d into the pi, use: the hostname
command. This will show you what the current hostname of the pi is.
Next use sudo nano /etc/hosts
and change the hostname in that file. (It’s listed next to the IP address.)
Then use sudo nano /etc/hostname
and change the hostname in this file.
Lastly, use reboot
to restart the pi and ssh back in using the new hostname and voila!
How to setup a shortcut to ssh into your Raspberry Pi
If you are ssh-ing into your Raspberry Pi, it can be a real pain to find and remember the IP address for the machine. This shortcut makes it easy, because you no longer need to remember!
On the computer you use to ssh into the pi, use: nano ~/.ssh/config
to open the config file in your ssh directory. (If you don’t have one of these use touch ~/.ssh/config
to create one, and then use nano
to edit it.)
Use the following:
Host myPi2
Hostname 192.168.2.45
User pi
You can use either the hostname of the Pi or the IP address for the hostname (use: hostname
in the terminal of your Pi to find the hostname or PiFinder to find the IP address).
You’ll use whatever username you usually use to log in for User
.
You can swap myPi2
with whatever shortcut you’d like to use to ssh in. For the above example, I would use: ssh myPi2
to gain access to the pi, and I would then be prompted for my password.
Start writing C# apps on a Raspberry Pi in 5 minutes or less
I decided to play around with running C# on a Raspberry Pi for a variety of reasons. First and foremost: I’ve been wanting to branch out into another language for a while. JavaScript is great, and it will always be my true love. But I have been feeling pretty limited lately about what I can accomplish knowing only one language. And I’ve always heard that learning the second language is much easier than learning the first.
But why C#? The honest reason is that C# is really popular around OKC. Lots of companies use it, and in an effort to be competitive, I figured I would pick it up too. Plus I know a lot of people who know C# whom I could ask for help.
But once I selected C#, a problem arose. I use a Mac, and that doesn’t jive with C#. There were some solutions I found including getting Parallels and installing Windows on a VM and writing the C# in my Windows VM. But even typing that sentence just now made my head spin. I was looking for something simple that would allow me to lean on what I already know as much as possible.
Enter: My Raspberry Pi.
I was able to set up my Raspberry Pi to run C# apps without much trouble. This solved a few problems. I’m able to remote into my Pi from my Mac, and I didn’t have to install anything on my Mac. I wasn’t too worried about breaking my Raspberry Pi, because it was only $35. It would be far easier to replace than my Mac, and if anything were corrupted it would likely be as simple as replacing the SD card. Easy-peasy. Also the set up to be able to run C# apps was incredibly easy. I joke around about things taking 5 minutes, but this literally took less than 5 minutes.
** I should note here that this process assumes you are already able to ssh into your Raspberry Pi. Or learn how to set up a pi from scratch. **
1. ssh into your Raspberry Pi using the terminal
Pretty easy: ssh username@pi
and then enter your password.
2. Install Mono
Mono is an implementation of the .NET framework that can be installed on Linux. Since the Pi is running a flavor of Linux, this is what we’ll use to run our apps.
sudo apt-get update
sudo apt-get install mono-complete
And that’s all you need!
3. Get a text editor && write your app!
Usually I prefer to use an IDE, but since I was writing a super simple Hello World
app, I didn’t think I needed one. I chose nano, but you can use whichever terminal text editor you like. I just liked nano because it comes with bash, which is already on the Pi. Also, a side note: writing your app might take more than 5 minutes. Don’t @ me.
Use this command to create and edit a file: nano yourFileName.cs
.
If you aren’t familiar with nano, it can be a little confusing. The thing I always fumble for is saving my work and closing. This is done with cmd-X
. You’ll get a prompt to save and get kicked back into the directory the file lives in. If you want to see the contents of a file, you can use: cat yourFileName.cs
.
** Remember to use the .cs
extension since we’re writing C# apps! **
4. Compile to .exe
This is where Mono comes in.
Use this command: mcs yourFileName.cs
That will create: yourFileName.exe
in the same directory. Pretty cool, right?
5. Run it!
To run the program use this command: mono yourFileName.exe
. Just make sure you target the .exe
and not the .cs
file.
Extra credit:
Let’s pretend for a moment that you don’t like writing apps in the terminal. (It’s ok, I don’t either!) There must be a way to write the app in an IDE and then run it, right? You’re in luck!
There are two options. First, you could simply plug your Pi into a monitor and download Visual Studio Code. VS Code is compatible with Linux (at least, according to the VS Code website) and you can write C# with it. This keeps you in the Pi 100% of the time.
However, I was looking for another solution. I’m very comfortable writing code on my Mac and wanted to stay there. Solution: Use VS Code for C# apps, and transfer the files to the Pi via scp
. Scp comes included with bash, and is very easy to use.
scp /path/to/your/local/file remoteUser@some_address:/home/remoteUser/Documents
And there you have it! You’re ready to write C# apps and run them on a Raspberry Pi. Happy hacking! Be sure to let me know what cool projects you come up with!