How to change the hostid of a Linux operating system

root's picture

Even if this tutorial includes only Linux, at least part of it may work on other UNIX like operating systems. You are free to test but as a golden rule: test it on TEST environment :).

So, what is hostid? Hostid simply prints the numeric identifier of the current host, in hex.
If you are curious what it does in detail, you can use strace command in front of it (e.g. run strace hostid).
In short, hostid takes the canonical name of the host (uname -n), greps it against /etc/hosts and the resulting ip address will transform it in hex in a bit of mixed up order. You will see below.

In our examples, we will play with docker containers and attempt to put the same host id as of the machine that runs them.

Hostid related details of a machine that runs the docker environment:

root@sonic:~# hostid
a8c0160b
root@sonic:~# uname -n
sonic
root@sonic:~# grep sonic /etc/hosts
192.168.11.22 sonic

In the following examples, we will change the host id of the docker container with the host id of the host where this container runs (details above)

Via /etc/hosts:

Will associate the canonical name with ip address in /etc/hosts file. This I do not recommend because you may need your docker container to have a different IP address. This is only for testing purpose.
Commands:
uname -n - shows the canonical name of the host
echo "192.168.11.22 8b7d80edc871" > /etc/hosts - will replace the content of the /etc/hosts file with host's ip address.
hostid - shows the hostid value

Output example:

root@1ebb82b3be22:/# hostid
11ac0300
root@1ebb82b3be22:/# uname -n
1ebb82b3be22
root@1ebb82b3be22:/# echo "192.168.11.22 1ebb82b3be22" > /etc/hosts
root@1ebb82b3be22:/# hostid
a8c0160b

Now this docker container have the same host id of the host.

Via /etc/hostid:

This time we will not touch the /etc/hosts file but will alter the host id by modifying the /etc/hostid file.
NOTE: when converting hex to octal, you need to put the values in reverse.
In our example, hex value of host id is "a8c0160b". So a8 = 250, c0 = 300, 16 = 26 and 0b = 13. The octal value that we will put in will be "\013\026\0300\0250".

Commands:
hostid - shows the hostid value
echo -e -n "\013\026\0300\0250" >/etc/hostid - will replace the content of /etc/hostid file with the octal output from echo command.

Output example:

root@399a2a5700a1:/# hostid
11ac0300
root@399a2a5700a1:/# uname -n
399a2a5700a1
root@399a2a5700a1:/# echo -e -n "\013\026\0300\0250" >/etc/hostid
root@399a2a5700a1:/# hostid
a8c0160b

So, as you can see from the examples above, it is not difficult to replicate a hostid from a host to another.

Extra stuff

You can generate the octal value for /etc/hostid also directly from the ip address.
So, as in the example above, If we would like to generate /etc/hostid from hex, the order is: 0b, 16, c0, and a8.
If we want to create a host id from 192.168.11.22, we will have the same octal value: "\013\026\0300\0250" but, as you can see, it is also in a different order: 11, 22, 192 and 168.
Decimal vs. octal for our ip address is: 192 = 300, 168 = 250, 11 = 13 and 22 = 26.

It is a bit twisted but I hope the table below will clears it up:

Ip address decimal numbers converted in Hex and Octal and their proper position to generate the host id
Decimal Hex Octal Hex position from decimal in hostid output Octal position in echo command for generation of /etc/hostid
192 C0 300 2 3
168 A8 250 1 4
11 B 13 4 1
22 16 26 3 2

Thou shalt not steal!

If you want to use this information on your own website, please remember: by doing copy/paste entirely it is always stealing and you should be ashamed of yourself! Have at least the decency to create your own text and comments and run the commands on your own servers and provide your output, not what I did!

Or at least link back to this website.

Recent content

root
root
root
root
root
root
root
root
root
root
root
root
root
root
root
root
root
root
root
root
root
root
root
root
root
root
root
root
root
root