Results 1 to 3 of 3

Thread: Changing the hostname permanently

  1. #1
    Just burned his ISO
    Join Date
    Aug 2008
    Posts
    15

    Exclamation Changing the hostname permanently

    Changing the hostname permanently

    Changing the hostname usually is a trivial task on most Linux OSes. Normally you just change /etc/hostname file to reflect the hostname you want to have. In BackTrack this unfortunately doesn’t work as expected. First of all BackTrack keeps its hostname not in /etc/hostname but in /etc/HOSTNAME. Now because BackTrack is build upon modules any changes made to the filesystem aren’t saved from boot to boot (unless you have specified a changes folder). So obviously you have to create a module that at boot time will replace the original /etc/HOSTNAME with your customized one.

    Usually this would work, but it doesn’t, because BackTrack has a little bug. Well it’s not actually a bug, but it simply has some crud left over from when it was built that is interfering with /etc/HOSTNAME. During a normal bootup of the system, before the hostname is set, the system initializes you’re network cards, and once that is done it tries to set up these cards using DHCP. This DHCP information usually contains useful information like what IP the card should have, what subnet it should use, etc… One of the information you can get through DHCP is the domain of the computer. That’s why when the DHCP requests for you cards have finished, the next part of the bootup tries to append the domain from those DHCP requests to the hostname(but it appends it to bt hostname located in rc.M and not the one in /etc/HOSTNAME). Now because BackTrack has some crud left over, previous request are still in /etc/dhcpc/ directory, the HOSTNAME file is allways ignored. So basically even if you overwrite the /etc/HOSTNAME file, it will never be used, because it’s always overwritten with the information provided by the files located in /etc/dhcpc/.

    Here are the steps that need to be taken in order to restore BackTrack to it’s intended working state. We are going to have to extract the BT3/base/etc.lzm module to a temporary directory, and remove all files from the etc/dhcpc/ directory. We are then going to have to remake the module from the directory contents, and replace the original etc.lzm file. Here are the commands used:

    mkdir /tmp/etc_tmp
    lzm2dir /mnt/sda1/BT3/base/etc.lzm /tmp/etc_tmp
    rm –rf /tmp/etc_tmp/etc/dhcpc/*
    dir2lzm /tmp/etc_tmp /tmp/etc.lzm
    mv /tmp/etc.lzm /mnt/sda1/BT3/base/etc.lzm
    Please note that /mnt/sda1 is the location of boot media! If yours differs please change accordingly, also this guide assumes that you are using BackTrack on read-writable medium.

    If you have done everything correctly you should now be able to create a module with the your hostname settings, that will be loaded correctly at boot time. Here is a example of creating such a module:


    mkdir /tmp/hostnameSettings
    mkdir /tmp/hostnameSettings/etc
    echo “MyHostname.MyDomain” > /tmp/hostnameSettings/etc/HOSTNAME
    dir2lzm /tmp/hostnameSettings/ /tmp/CustomHostname.lzm
    mv /tmp/CustomHostname.lzm /mnt/sda1/BT3/modules/CustomHostname.lzm
    Hope this helps someone out. Any comments or suggestions are welcome.

  2. #2
    Developer
    Join Date
    Mar 2007
    Posts
    6,126

    Default

    We very much appreciate the tutorials so this is in no way meant to discourage you however there is a much easier way to do this. Its not a bug its just that there is a start up script that runs at boot which overwrites the host name file.

    The script is located at /etc/rc.d/rc.M and the relevant line is line 98 where is says
    echo "bt." $DOMAIN > /etc/HOSTNAME

    All you have to do is change the "bt" to whatever you want however make sure you leave the "." after the hostname or else it will be all messed up.

  3. #3
    Just burned his ISO
    Join Date
    Aug 2008
    Posts
    15

    Default

    Actually I'm aware of that I just wanted to show how it should have been from the start. The files in /etc/dhcpc/ directory shouldn't have been there in the first place. But you are right about my method not being the most simple. It's a fix and tutorial combo

    The best way to fix this would be to change the script so that it takes the hostname from the /etc/HOSTNAME file, and if a domain is found by dhcp it would only append the domain to the existing hostname in the /etc/HOSTNAME.
    I would change the following:

    Code:
    # Set hostname by using DHCP response
    ls -aA1b /etc/dhcpc | egrep ".info\$" | while read INFOFILE; do
       # the next line won't affect rc.M variables, because it's in >while read< loop
       . /etc/dhcpc/$INFOFILE
       echo "bt."$DOMAIN >/etc/HOSTNAME
       break
    done
    Into the following:
    Code:
    # Set hostname by using DHCP response
    ls -aA1b /etc/dhcpc | egrep ".info\$" | while read INFOFILE; do
       # the next line won't affect rc.M variables, because it's in >while read< loop
       . /etc/dhcpc/$INFOFILE
       HOSTNAME=$(cat /etc/HOSTNAME | head -n 1 | cut -d . -f 1)
       [ "$HOSTNAME" == "" ] && HOSTNAME="bt"
       echo $HOSTNAME"."$DOMAIN >/etc/HOSTNAME
       break
    done
    This solution is more universal I think...
    =----------------=
    Imagination is more important than knowledge. For while knowledge defines all we currently know and understand, imagination points to all we might yet discover and create.
    =----------------=
    =--=

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •