Its always better to have a fixed IP for your Raspberry Pi within your network so that you no need to worry about the dynamic allocation of IP by your router for Pi which may at times break the headless connection to your Pi due to change of IP address.
You can assign a fixed IP to your Pi in 2 ways. One is setting it in the Pi itself and the other option is to set it in your network router. I would prefer the second option as this would avoid the possibility of assigning the Pi’s IP to some other device in your network by the router while the Pi is not available (switched off or disconnected) in the network.
Setting fixed IP in Pi
Open the terminal window and type “sudo leafpad /etc/network/interfaces“. This would open a window like the one displayed below.
Copy and modify the below lines according to your router settings and insert it after the line “iface wlan0 inet manual“.
To find the currently assigned IP address of your PI type “hostname -I” in the terminal. To view the full network connection details type the command “ifconfig”. The below image displays the details of “wlan0” segment in my Pi.
The updated file would look like the one shown below.
save the file and reboot the Pi. This would assign the new IP address with the netmask and gateway specified in this file.
Setting fixed IP in router
In this method you have to make changes in both the interfaces file in Pi and also you have to add a DHCP static entry in your router.
Open up your router configuration page and assign a static IP for your Pi. This settings may change from router to router. The below screen shot is based on my home router.
As a next step, In the “interfaces” file of Pi change the “manual” mode to “dhcp” instead of adding address, netmask and gateway as shown below
Save the changes and perform a reboot. This would force the router to assign a static IP ( mentioned in router) for your Pi when ever its connected to your home network.