Rename Linux and MacOS devices in Intune

A consistent naming convention is important for efficient management within an IT environment, as it significantly helps us in the organization and clarity of device identification. This approach allows for the straightforward recognition of various aspects of a device, including its type, whether it is personally owned or a corporate asset, and its associated location or department. This kind of clear setup is really key to making sure everything runs smoothly and efficiently.

Furthermore, in the context of large organizations, where the volume of devices can be considerable, a consistent naming convention simplifies the overall management of these devices. It facilitates quick identification, making it much easier to group devices for essential tasks such as applying updates, implementing policies, or conducting troubleshooting. This streamlined approach to device management not only saves time but also enhances the effectiveness of the IT.

I have created two scripts that will help to rename linux and macOS devices that are managed by Intune. We will deploy them both and also log the process in a seperate hostname.log file. In example I took the approach to rename the devices to “LT-%SERIAL” where LT stands for Laptop and SERIAL is the serialnumber but only to a maximum of 8 digits as the character limitations of device names in Intune is only 15 characters (max length). You can change the variables in the script and rename them to something else e.g. location or department.

Renaming Linux Devices in Intune

The script:

#!/bin/bash

# Define the log file location
LOG_FILE="/var/log/hostname.log"

# Check if the dmidecode command is available
if ! command -v dmidecode &> /dev/null
then
    echo "$(date "+%Y-%m-%d %H:%M:%S") - dmidecode command not found, cannot retrieve serial number" | sudo tee -a "$LOG_FILE"
    exit 1
fi

# Retrieve the serial number of the machine
FULL_SERIAL_NUMBER=$(sudo dmidecode -s system-serial-number)

# Use the last 6 characters of the serial number
SERIAL_NUMBER=${FULL_SERIAL_NUMBER:(-8)}

# Set the hostname
sudo hostnamectl set-hostname "LT-$SERIAL_NUMBER"

# Log the outcome
if [ $? -eq 0 ]; then
    echo "$(date "+%Y-%m-%d %H:%M:%S") - Successfully changed hostname to LT-$SERIAL_NUMBER" | sudo tee -a "$LOG_FILE"
else
    echo "$(date "+%Y-%m-%d %H:%M:%S") - Failed to change hostname" | sudo tee -a "$LOG_FILE"
fi

You can also find the script here: https://github.com/ugurkocde/Intune/blob/main/DeviceRename/linux_devicerename.sh

How does the script work?

  1. Define Log File Location: The script sets a variable LOG_FILE to specify the location of the log file (/var/log/hostname.log). This file is used to log the operations carried out by the script.
  2. Check for dmidecode Command: The script checks if the dmidecode command is available on the system. This command is used to retrieve hardware information from the system BIOS, including the serial number. If dmidecode is not found, the script logs an error message to LOG_FILE and then terminates (exit 1).
  3. Retrieve the Full Serial Number: If dmidecode is available, the script uses it to retrieve the full serial number of the system and stores it in the variable FULL_SERIAL_NUMBER.
  4. Extract Serial Number: The script then extracts the last 8 characters from the full serial number and stores them in the variable SERIAL_NUMBER.
  5. Set Hostname: It uses the hostnamectl command to set the system’s hostname to LT- followed by the extracted serial number. This is a typical practice for managing and identifying systems in a network.
  6. Log the Outcome: After attempting to set the hostname, the script checks if the operation was successful ($? -eq 0). If successful, it logs a message stating the hostname was successfully changed, including the new hostname. If the operation fails, it logs a failure message.

Step-by-Step Guide to Rename Linux Devices

  1. Go to https://intune.microsoft.com/#view/Microsoft_Intune_DeviceSettings/DevicesLinuxMenu/~/configPolicies.
  2. Click on Add.
  3. Give it a Name and click on Next.
  4. Now on the settings page select and upload the script as follows:

5. Assign and Save the Configuration Script.

6. Done 🙂

Logging

The logging on the device takes place in the following path: /var/log/hostname.log

Renaming macOS Devices in Intune

The script:

#!/bin/bash

# Define the log file location
LOG_FILE="/var/log/hostname.log"

# Retrieve the serial number of the Mac
FULL_SERIAL_NUMBER=$(system_profiler SPHardwareDataType | awk '/Serial/ {print $4}')

# Use the last 6 characters of the serial number
SERIAL_NUMBER=${FULL_SERIAL_NUMBER:(-8)}

# Set the computer name, host name, and local host name
sudo scutil --set ComputerName "LT-$SERIAL_NUMBER"
sudo scutil --set HostName "LT-$SERIAL_NUMBER"
sudo scutil --set LocalHostName "LT-$SERIAL_NUMBER"

# Log the outcome
if [ $? -eq 0 ]; then
    echo "$(date "+%Y-%m-%d %H:%M:%S") - Successfully changed names to LT-$SERIAL_NUMBER" | sudo tee -a "$LOG_FILE"
else
    echo "$(date "+%Y-%m-%d %H:%M:%S") - Failed to change names" | sudo tee -a "$LOG_FILE"
fi

You can also find the script here: https://github.com/ugurkocde/Intune/blob/main/DeviceRename/macos_devicerename.sh

How does the script work?

  1. Log File Definition: The script starts by defining a log file, hostname.log, located at /var/log/. This file will be used to record the outcomes of the script’s execution.
  2. Retrieving the Serial Number: It uses the system_profiler SPHardwareDataType command to fetch hardware details of the Mac, specifically extracting the serial number using awk. The full serial number is stored in the variable FULL_SERIAL_NUMBER.
  3. Extracting Part of the Serial Number: The script then captures the last 8 characters of the serial number and stores them in the variable SERIAL_NUMBER.
  4. Setting Computer Names: Using the scutil command with sudo (superuser privileges), it sets three system properties:
    • ComputerName: The name used to identify the computer in GUIs.
    • HostName: The name used on the network.
    • LocalHostName: The local (Bonjour) host name.
    All these properties are set to “LT-” followed by the extracted part of the serial number.
  5. Logging the Outcome:
    • If the name setting commands succeed (indicated by $? -eq 0, where $? is the exit status of the last command), it logs a success message with the new names and the current date and time.
    • If any command fails, it logs a failure message.
    These logs are appended to the previously defined log file using tee -a, which allows both appending to the file and displaying the output.

Step-by-Step Guide to Rename MacOS Devices

  1. Go to https://intune.microsoft.com/#view/Microsoft_Intune_DeviceSettings/DevicesMacOsMenu/~/shellScripts.
  2. Click on Add.
  3. Give it a Name and click on Next.
  4. Now on the settings page select and upload the script as follows:

5. Assign and Save the Configuration Script.

6. Done 🙂

Logging

The logging takes place in the following path: /var/log/hostname.lo

Conclusion

Renaming devices in Intune might seem like a small thing, but it’s a powerful step towards keeping your IT environment tidy and under control. Embrace a good naming convention, and you’ll be amazed at how much easier it makes your life. Happy managing, and here’s to an organized and efficient tech world!

Any suggestions or questions? Please message me on Twitter: UgurKocDe

1 thought on “Rename Linux and MacOS devices in Intune”

Comments are closed.