Skip to main content
Question

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


Forum|alt.badge.img+15

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

```

!/bin/sh

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

Thoughts? Comments?

9 replies

bpavlov
Forum|alt.badge.img+18
  • Esteemed Contributor
  • 1206 replies
  • December 11, 2015

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.


Forum|alt.badge.img+9
  • Contributor
  • 21 replies
  • December 11, 2015

Hello,

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!

jkb


acodega
Forum|alt.badge.img+15
  • Valued Contributor
  • 383 replies
  • December 12, 2015

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:

#!/bin/sh
#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
un=($finitial$ln)

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

$jamfbinary setComputerName -name $hostname

Forum|alt.badge.img+11
  • Contributor
  • 46 replies
  • December 14, 2015

@adamcodega

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


acodega
Forum|alt.badge.img+15
  • Valued Contributor
  • 383 replies
  • December 15, 2015

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


Forum|alt.badge.img
  • New Contributor
  • 1 reply
  • April 27, 2017

Thanks for starting this thread bwiessner! Super useful.


Forum|alt.badge.img+7
  • Contributor
  • 31 replies
  • May 15, 2018

Hey,

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 ?


acodega
Forum|alt.badge.img+15
  • Valued Contributor
  • 383 replies
  • May 15, 2018

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


Forum|alt.badge.img+7
  • Contributor
  • 31 replies
  • August 16, 2018

@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.


Reply


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings