Resetting machine id on debian

From das_wiki
Jump to: navigation, search

You need a script, and you need a systemd unit file.

Script

Create a a script called refresh_machine_id in /usr/local/bin or somewhere else in $PATH. You can do
$ echo $PATH
to see your options. Put this in the script:


#!/bin/sh
# 
# refresh_machine_id
# ------------------
# Sets a new machine ID.
# 
# Some ideas from 
#  github.com/vzhestkov/reset-machine-id/blob/master/reset-host-machine-id.sh
# 
# author: David

TRIGGER_FILE="/RESET-HOST-MACHINE-ID"

if [ -f "${TRIGGER_FILE}" ]; then
   rm "${TRIGGER_FILE}"
   mv /etc/machine-id /home/YOUR_USERNAME_HERE/etc-machine-id-backup
   mv /var/lib/dbus/machine-id /home/YOUR_USERNAME_HERE/dbus-machine-id-backup

   dbus-uuidgen --ensure
   systemd-machine-id-setup

   systemctl disable refresh-machine-id.service
   systemctl daemon-reload

   reboot
fi

This makes backup copies of your old machine-id (be sure to change YOUR_USERNAME_HERE in the above code), changes your machine-id, then disables the service. You can leave the service enabled, if you want to, by removing the two systemctl lines. In that case, you can just use the trigger file to reset the machine ID on the next boot, rather than having to re-enable the service each time.

Next give the script to root, and fix permissions:

$ script="/put/the/path/here/refresh_machine_id"; chown root:root "$script"; chmod 0700 "$script"

You need to do this since you're modifying directories that ordinary users can't modify, and issuing commands with systemctl.

Unit File

Next you need to make the unit file so that systemd will call your script once your system starts. Go to /etc/systemd/system, where administrator-added unit files go (unit files from packages go to /lib/systemd/system), and create a file called refresh-machine-id.service. Put this in there:

[Unit]                                                                          
Description=Refresh the machine ID based on trigger file /RESET-HOST-MACHINE-ID 
Wants=basic.target                                                              
After=basic.target dbus-org.freedesktop.hostname1.service systemd-hostnamed.service
                                                                                
[Service]                                                                       
Type=oneshot                                                                    
RemainAfterExit=true                                                            
ExecStart=/put/the/path/here/refresh_machine_id                             
                                                                                
[Install]                                                                       
WantedBy=multi-user.target                                                      

This runs the script on start, and waits until it completes.

Install the service

Do a couple of things to get the service running.

$ sudo systemctl daemon-reload
$ sudo systemctl enable refresh-machine-id.service

Check that it's loaded OK with

$ sudo systemctl status refresh-machine-id.service

Add the trigger file

The last thing we need to do before running this thing is add the trigger file in the root directory (/) that the script looks for before resetting the machine-id. That's easy:

$ sudo touch /RESET-HOST-MACHINE-ID

Now we are ready to try it.

Check that it worked

Take note of the values of the machine ID (they should match):

$ cat /etc/machine-id
$ cat /var/lib/dbus/machine-id

Now restart your system. The boot will get to your script, change the machine-id with your script, and reboot. The next boot will be normal. Repeat the commands above to see that the machine-id has changed, and check to be sure that /RESET-HOST-MACHINE-ID no longer exists. Voilá!