Naming your Apple computer via the CLI

  • In an ongoing attempt to improve my OSX setup scripts I'm wanting to set the local computername, expected username and the asset tag.

    With the OSX cli I can use the scutil --set HostName & ComputerName & LocalHostName each from individual variables that are prompted for during execution.

    So for example

    read -p 'Enter a username: ' compuser sets that variable to be called in scutil --set ComputerName $compuser

    The reason for using the username for the computer name is because we name our computers based on the office, SAMAccountname (Username) and the asset tag.

    What I'm wanting to do is concatenate a few of these variables to set these so they match a nice unique format that makes it easier for binding to the domain later.

    This is how that script looks and works.

    # Setting the (office) offname variable
    read -p 'What office are you in?: ' offname
    # Setting computer user variable
    read -p 'Enter the computer username ie jdoe: ' compuser
    # Setting asset tag variable
    read -p 'Enter this computers asset tag: ' tagnumber
    scutil --set HostName $offname$compuser && scutil --set ComputerName $compuser$tagnumber && scutil --set LocalHostName %offname$compuser$tagnumber

  • I've added this to a larger script that I use, but if you only wanted to automate the naming process the above would work for you.

    Else just remove the header #!/bin/sh and add the reset to any setup scripts that you have to automate this portion of the setup.