MuntyScruntfundle
Posts: 223
Joined: Fri Oct 27, 2017 11:14 pm

Set IP in boot sector?

Wed Nov 07, 2018 12:58 pm

I believe the answer to this is already no, but I thought I'd ask...

Is there any way to set an ip address within the boot sector somehow? My image is part setup and flashed to many cards, part of the setup is a static ip address, it has to be static. Manually editing every dhcpcd.conf is going to be boring at best as I'll have to boot up a pi for every single one. If I could quickly edit the boot partition I could get it done in a lot less time.

Possible or not?

Many thanks.

User avatar
topguy
Posts: 5674
Joined: Tue Oct 09, 2012 11:46 am
Location: Trondheim, Norway

Re: Set IP in boot sector?

Wed Nov 07, 2018 1:26 pm

I think its possible

Basic idea is that you add it as parameter to kernel. So you append it to cmdline.txt in the boot partition.

Code: Select all

ip=192.168.1.200::192.168.1.1:255.255.255.0:rpi:eth0:off
ref: pont 3 in this guide : https://kr15h.github.io/RPi-Setup/

W. H. Heydt
Posts: 10749
Joined: Fri Mar 09, 2012 7:36 pm
Location: Vallejo, CA (US)

Re: Set IP in boot sector?

Wed Nov 07, 2018 3:33 pm

Now that you have an answer, this is a nitpick...

There is no "boot sector" or the SD card for a Pi. There is a FAT partition that is normally mounted as /boot, sometimes referred to as the "boot partition".

mfa298
Posts: 1387
Joined: Tue Apr 22, 2014 11:18 am

Re: Set IP in boot sector?

Wed Nov 07, 2018 3:36 pm

MuntyScruntfundle wrote:
Wed Nov 07, 2018 12:58 pm
Is there any way to set an ip address within the boot sector somehow? My image is part setup and flashed to many cards, part of the setup is a static ip address, it has to be static.
For that I would normally leave the pis to get their address via dhcp and allocate them static IPs via the dhcp server. With the isc dhcp server it's relatively easy to script generation of the config files.

This also gives the advantage of not needing to customise that part of the config on each image after writing it to an sd card.

andrum99
Posts: 772
Joined: Fri Jul 20, 2012 2:41 pm

Re: Set IP in boot sector?

Wed Nov 07, 2018 5:04 pm

mfa298 wrote:
Wed Nov 07, 2018 3:36 pm
MuntyScruntfundle wrote:
Wed Nov 07, 2018 12:58 pm
Is there any way to set an ip address within the boot sector somehow? My image is part setup and flashed to many cards, part of the setup is a static ip address, it has to be static.
For that I would normally leave the pis to get their address via dhcp and allocate them static IPs via the dhcp server. With the isc dhcp server it's relatively easy to script generation of the config files.

This also gives the advantage of not needing to customise that part of the config on each image after writing it to an sd card.
That's the way I do IP address assignment on my local network, but it's not really correct to call it "static IPs". Static IP addresses get assigned to a specific interface all of the time, and are assigned by setting the IP address on the host itself, for example on the kernel command line, so whatever network you plug the host into, the interface always comes up with the same IP. If you're allocating a host the same IP using DHCP then this is called DHCP address reservation. The advantage of DHCP address reservation over static IPs is that you can reconfigure your network from a single point - the DHCP server - rather than having to go round every host and change the static IPs for each interface. It also means you can take a host and plug it into some other network, and it will get a valid IP address from the whatever DHCP server happens to be there, which allows it to communicate correctly without you having to go into the host and change the IP address by hand.

mfa298
Posts: 1387
Joined: Tue Apr 22, 2014 11:18 am

Re: Set IP in boot sector?

Wed Nov 07, 2018 5:47 pm

andrum99 wrote:
Wed Nov 07, 2018 5:04 pm
mfa298 wrote:
Wed Nov 07, 2018 3:36 pm
For that I would normally leave the pis to get their address via dhcp and allocate them static IPs via the dhcp server. With the isc dhcp server it's relatively easy to script generation of the config files.

This also gives the advantage of not needing to customise that part of the config on each image after writing it to an sd card.
That's the way I do IP address assignment on my local network, but it's not really correct to call it "static IPs". Static IP addresses get assigned to a specific interface all of the time, and are assigned by setting the IP address on the host itself, for example on the kernel command line, so whatever network you plug the host into, the interface always comes up with the same IP. If you're allocating a host the same IP using DHCP then this is called DHCP address reservation. The advantage of DHCP address reservation over static IPs is that you can reconfigure your network from a single point - the DHCP server - rather than having to go round every host and change the static IPs for each interface. It also means you can take a host and plug it into some other network, and it will get a valid IP address from the whatever DHCP server happens to be there, which allows it to communicate correctly without you having to go into the host and change the IP address by hand.
Within the ISC dhcpd server (the one I mentioned, which was originally supplied as the reference DHCP server implementation) they've traditionally talked about Static IP Allocations for addresses configured via the configuration using a fixed-address statement. Although maybe it would have been better if I described that as a Fixed address rather than Static IP.

More recently (the last few years) the ISC dhcpd has also introduced support for reserved addresses but I'm not sure these can be easily scripted into the config file. I think you have to talk to the dhcp server(s) via an API to set these.

So for a scripted configuration file the easiest method is probably via a Fixed address allocation (what I called a static IP before) if someone wanted to talk directly to the API (which I'm not sure is that well documented) they could set address reservations.

User avatar
HawaiianPi
Posts: 4530
Joined: Mon Apr 08, 2013 4:53 am
Location: Aloha, Oregon USA

Re: Set IP in boot sector?

Wed Nov 07, 2018 9:18 pm

As others have said, it's usually simpler and better to reserve an IP in your router's DHCP settings and leave the OS alone.

  • The procedure is the same for all devices on your network, regardless of device type or OS.
  • No mucking about with OS configurations which may change over time (as it did between Jessie and Stretch).
  • Avoids address conflicts by reserving the IP so it can't be assigned to another device by DHCP.
  • IP is reserved by MAC address, so the same device will always get the same IP, regardless of OS.

The main drawback is that you need to boot the Pi to find it's MAC address (some other computers I've purchased had the MAC on a sticker). The simplest solution is to boot the Pi and then reserve whatever IP address DHCP assigns. If you want a specific IP address you may need to reboot your router (depends on the router), and that may elicit screams of anguish from the rest of your household (if it's anything like mine).
My mind is like a browser. 27 tabs are open, 9 aren't responding,
lots of pop-ups...and where is that annoying music coming from?

andrum99
Posts: 772
Joined: Fri Jul 20, 2012 2:41 pm

Re: Set IP in boot sector?

Wed Nov 07, 2018 11:26 pm

I don't know about the ISC DHCP server, but I've always referred to allocating specific IP addresses to specific hosts using DHCP as DHCP address reservation, and I use the term static IP address to refer to fixing the IP by configuring it directly on the host. That way everyone knows what we're talking about. If you start calling DHCP address reservation the same thing it kind of muddies the waters, that's all I was getting it.

You can certainly set the IP address of the primary network interface using the kernel command line, which would allow you to assign a specific IP address to the Pi before you boot it, although obviously you would need to edit the cmdline.txt file somehow after you've written the SD card. To me the DHCP address reservation method works best for multiple Pi's, although another consideration is that this requires access to the DHCP server, which you may not have, so in this case using the kernel command line, or some other config file in Linux, might be your best bet.

I would suggest that if your router needs rebooted in order to set up DHCP address reservation, then that's a bug.

W. H. Heydt
Posts: 10749
Joined: Fri Mar 09, 2012 7:36 pm
Location: Vallejo, CA (US)

Re: Set IP in boot sector?

Thu Nov 08, 2018 12:25 am

andrum99 wrote:
Wed Nov 07, 2018 11:26 pm
You can certainly set the IP address of the primary network interface using the kernel command line, which would allow you to assign a specific IP address to the Pi before you boot it, although obviously you would need to edit the cmdline.txt file somehow after you've written the SD card.
That is rather trivially easy to do.

drgeoff
Posts: 9737
Joined: Wed Jan 25, 2012 6:39 pm

Re: Set IP in boot sector?

Thu Nov 08, 2018 12:37 am

To set a fixed address on the RPi itself you need to use an address which is not already allocated to another device in the subnet and you need to know the range(s) of addresses that are outside the pool used by the DHCP server.

n67
Posts: 938
Joined: Mon Oct 30, 2017 4:55 pm

Re: Set IP in boot sector?

Thu Nov 08, 2018 1:31 am

The real underlying on this thread is the idea of "I want it to be on the FAT partition so that I can edit it from Windows" (or some other non-Linux OS).

The point is that if you relax this requirement, then it becomes trivial to fix whatever needs fixing on the ext4 partition. And the easy way to eliminate the requirement is to use a Linux PC to do the editing.

So, I would suggest to the OP that he do exactly that. Use a Linux PC to do his editing. Then the need to be hacking around on the FAT partition evaporates.
"L'enfer, c'est les autres"

G fytc hsqr rum umpbq rm qyw rm rfc kmbq md rfgq dmpsk:

Epmu Sn!

J lnacjrw njbruh-carppnanm vxm rb mnuncrwp vh yxbcb!

incognitum
Posts: 306
Joined: Tue Oct 30, 2018 3:34 pm

Re: Set IP in boot sector?

Fri Nov 09, 2018 9:58 pm

Does it need to be IPv4?
IPv6 link local addresses based on MAC address can also work for some use-cases...

MuntyScruntfundle
Posts: 223
Joined: Fri Oct 27, 2017 11:14 pm

Re: Set IP in boot sector?

Fri Nov 09, 2018 10:48 pm

I can't let DHCP allocate address', I can't reassign the number of pi's I have to setup, that would be a complete logistical nightmare. Therefore I asked if it was possible to set a static IP address in the boot sector. Or boot partition. Or boot folder. Or the little bit of the SD assigned to booting.

I know you can't see the problem, if you could I think you'd better understand.

Thanks to the first responder, I'll have a good look into this, it could save me hours if not days!

:o)

