Setting up Static IP in Raspberry Pi

Usually DHCP is enabled by default.
Means Pi will get new random IP on most cases. But building a Project with Dynamic IP is Pain, Because the IP changes frequently.
Why Static IP?
My Raspberry Pi hosts lot of In-house tools, If IP Address changes frequently either i have login to the Router and Check Pi Address or Using the Fing app does the Job.
How about giving static IP to Pi which never changes.
Follow the below instruction to set static IP address in Raspberry pi.
Open the Terminal and run following commands.
sudo nano /etc/dhcpcd.conf
This will open up DHCP Configuration file.
Paste the below line inside the file.
If you connected in Raspberry Pi via Ethernet Cable
interface eth0
static ip_address=192.168.1.31/24
static routers=192.168.1.1
static domain_name_servers=192.168.1.1
If you connected in Raspberry Pi via WiFi
interface wlan0
static ip_address=192.168.1.31/24
static routers=192.168.1.1
static domain_name_servers=192.168.1.1
Once you're done. Press CTRL+O to save the file, Then CTRL+X to exit.
Now, Perform reboot. sudo reboot
Let's break the above lines...
interface eth0
is the Network Interface I'm using. Here it means ethernet portstatic ip_address=192.168.0.10/24
means,192.168.0
Network ID and10
means host addressstatic routers=192.168.0.1
means IP Address of my routerstatic domain_name_servers=192.168.0.1
Means, Domain Name Server. It can be anything like your router IP orGoogle Public DNS 8.8.8.8
Note: IP Address of your router may vary
To check the IP Address. Run the Below Command in Terminal
ifconfig
If you face any issue. Feel free to comment below.