Configuring auto Hostname derived from MAC ID.

Asked by atul kayastha

I want to make an ubuntu ISO for my organization. And for hostname i want the devices to have name@last-6-digit-of -wifi-mac-id. How do i do it ?

Question information

Language:
English Edit question
Status:
Solved
For:
Cubic Edit question
Assignee:
No assignee Edit question
Solved by:
Cubic PPA
Solved:
Last query:
Last reply:
Revision history for this message
Cubic PPA (cubic-wizard) said (last edit ):
#1

In the Ubiquity installer, you are asked for "Your computer's name".

You want the installer to override whatever the user enters?

So, if your machines MAC is "ab:b9:f5:7e:2b:e3", you want your host name to be "7e2b3e" no matter what the user enters?

Revision history for this message
Cubic PPA (cubic-wizard) said (last edit ):
#2

Setting a *static* host name is pretty simple using preseed.

For example you would add the following to your preseed file:

    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    d-i netcfg/hostname string myhostname
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

However, you want the hostname to be different for each machine, depending on the mac address of the machine you are installing your customized OS to.

To achieve this, you can use the post install command ("ubiquity/success_command") in your preseed.

The hardest part will be parsing the machine's mac address to extract the last 6 characters.

The following should work...

On the Options page, on the Preseed tab, add the following to the end of your preseed file (ubuntu.seed):

    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    ubiquity ubiquity/success_command string in-target bash -c \
      'mac=$(ip link show | grep "link/ether"); \
       mac=($mac); \
       SAVE_IFS=$IFS; IFS=":"; mac=(${mac[1]}); IFS=$SAVE_IFS; \
       mac6="${mac[3]}${mac[4]}${mac[5]}"; \
       echo "Setting host name to ${mac6}."; \
       echo "${mac6}" > /etc/hostname'
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

Remember, this will override the hostname supplied by the user during installation.

Revision history for this message
atul kayastha (kayastha-jr) said (last edit ):
#3

Thank you for the reply. But i want my image to install directly without asking to try ubuntu. And for this direct installation i am looking for a script without using ubiquity. Is that possible?

Revision history for this message
Best Cubic PPA (cubic-wizard) said :
#4

You don't need Ubiquity to install Ubuntu.
You can use another installer.

You may still be able to use preseeding to automate your install.
See https://wiki.debian.org/DebianInstaller/Preseed

If you are not using ubiquity, but the debian installer that relies on preseed files, note that the correct debian command is "d-i preseed/late_command" instead of "ubiquity ubiquity/success_command".

Whatever installer you decide to use, you can still use the bash commands from comment # 2 to update your host name. Just make sure your installation script modifies /etc/hostname.

Revision history for this message
atul kayastha (kayastha-jr) said :
#5

Thanks Cubic PPA, that solved my question.