linux下修改hostid

网上有很多版本,总结了这几点。
1> 一个以16进制显示的int字符串;
2> 配置文件: /etc/hostid; 如果有值,输出, 结束。
3> 从hostname相对应ip的值经过某一算法,输出, 结束。
3.1 其中一个算法流传很多: ip地址转换成hex,然后将位置2143位置颠倒
4>如果没有配置相对应的hostname相对应的IP, 输出00000000, 结束;
     
网上有几个列子:
例子1. c语言版:
  • #include <unistd.h>
  •  int main(void){
  •  sethostid(0x12345678); /* 将hostid设为12345678(16进制) */
  •  return 1;
  • }
  • 经测试,第一次设置起到作用,并且/etc/hostid的值发生相对应的变化。以后的设置不起作用,/etc/hostid的值没有变。估计里面做了判断。

    例子2. python直接修改根据hostname相对应的ip修改/etc/hostid 文件:
  • #!/usr/bin/python
  • from struct import pack
  • a = '10.0.0.130'
  • l =a.split('.')
  • id = hex(int(l[1]))[2:]+'-'+hex(int(l[0]))[2:]+'-'+hex(int(l[3]))[2:]+'-'+hex(int(l[2]))[2:]
  • id = '0x'+id
  • hostid = pack("I",int(id,16))
  • filename = "/etc/hostid"
  • open(filename,"wb").write(hostid)
  • 这个例子不适合我,我的hostname没有和ip关联,/etc/hosts以及/etc/hostname中都没有配置,同时不符合我的情况, 就没有测试。不过目测它是能起到作用的, 因为修改了配置文件。

    例子3: 直接修改为某个hostid: aa0a1209。 一条命令:
  • echo -ne '\x09\x12\x0a\xaa' > /etc/hostid
  • 我的系统是小字节排序,知道原理,就直接倒序写了,c, python 闪一边去吧。



    参考:  
    sethostid 函数: http://manpages.ubuntu.com/manpages/jaunty/man2/sethostid.2.html
    python设置的例子: http://tdis.me/2013/02/fedora-18-linux-hostid-and-nuke/
    c设置的例子: http://heweist.blog.163.com/blog/static/346429092010023111742342/

    相关文章:

    • 2022-01-23
    • 2022-12-23
    • 2021-09-21
    • 2022-12-23
    • 2021-11-20
    • 2021-11-21
    • 2021-11-21
    猜你喜欢
    • 2022-12-23
    • 2022-01-25
    • 2022-12-23
    • 2021-08-10
    • 2022-02-24
    • 2022-01-11
    • 2022-02-15
    相关资源
    相似解决方案