linux下修改hostid
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 'x09x12x0axaa' > /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/
總結
以上是生活随笔為你收集整理的linux下修改hostid的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Linux常见系统故障
- 下一篇: 圆的面积和周长