📜 ⬆️ ⬇️

Automatic compilation of computer name and issue it via DHCP

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

On the DHCP server, I use the ISC DHCP Server (dhcpd). To solve the problem, I used the built-in ability to use 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 hostnames for each machine: what if you need to install not 15 machines, but 100? Therefore, we will issue a computer name based on IP, issued to it by DHCP. The configuration string 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 specified using the third operand. Finally, we take only the last two characters from the address (01) and perform concatenation with the string “pc”, getting the address pc01. The uniqueness of IP guarantees us the uniqueness of the computer name.

And this is the complete configuration file:

ddns-update-style none;

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

log-facility local7;

subnet 192.168.0.0 netmask 255.255.255.0 {
range 192.168.0.101 192.168.0.199;
option domain-name-servers 192.168.0.1;
option host-name=concat("pc", suffix(binary-to-ascii(10,8, "", leased-address),2));
filename="pxelinux.0";
}


As you can see, I restricted the range to 192.168.0.101-192.168.0.199, i.e. from pc01 to pc99.

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

Source: https://habr.com/ru/post/87304/


All Articles