Automatically compiling a computer name and issuing it via DHCP

    Recently there was the task of installing a Linux distribution on a fleet of 15 machines. The most automated method for such an installation is network installation. As such, many manuals are devoted to this task; there are tools both specific for each distribution (debian-installer, kickstart) and universal (CloneZilla, System Installer). In this article I want to write about how, when solving this problem, to ensure that each machine is assigned a computer name in pcNN format, where NN are numbers in order from 01 to 99, in my solution it will be the last two decimal places from IP addresses Googling on this topic, I did not find a ready-made answer, therefore, delving into the manual for the DHCP server I found a solution and decided to share it with the habrasociety.

    On a DHCP server, I use ISC DHCP Server (dhcpd). To solve the problem, I used the built-in ability to use the so-called. expressions to set any parameters.

    Typically, the computer name can be set in this format:

    option host-name "example";

    And bind it, for example, to a specific machine by MAC address. But I was too lazy to prescribe the hostnames for each machine: what if I need to put not 15 machines, but 100? Therefore, we will issue a computer name based on the IP issued to it via DHCP. The configuration line for this is as follows:

    option host-name=concat("pc", suffix(binary-to-ascii(10,8, "", leased-address),2));

    Using the "=" sign, we show that the option will be set using expression'a.
    leased-address returns the given IP address in binary format, using the binary-to-ascii function we convert it to binary decimal format without separators (for example, 19216801), the separator can be set using the third operand. Finally, we take only the last two characters from the address (01) and concatenate with the string “pc”, getting the address pc01. The uniqueness of IP guarantees us the uniqueness of a computer name.

    And this is the full configuration file: As you can see, I limited the range to addresses, i.e. from pc01 to pc99.

    ddns-update-style none;

    default-lease-time 600;
    max-lease-time 7200;

    log-facility local7;

    subnet netmask {
    option domain-name-servers;
    option host-name=concat("pc", suffix(binary-to-ascii(10,8, "", leased-address),2));

    Of course, with this algorithm, a problem arises that a different IP can be assigned to the same computer, therefore, the computer name will be different each time. But when using this scheme for installation over a network, the system is turned on and installed, as a rule, at a time, and the name issued via DHCP is written into the configuration of each machine (at least when installing using the debian-installer), so the problem is not so significant. If you need a name that is rigidly attached to a specific machine, you can compile it based on the MAC address (for example, its last byte), or display the IP addresses themselves based on the MAC address.

    Also popular now: