Modify HostID under Linux

Source: Internet
Author: User
Tags pack

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:
    1. #include <unistd.h >
    2.  int main (void ) {
    3.   Sethostid (0x12345678) / ( 16 binary ) /
    4.  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:
  1. #! / usr/bin/python
  2. From struct Import pack
  3. A = ' 10.0.0.130 '
  4. L =asplit('. ') )
  5. Id= Hex(int(l[1]))[2:]+‘-‘+hex(int(l[0]))[2:]+-"+hex (Int[3]) ) [2:]+-"+hex (Int[2]) ) [2: /span>
  6. ID = ' 0x '+id
  7. HostID = Pack("I", int(ID, +))
  8. FileName = "/etc/hostid"
  9. 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:
    1. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.