Changing the hostname on a Linux Mint desktop to a random value at boot time

When changing the hostname to a random set of alphanumeric characters on a machine running Linux Mint(17.1 Rebecca, Cinnamon 64-bit) I encounter some problems.

The following script changes the hostname to a random set of alphanumeric characters:

#!/bin/bash

### BEGIN INIT INFO
# Provides:          randomhostname
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Changing hostname to random value
# Description:       Changing hostname to random value
### END INIT INFO

old=$(cat /etc/hostname)
new=$(tr -dc 'A-Z0-9' < /dev/urandom | head -c12)
sed -i "s/$old/$new/g" /etc/hosts
sed -i "s/$old/$new/g" /etc/hostname

exit 0

Ideally the change of hostname would be achieved without restarting the system, but since /etc/init.d/hostname.sh start has to be executed afterwards and that file isn’t present in this version of Linux Mint(not sure about other version), I have to reboot the system for the changes to take effect. So doing this at boot time seems like the next best option for me at the moment.

I moved the file to the /etc/init.d/ directory and made it executable with sudo chmod +x randomhostname. After that I added it to the boot sequence with sudo update-rc.d randomhostname defaults.

When I restart and the system and login to Linux Mint I can see the change of the hostname in /etc/hostname and /etc/hosts, which in this case is YKNG7Z4PJD7H. However, I don’t see this when using the terminal(i.e. username@BFZJ7LB0BKAD) and I get the following message when using sudo:

username@BFZJ7LB0BKAD ~ $ sudo true
sudo: unable to resolve host BFZJ7LB0BKAD

I presume this problem is caused by the fact that the script is executed on the wrong time(maybe after certain services have already been started) and the system has yet to be rebooted again for the previous changes to take effect.

What is considered the correct way of adding a script like this to the boot sequence so that the changes take effect without rebooting another time? Or is there a better way to achieve this?

I would really appreciate any help regarding this question.

Solution
As roaima pointed out, I only had to add hostname "$new" at the end of the script to make the changes work. I also added stopping and starting of the network-manager service to force the client register with the network using the new hostname.

#!/bin/bash

### BEGIN INIT INFO
# Provides:          randomhostname
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Changing hostname to random value
# Description:       Changing hostname to random value
### END INIT INFO

service network-manager stop

old=$(cat /etc/hostname)
new=$(tr -dc 'A-Z0-9' < /dev/urandom | head -c12)
sed -i "s/$old/$new/g" /etc/hosts
sed -i "s/$old/$new/g" /etc/hostname
hostname "$new"

service network-manager start

exit 0
Asked By: H de Vries

||

You don’t appear to be setting the hostname after rewriting /etc/hostname. This should do the trick, added after the two sed lines:

hostname "$new"

If you’re using DHCP and this hasn’t resolved the problem, take a look at I changed my hostname, why is my bash PS1 prompt unchanged to see if that’s at all relevant to your situation.

“Rebecca” uses systemd, and the file that is used to set the hostname is /etc/init/hostname.conf. How to add your change hostname script into systemd is something I’m going to leave for another Question (more commonly known as “an exercise for the Reader”).

Answered By: roaima

On systemd you can use: hostnamectl set-hostname $new

Answered By: user365559

you can change the line

new=$(tr -dc 'A-Z0-9' < /dev/urandom | head -c12)

for this one to make random hostname from 6 to 20 characters

new=$(tr -dc 'A-Z0-9' < /dev/urandom | head -c$((6 + $RANDOM % 20)))
Answered By: Jose Gabriel Marmol
Categories: Answers Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.