448
Views

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.

ifconfig

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.”

./home/pi/scripts/ipaddress

Hers is an example of mine.

rc-local-startup-scripts

Conclusion

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.

Article Tags:
·
Article Categories:
Tech
SDATIC

Web developer and former photographer by trade...gamer and all around tech enthusiast in his free time. Christoph started sdatic.com as a way to organize his ideas, research, notes and interests. He also found it enjoyable to connect and share with others who parallel his interests. Everything you see here is a resource that he found useful or interesting and he hopes that you will too.

Leave a Reply

Your email address will not be published. Required fields are marked *