yyyyyyyyyyyyyyyyyyyy

博客园 首页 新随笔 联系 订阅 管理

linux下修改hostid

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

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

例子3: 直接修改为某个hostid: aa0a1209。 一条命令:
  1. 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/
posted on 2015-09-04 12:39  xxxxxxxx1x2xxxxxxx  阅读(2785)  评论(0编辑  收藏  举报