Change ComputerName, Host, & LocalHost to Current UserName - DONE!

Contributor II

After some digging around I got this to work - might as well share with the nation.



username=$(id -un)
scutil --set LocalHostName $username
scutil --set ComputerName $username
scutil --set HostName $username

Thoughts? Comments?


Honored Contributor

I would say just be careful if multiple users are logged in. From the man page, "The id utility displays the user and group names and numeric IDs, of the calling process, to the standard output." I haven't tested it but you should see what happens if multiple accounts are logged in to see what it pulls. And more importantly, what it pulls if you run that in a script via Casper.

New Contributor III


This is the way I've traditionally use to get the logged in user isr:
/usr/bin/w | grep console | awk '{ print $1 }'

But the following is what are the cool kids are recommending now:
loggedInUser=python -c 'from SystemConfiguration import SCDynamicStoreCopyConsoleUser; import sys; username = (SCDynamicStoreCopyConsoleUser(None, None, None) or [None])[0]; username = [username,""][username in [u"loginwindow", None, u""]]; sys.stdout.write(username + " ");'

Hey, Balmesto!


Valued Contributor

What Zoocoup mentioned is the Apple recommended method which supports a variety of environments, for example if you have multiple users per computer or use fast user switching.

May not be applicable to your situation since it sounds like you have 1:1 computers, but we all know technically correct is the best kind of correct!

Since it's just copy pasta I resort to it in my scripts.

Also, since these computers have jamfFramework on them, you can use the built-in setComputerName option. From my DEP enrollment script:

#JAMF where you at
jamfbinary='/usr/bin/which jamf'

# figure out the user
user=`python -c 'from SystemConfiguration import SCDynamicStoreCopyConsoleUser; import sys; username = (SCDynamicStoreCopyConsoleUser(None, None, None) or [None])[0]; username = [username,""][username in [u"loginwindow", None, u""]]; sys.stdout.write(username + "

#figure out the user's full name
name=$(finger $user | awk -F: '{ print $3 }' | head -n1 | sed 's/^ //' )

# get first initial
finitial="$(echo $name | head -c 1)"

# get last name
ln="$(echo $name | cut -d   -f 2)"

# add first and last together

# clean up un to have all lower case
hostname=$(echo $un | awk '{print tolower($0)}')

$jamfbinary setComputerName -name $hostname



Just wondering what version of CocoaDialog that you use to make this work, stable or development?

Valued Contributor

@rquigley I deploy the development release, you can follow issues here.

New Contributor

Thanks for starting this thread bwiessner! Super useful.



I've been trying to get the full username with id -F but unfortunately jamf runs the script as sudo so it returns System Administrator, whatever user account i'm on. And I'm actually trying to reach for that user full Name, the one you get if you do the id -F without sudo.

So I found a workaround using finger command and some grep;
fullname=$(finger $name | grep Name | awk -F "Name: " '{print $2}' | grep -v Service)

But it only works on the computer itself, when i do it manually step by step, i manage to get the result I want to have. But when the sricpt is run through Jamf it looks like no name is reached.

Do you guys have any clue about this ?

Valued Contributor

@fdeltesta did you try my script above? Looks like you're trying to do what I've done with my script.


@adamcodega Hey, unfortunately i haven't pushed my investigations any further on a script to change the name.

I actually found a easier method, which is to create a policy that updates the computer name at every check-in. Thus you can simply manually change the name to whatever you want through the jamf inventory. The policy will catch the update instead of having the invetory update overwrite your changes.

And apparently you can even do it with jamf remote, but i havent tested it yet.