What is the hostname?
The hostname (hostname) is the name of a device within a network. This host name makes it possible to distinguish between devices on a local network and also allows computers to locate others, making it possible to exchange data within a network, for example. Host names are used on the Internet as part of the Fully Qualified Domain Name.
How to know the current hostname?
To know the name of the linux machine we are working on, we can use a simple command that returns the current hostname.On a Pi or other Debian-based Linux machine, use the following command:
hostname
Why do we want a dynamic hostname?
Well, in productive environments it is important to reference the machines in an automated way.
The raspberry Pi for example, has 2 network interfaces, Wifi and Ethernet, it is important that our dynamic hostname is created by connecting our equipment through either of these two interfaces.you can check which interface is being used from the terminal:
ifconfig -a
What parameters can we use to create unique names in our hostname? the MAC address.
The Mac Address or Mac address is a unique 48-bit identifier to identify all network devices such as Ethernet network cards, Wi-Fi or wireless network cards, network switches, routers, printers, etc.MAC addresses are unique identifiers worldwide for each device and therefore it is impossible to find 2 network cards or 2 network devices that have the same Mac Address.All manufacturers at the time of manufacturing the hardware, such as a Wi-Fi network card, record the Mac Address in binary format in a ROM memory of the device they are manufacturing. As the ROM memory is read-only, it is totally impossible to modify it and therefore this implies that the Mac address or identifier of a device can never be modified.
So, how can we know the MAC of our Wifi and Ethernet cards?
On a Pi, use the following command:
cat /sys/class/net/eth0/address
cat /sys/class/net/wlan0/address
Caution:
This works very well if we have the option of non predictable names active for our network cards, (this in raspbian can be deactivated with the command raspi-config / advanced options /.
If we do not want to have predictable names, your network card will appear with a type of name composed with the mac, of the type: enxb81122334455 for example for ethernet.
In this case you should create a script that can give us the name of the interface with a command like:
ip link | grep ether | tr -d ":" | awk '{print $ 2}'
ip link | grep wlan | tr -d ":" | awk '{print $ 2}'
for both ethernet and wifi.
It is usually a good idea to generate the hostname with a unique identifier such as the MAC, but it is still recommended to add a prefix that details exactly the series of computers we have on the network.
In our case we are going to use the prefix BD- (Blackdevice)
We already have all the necessary data to modify our system and generate a dynamic hostname depending on the interface that you have connected to the network.
Generating the dynamic hostname
We are going to back up our system config file first, so that we can generate a new one that dynamically creates the hostname:
sudo mv /etc/rc.local /etc/rc.bak
What we need is to modify our file /etc/rc.local To do this, we will use the nano editor that comes by default in almost all current Linux distributions:
sudo nano /etc/rc.local
let’s write our new rc.local:
#!/bin/sh -e
#
# rc.local
#
# This script capture $MAC value and assigns the dynamic hostname : (BD)+($MAC)
if [ -e /sys/class/net/eth0 ]; then
MAC="BD"$(cat /sys/class/net/eth0/address | tr -d ":")
else
MAC="BD"$(cat /sys/class/net/wlan0/address | tr -d ":")
fi
CURRENT_HOSTNAME=$(cat /proc/sys/kernel/hostname)
echo "$MAC"> "/etc/hostname"
#sed "s/127.0.1.1.*$CURRENT_HOSTNAME/127.0.1.1\t$MAC/g" /etc/hosts
IP="127.0.1.1"
if [ -n "$(grep $IP /etc/hosts)" ]
then
echo ""
else
sudo -- sh -c -e "echo '127.0.1.1 $MAC' >> /etc/hosts";
fi
hostname $MAC
This is all, now we can restart our raspberry and check if it has correctly generated the dynamic hostname.