We have already seen how you can set up your raspberry pi and access it either using HDMI or headlessly using VNC and SSH. In this article, we will look at setting up a static IP address in a Raspberry pi.
When you connect to a wireless network, the router is responsible for assigning an IP address to the raspberry pi and all the other devices on the network. The router does so, by using the DHCP server running on it. The router will assign a unique IP address from the available pool of addresses. In most cases for home networks, the assigned IP address would be 192.168.0/1/2.xxx
Once an IP address is assigned to your Raspberry PI or any other device, your Rpi will retain the same IP address as long as it doesn’t remain disconnected from the network for a period longer than the DHCP lease time. The lease time though is configurable up to a certain time through the router. If you do, however, stay disconnected, your Raspberry Pi will be assigned a new IP address the next time it connects to the same network. This is where a static IP address can come handy.
Static IP address.
Static IP addresses as the name suggest, do not change. It has its own Pros and cons. We won’t be going into those comparisons. But as far as Raspberry PI is concerned, static IP will be crucial if you have multiple products build around raspberry PI and have been deployed on-site. This will allow you to identify problems and log data remotely without having to worry about the changing IP address. You can also use a static IP if you intend to use your raspberry pi as a server.
You can configure your static IP address either from the router or through raspberry pi itself. If you plan on setting it up through your Router, you will have to visit the IP address of your router which you will find written on the router itself. You will then need to log in with the credentials and look for DHCP reservations(name might differ for different routers) where you can add static IP addresses for different devices.
Setting up Static IP in Raspberry Pi
Static IP address to a Raspberry Pi can be assigned in case of both, wired and wireless networks. To do so, you will have to modify the dhcpcd.config
file.
Open your raspberry pi terminal and type sudo nano /etc/dhcpcd.config
Scroll down all the way to the bottom and add the following properties.
interface wlan0
static ip_address=192.168.0.80/24
static routers=192.168.0.1
static domain_name_servers=192.168.0.1
Note: The static router and domain_name_server might change in your case. So ensure that you put the correct information based on your router.
Here the ‘static IP address’ is the IP address that you wish to assign to your raspberry pi. The number after the ‘/’ is CIDR formatting. It indicates the number of possible IP assignments. A 24 indicates that there are 8 bits available for assigning IP to your device (192.168.0.0-255). You can check your existing IP address along with the value after ‘/’ by typing ip addr show eth0
for ethernet and ip addr show wlan0
for the wireless network.
‘Static routers’ is the LAN IP of your Router. The last is the ‘domain name server’ which is generally the same as the Router IP address for home networks.
After adding these lines, save the file and restart your raspberry PI. You can then use your assigned IP address to access your PI either through VNC or SSH. The same idea can be extended to eth0, with interface wlan0
replacing interface eth0
and then putting the appropriate fields for the same.
Connecting To different Wireless network
Another thing that I’d just like to add, is connecting to different wireless networks based on availability. This is useful when you want your raspberry pi to automatically connect to a network. You can add all the available networks with their SSID and Password along with priority.
Open the wpa_supplicant.conf file by typing
sudo nano /etc/wpa_supplicant/wpa_supplicant.conf
In this file, you can add other available networks.
network={
ssid="your ssid"
psk="network password"
key_mgmt="encrypition"
priority=1
}
network={
ssid="your ssid"
psk="network password"
key_mgmt="encrypition"
priority=2
The Raspberry Pi will automatically connect to the network with the highest priority in descending order. The network with priority 2 will be given preference over priority 1. This was a brief about setting up static IP and wireless networks. If you have any difficulties executing any of the above steps, let us know.
0 Comments