mfa298
Posts: 1387
Joined: Tue Apr 22, 2014 11:18 am

Re: Set IP in boot sector?

Sat Nov 10, 2018 1:07 am

MuntyScruntfundle wrote:
Fri Nov 09, 2018 10:48 pm
I know you can't see the problem, if you could I think you'd better understand.
I fully understand the issue. I've managed hundreds of devices that need known IP addresses (including a project with ~100 pis). My solution to that has generally been DHCP and systems automation (Chef in my case).

For instance I have a dhcp (ISC dhcpd) config a bit like:

Code: Select all

    host pi001 {
        hardware ethernet b8:27:eb:c4:24:01;
        fixed-address 192.168.0.101;
        option host-name "pi001";
    }
    host pi002 {
        hardware ethernet b8:27:eb:c4:24:02;
        fixed-address 192.168.0.102;
        option host-name "pi002";
    }
This assigns static IPs to each Pi via DHCP based on their Ethernet Addresses. My actual config has a bit more as it also supports netboot for some Pis. The benefit of this approach is I only ever have to enter the details (Ethernet and IP addresses) once for the DHCP server and that Pi gets it's name (with a small change in the image) and fixed address via DHCP without having to create a custom image per Pi. With a configuration tool (which could be as simple as a text file and short script) you can automate the generation of that configuration (and in my case also automation of netbooting for some Pis).

Manually editing every single SD card after writing the image (as you seem to be planning) becomes tedious and prone to errors quite quickly. I've done manual configuration of multiple sets of similar systems in the past and the error rate is surprisingly high (even when you think you're being very careful not to make mistakes). Also remember for 100 Pi's you're likely to have to apply the config manually >>100 times over the course of their life as things need to be reset. In my approach I can stick any OS image with no modification on that Pi and I know what IP it will have (and if I happen to plug it into another network for testing it'll still get a valid IP from dhcp - just not the one it's been allocated).

The other things to consider is how you can set things like the hostname, domain name, dns servers etc. These can all be done automatically via DHCP (small change needed to the image to make it pick up the hostname via DHCP) I'm not sure how well those work (if at all) via the ip= option which I think is really aimed at giving the kernel enough stuff that it can mount an NFS filesystem for the rootfs (it's documented in the nfsroot documentation rather than in something more generic) and then get a fuller config from the OS once that's mounted (which you might have to apply manually once the Pi has been booted into that image).

Return to “General discussion”