Machine Hostid


Primeiramente gostaria de salientar que não sou o autor principal do conteúdo deste artigo. A minha intenção é de mostrar o que outros autores tem realizado (referenciando links para o conteúdo original) neste blog até como um guia pessoal para uso próprio e ainda compartilhar minhas anotações e pequenas modificações que realizo no conteúdo, desejando que possa ajudar mais alguém em algum lugar.
First and foremost, I take no credit for any of this post’s content. I am really just taking what others have done (which I have links to bellow) and am putting it on my blog for a personal reference and hopefully the small changes that I made to their guides will help someone somewhere.


link: http://www.edaboard.com/thread5989.html



changehostid.c #include <stdio.h> #include <unistd.h> int main() { long id,res; // get real (default) hostid id = gethostid(); printf("current hostid is: %x\n",id); // set new hostid if is superuser res = sethostid(0x11223344); if (res == 0) printf("if result is zero - success! (%d) \n",res); // check if it is changed.... id = gethostid(); printf("current hostid is: %x ;-PPPppppp\n",id); } compile with gcc -o changehostid changehostid.c run with ./changehostid








#!/bin/bash
#
# Purpose: Write the passed in parameter as hostid to /etc/hostid
#          If no parameter is passed, write current hostid to /etc/hostid
# Author:  Fazle Arefin

if [[ -n "$1" ]]; then
  host_id=$1
  # chars must be 0-9, a-f, A-F and exactly 8 chars
  egrep -o '^[a-fA-F0-9]{8}$' <<< $host_id || exit 1
else
  host_id=$(hostid)
fi

a=${host_id:6:2}
b=${host_id:4:2}
c=${host_id:2:2}
d=${host_id:0:2}

echo -ne \\x$a\\x$b\\x$c\\x$d > /etc/hostid &&
  echo "Success" 1>&2

exit 0

The script can also be found in my GitHub repo

git clone https://github.com/fazlearefin/sysadm-scripts.git

Example

Here's a demonstration on my Ubuntu 12.04. I changed to root user first using sudo su -
root@ubuntu1204:~# # My original hostid
root@ubuntu1204:~# hostid 
007f0101
root@ubuntu1204:~# 
root@ubuntu1204:~# # Now let's change the hostid to 0011aabb
root@ubuntu1204:~# # using the script named sethostid.sh
root@ubuntu1204:~# ./sethostid.sh 0011aabb
0011aabb
Success
root@ubuntu1204:~# # Now let's check the hostid again
root@ubuntu1204:~# hostid 
0011aabb
root@ubuntu1204:~# # It changed!
root@ubuntu1204:~# 
root@ubuntu1204:~# # Now let's revert back to our original hostid
root@ubuntu1204:~# rm /etc/hostid 
root@ubuntu1204:~# hostid 
007f0101
root@ubuntu1204:~# # Back to out original hostid
root@ubuntu1204:~# 



Comments