Reviving an Old Raspberry Pi: Setting a Static IP and Hostname
Reviving an Old Raspberry Pi: Setting a Static IP and Hostname
I recently powered on my old Raspberry Pi 3 Model B that had been sitting unused for years. I used that to build a small robot that could move around. It was a fun project in 2019.
Anyways, I thought of doing something fun with it again.
Before doing anything else, I wanted to do two things:
- Assign a static IP address.
- Give it a meaningful hostname.
Just because I would be able to manage and program the device more easily from my Mac.
Here are the steps I followed.
After the boot, I logged in. I did not remember the password however, I may have set it to auto-login. Lucky me.
I need to set a reminder to disable that later :D
Since I could see the desktop, I just connected to my new Wi-Fi network and opened a terminal to see the current IP address.
$ hostname -I
192.168.1.3
Determine the default gateway:
$ ip route
default via 192.168.1.1 dev wlan0 src 192.168.1.3 metric 303
192.168.1.0/24 dev wlan0 proto dhcp scope link src 192.168.1.3 metric 303
Reusing the same IP would avoid any potential conflicts with other devices on the network. So I finally have -
- Router IP:
192.168.1.1 - Desired Static IP:
192.168.1.3
Once I have the desired IP address, I just edited the DHCP client config.
$ sudo vi /etc/dhcpcd.conf
To set a static IP address, I added the following lines at the end of the file:
interface wlan0
static ip_address=192.168.1.3/24
static routers=192.168.1.1
static domain_name_servers=1.1.1.1 8.8.8.8
..and just rebooted the device:
$ sudo reboot
After the reboot, I logged in again and verified the IP address:
$ hostname -I
192.168.1.3
It was the same as before, but now it is static and won't change on the next reboot.
Next, I wanted to change the hostname to something more meaningful. Since I would be doing something related to my agent mav, I decided to name it mav-pi.
$ sudo hostnamectl set-hostname mav-pi
Also, I needed to update the /etc/hosts file to reflect the new hostname:
$ sudo vi /etc/hosts
I just needed to add the following line at the end of the file:
127.0.1.1 mav-pi
That's it. Just rebooted the device again and it worked fine.
From my mac, I could ping the device using the new hostname:
❯ ping mav-pi.local
PING mav-pi.local (192.168.1.3): 56 data bytes
64 bytes from 192.168.1.3: icmp_seq=0 ttl=64 time=137.579 ms
64 bytes from 192.168.1.3: icmp_seq=1 ttl=64 time=56.285 ms
--- mav-pi.local ping statistics ---
2 packets transmitted, 2 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 56.285/96.932/137.579/40.647 ms
It worked!