How to Set a Static IP for Your Python Project
Scenario
I recently needed to establish a static IP for my Python project to facilitate SSH access. The issue I faced was that DHCP kept assigning a new IP every time my system rebooted.
I discovered that we can set the IP address to be static, and the steps for doing so are outlined below:
About:
dhcpcd.conf:
Location: /etc/dhcpcd.conf Usage: DHCP client daemon is used towards the client side to configure the interfaces.
while
dhcpd.conf
Location: /etc/dhcp/dhcpd.conf Usage: It acts as server for dynamically assigning IPs to devices in the network
Step 0: Setup dhcpcd:
Install:
sudo apt-get install dhcpcd
Enable:
sudo systemctl enable dhcpcd
sudo systemctl start dhcpcd
Verify:
sudo systemctl status dhcpcd
Step 1: Open the dhcpcd.conf file
sudo nano /etc/dhcpcd.conf
If your project uses Wi-Fi, proceed with the following steps; otherwise, replace the interface name accordingly.
interface wlan0
static ip_address=your_desired_static_ip/24
static routers=your_router_ip
static domain_name_servers=your_dns_server_ip
or
interface wlan0
static ip_address=your_desired_static_ip/24
static routers=192.168.0.1
static domain_name_servers=192.168.0.1 8.8.8.8
Replace “your_desired_static_ip”, “your_router_ip”, and “your_dns_server_ip” with the appropriate values for your network. Ensure you choose an IP address that is not already in use on your network.
How to get router ip: Refer whats my ip address?
How to get the dns server: On terminal do nslookup google.com
Note: you need to have package 'dnsutils' installed. (sudo apt-get install dnsutils
)
Step 2: Restart the dhcp service
sudo service dhcpcd restart
or do sudo reboot