Staviar
Posts: 40
Joined: Tue Sep 10, 2019 9:51 am

[SOLVED]How to set up dhcp IP from file similar to setting up Static IP in dhcpcd.conf

Wed Sep 18, 2019 8:01 am

Hello !

I am using a Raspberry Pi 4 with Linux raspberrypi 4.19.58-v7l+ #1245 SMP Fri Jul 12 17:31:45 BST 2019 armv7l GNU/Linux.

I would like to be able to set a DHCP IP just like I have set a Static IP.
I have set a static IP by adding the following lines in /etc/dhcpcd.conf :

Code: Select all

interface eth0
static ip_address=192.168.1.20/24
static routers=192.168.1.1
static domain_name_servers=8.8.8.8
What do I need to add and where to be able to do something similar to what I've done with the Static IP.

I need this method because I want to link to it a php interface in which an user can enter network credentials. I have also done this for Static IP.

Thank you !
Last edited by Staviar on Wed Sep 18, 2019 11:21 am, edited 1 time in total.

RattusRattus
Posts: 70
Joined: Sat Sep 29, 2018 11:27 am

Re: How to set up dhcp IP from file similar to setting up Static IP in dhcpcd.conf

Wed Sep 18, 2019 8:37 am

Hi there

setting an IP address can be done 2 ways ether static or dynamically

if you are setting your IP statically then this is done on your local machine and that would be a setting in /etc/networks

If you want dynamic allocation there are several ways to obtain an IP address

traditionally (and my preferred method) you have a device on your network that acts as the DHCP server. for a home network this is often your Internet modem / router / wifii access point.

If you have one of these and it is acting as your DHCP server then this will be handing out IP addresses or rather a LEASE of an address for a period of time to machines running a DHCP CLIENT whenever they ask.

So if you want to always have the same IP address for your RPI when it is on that specific network, then you will need to look for a setting on your Internet modem / router / wifii access point. (of cause they are pretty much all different and it is possible that this isn't provided by the box that you have).

If you were running your own DHCP server on a Linux based machine then take a look at isc-dhcp-server (you can reserve leases for given machines based on the MAC address of the network interface.

Staviar
Posts: 40
Joined: Tue Sep 10, 2019 9:51 am

Re: How to set up dhcp IP from file similar to setting up Static IP in dhcpcd.conf

Wed Sep 18, 2019 9:12 am

I am actually interested in which file do I need to write SSID and Password for the desired network.

epoch1970
Posts: 4139
Joined: Thu May 05, 2016 9:33 am
Location: Paris, France

Re: How to set up dhcp IP from file similar to setting up Static IP in dhcpcd.conf

Wed Sep 18, 2019 9:15 am

Staviar wrote:
Wed Sep 18, 2019 8:01 am
Hello !

I am using a Raspberry Pi 4 with Linux raspberrypi 4.19.58-v7l+ #1245 SMP Fri Jul 12 17:31:45 BST 2019 armv7l GNU/Linux.

I would like to be able to set a DHCP IP just like I have set a Static IP.
I have set a static IP by adding the following lines in /etc/dhcpcd.conf :

Code: Select all

interface eth0
static ip_address=192.168.1.20/24
static routers=192.168.1.1
static domain_name_servers=8.8.8.8
What do I need to add and where to be able to do something similar to what I've done with the Static IP.

I need this method because I want to link to it a php interface in which an user can enter network credentials. I have also done this for Static IP.

Thank you !
By default on Raspbian dhcpcd will try to get an address via DHCP for all active interfaces.
There is nothing to add. Just remove the static IP address definition if there is one for the interface.
"S'il n'y a pas de solution, c'est qu'il n'y a pas de problème." Les Shadoks, J. Rouxel

epoch1970
Posts: 4139
Joined: Thu May 05, 2016 9:33 am
Location: Paris, France

Re: How to set up dhcp IP from file similar to setting up Static IP in dhcpcd.conf

Wed Sep 18, 2019 9:18 am

And wireless credentials do to /etc/wpa_supplicant/*
"S'il n'y a pas de solution, c'est qu'il n'y a pas de problème." Les Shadoks, J. Rouxel

Staviar
Posts: 40
Joined: Tue Sep 10, 2019 9:51 am

Re: How to set up dhcp IP from file similar to setting up Static IP in dhcpcd.conf

Wed Sep 18, 2019 9:33 am

epoch1970 wrote:
Wed Sep 18, 2019 9:18 am
And wireless credentials do to /etc/wpa_supplicant/*
Ok. I guess the file in question is wpa_supplicant.conf.

All I have in /etc/wpa_supplicant/wpa_supplicant.conf is

Code: Select all

ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
And from what I've read online I need to add something like:

Code: Select all

network={
 scan_ssid=1
 ssid="MyNetworkSSID"
 psk="Password1234"
}
But can you tell me what ctrl_interface and update_config do ?

Also, a weird thing happened when I have deleted the content of /etc/dhcpcd.conf. It automatically switched to DHCP, which is normal, and it connected to my password protected router, which is weird, because it did not know the SSID or the password. How could the Pi connect to something it doesn't know about ?

Staviar
Posts: 40
Joined: Tue Sep 10, 2019 9:51 am

Re: [SOLVED]How to set up dhcp IP from file similar to setting up Static IP in dhcpcd.conf

Fri Sep 27, 2019 12:06 pm

Here is an example for Wireless DHCP that works nicely for me:

wpa_supplicant.conf

Code: Select all

ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
country=GB
network={
ssid="MySSID"
psk="Password123"
}
dhcpcd.conf

Code: Select all

interface wlan0
hostname ProjectWiFi
clientid ProjectWifi
interface eth0
noipv4
noipv6
The point for wireless is to add a country in "wpa_supplicant.conf", otherwise it won't work.

Return to “Beginners”