# 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  ## migrated from serverfault.comFeb 7 '15 at 13:46 This question came from our site for system and network administrators. • Just wanted to point out that your assignment of new can be simplified a bit: new=$(tr -dc 'A-Z0-9' < /dev/urandom | head -c12) – kasperd Feb 6 '15 at 11:22
• Thank you for that addition, I will use your method for the generation of random alphanumerical characters. – H de Vries Feb 6 '15 at 11:36
• Isn't there anyone that has knowledge about this particular subject and is able to help? – H de Vries Feb 10 '15 at 11:51
• I don't see any problems with the question, so I think the only reason it hasn't received an answer is that none of the people who viewed it knows the answer. One option you have now is to find other unanswered questions on this site which you are able to answer. 5 accepted answers or 7 votes on your answers would give you enough reputation to come back and put a bounty on this question. – kasperd Feb 15 '15 at 15:24
• @kasperd Thank you very much again for the explanation, it was very clear. Now I know which steps are necessary to take so that this question will get some more attention. – H de Vries Feb 16 '15 at 15:24

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"). • Thank you for your answer, adding hostname "$new" to the script solved the problem. I also added stopping and starting of the network-manager service to prevent any problems and to make the client register again with the router using the new hostname. Thanks for taking the time to answer my question. I should have given it more thought, as it eventually turned out to be such a simple addition that made it work. Currently I don't have enough points to upvote your answer, will upvote when I am able. – H de Vries Mar 9 '15 at 16:45
On systemd you can use: hostnamectl set-hostname \$new