How to set static IP address for Raspberry pi in headless setup?

For a headless raspberry pi device, which was configured for dhcp service in the network, if the router has become unavailable, the device IP assignment is not done in the network. In such scenario, the device will not be accessible in the network as no IP address is available to it. 

Using ssh, the raspberry pi device can be accessed from remote system. This require IP address of the raspberry pi device to be know beforehand. For raspberry pi running Raspbian OS from an SD card, there is a way to directly configure the IP address as static.

1) Remove the SD card and load it into an SD card reader. Now plug the card reader to laptop or reader slot attached with the system. Now two partitions will be mounted on the system /boot and /rootfs. To enable ssh simply create an empty file named ssh in the /boot partition
 $ cd /boot
 $ touch ssh

2) Navigate to "etc" folder in rootfs partition. In ubuntu terminal
 $ cd /rootfs/etc

3) Edit the file dhcpcd.conf with root privilages.
$ sudo nano dhcpcd.conf

4) Add the static IP details in the file. 
A sample network having IP range of 192.168.0.x/24 is used as an example here. Setting static IP address on interface eth0 with 192.168.0.113/24 and connected through a switch 192.168.0.60, the edited file is as shown below

5) Save the file (Ctrl+x, Press y).

6) Unmount both /boot and /rootfs partitions from the system

7) Plug the SD card back to the raspberry pi board. Connect to the network using ethernet cable. Now the device (with username "pi", here) is accessible through ssh from the system.
$ sudo ssh pi@192.168.0.113

Prompt will now appear for entering the password 

Comments

Popular from this site

Exception: Please shut down the controller which is running on port 6653: Mininet

Send and Capture icmp packets with type&code using hping