Setting A Static IP Address On Raspberry Pi Zero Dongle
The Raspberry Pi Zero Dongle project is a great way to work or play with the Zero. I use it for developing and testing websites from my PC without sharing resources to a virtual machine or needing to install a webserver and additional services to my laptop which again saves resources. For this I need to be able to use the same IP Address each time I access the device. Since the internet is being shared from my computer to the device, my computer is “issuing” a dhcp lease and the IP Address to the Zero. Every time the Zero boots I end up with a new 192.168.137.xx IP address.
Of course I could just use raspberrypi.local to connect to the device with a browser but I find that connecting to it using it’s IP address for viewing hosted sites is marginally faster than using the .local address. You can set or change the IP address for the usb0 interface of the Raspberry Pi Zero using the following command. As a note, you may loose connection with the device if you are connected via SSH as the IP address changes.
sudo ifconfig usb0 192.168.137.100
In this example I set my IP address to 192.168.137.100. You can view your current IP address by running ifconfg to view it.
Automate The Process
There are many ways to set a static IP in Linux but for this project I wanted to use a script. I created a Bash script in a folder I called scripts in my home folder. Create a file called “ipaddress” and then add the script.
sudo nano ipaddress
#!/bin/bash ifconfig usb0 192.168.137.100
Set the permissions.
sudo chmod 777 ipaddress
Now add the script to your rc.local file which executes commands or scripts at startup.
sudo nano /etc/rc.local
Add the following line. Make sure to change the path to the location of your script “ipaddress.”
Hers is an example of mine.
That’s it. Now when you boot your Raspberry Pi Zero, it will update it’s IP address to one that you choose. With this method, you won’t need to worry about or understand a subnet, dns or network interfaces in general. Let me know if you have any questions or suggestions on how to improve the setup in the comments section.