Modify HostID under Linux
There are many versions on the Internet, summarizing these points.
1> an int string displayed in 16 binary;
2> configuration file:/etc/hostid; If there is a value, output, end.
3> the value of the corresponding IP from the hostname passes through an algorithm, outputs, ends.
3.1 One algorithm spreads a lot: The IP address is converted to hex, and the position 2,143 is reversed .
4> if no corresponding IP is configured for hostname, Output 00000000, end;
There are several columns on the Web:
Example 1. C language version:
- #include <unistd.h >
- int main (void ) {
- Sethostid (0x12345678) / ( 16 binary ) /
- return 1
-
After testing, the first setting plays a role, and the value of/etc/hostid occurs corresponding to the change. Subsequent settings do not work, and the value of/etc/hostid does not change. The estimate was made in judgment.
Example 2. Python modifies the/etc/hostid file directly based on the IP corresponding to the hostname:
- #! / usr/bin/python
- From struct Import pack
- A = ' 10.0.0.130 '
- L =asplit('. ') )
- Id= Hex(int(l[1]))[2:]+‘-‘+hex(int(l[0]))[2:]+-"+hex (Int[3]) ) [2:]+-"+hex (Int[2]) ) [2: /span>
- ID = ' 0x '+id
- HostID = Pack("I", int(ID, +))
- FileName = "/etc/hostid"
- Open(filename,"WB"). Write(HostID)
This example is not suitable for me, my hostname is not associated with IP,/etc/hosts and/etc/hostname are not configured, and do not meet my situation, there is no test. But visually it can work because the configuration file has been modified.
Example 3: Direct modification to a hostid:aa0a1209. a command:
- echo -ne ' \x09\x12\x0a\xaa ' >/ etc/hostid
My system is small byte sort, know the principle, directly reverse write, C, Python flash side go.
Reference:
Sethostid function: http://manpages.ubuntu.com/manpages/jaunty/man2/sethostid.2.html
Examples of Python settings: http://tdis.me/2013/02/fedora-18-linux-hostid-and-nuke/
Example of the C setting: http://heweist.blog.163.com/blog/static/346429092010023111742342/
Modify HostID under Linux