Hosts File Update

soportetpco
New Contributor II

Hey I want to update my hosts file in all computers with Jamf but i dont know which commands use. The lines that i want to add are

17.171.120.82 locate.apple.com
17.171.120.18 locate.apple.com

thanks

10 REPLIES 10

allanp81
Valued Contributor

https://www.imore.com/how-edit-your-macs-hosts-file-and-why-you-would-want

In theory you can just pipe those lines into the hosts file by doing something like:

sudo echo "17.171.120.82 locate.apple.com" >> /etc/hosts

azevon
New Contributor

So I was trying to do what @allanp81 suggested...

And using my own criteria, I want to block the Amazon Kindle Reader website (and have it not resolve). This is what I'm using but I'm getting a permission denied. Anyone have any thoughts?

sudo echo 0.0.0.0 read.amazon.com >> /private/etc/hosts

I've also tried

sudo echo 0.0.0.0 read.amazon.com >> /etc/hosts

Thanks so much in advance!

donmontalvo
Esteemed Contributor II

Doesn't 0.0.0.0 mean all the IP Addresses on the local machine?

We usually point undesirable DNS addresses to our internal site.

--
https://donmontalvo.com

azevon
New Contributor

Sorry, perhaps I wasn't clear. The permission denied was in Terminal... so even now when I type:

sudo echo 54.165.176.148 read.amazon.com >> /etc/hosts

I am still getting a permission denied.

My command 0.0.0.0 does however work (and simply gives an error page which I don't mind) when using the Nano text editor, however I won't be able to use that with Jamf. Obviously I need to deploy this via a script and deploy (right?).

donmontalvo
Esteemed Contributor II

Would this work?

#!/bin/sh
# Check if there is a line in /etc/hosts containing "read.amazon.com".
# If it doesn't then add it, else it if exists replace it with new line.

if grep -q "read.amazon.com" /etc/hosts; then
    echo "Redirect exists for read.amazon.com, replacing with new..."
    # Remove line
    /usr/bin/sed -i_bak -e '/read.amazon.com/d' /etc/hosts
    # Add redirect
    echo "54.165.176.148 read.amazon.com" >> /etc/hosts
    # Reset DNS
    /usr/bin/killall -HUP mDNSResponder
else
    echo "Redirect does not exist for read.amazon.com, adding it now..."
    # Add redirect
    echo "54.165.176.148 read.amazon.com" >> /etc/hosts
    # Reset DNS
    /usr/bin/killall -HUP mDNSResponder
fi

exit 0
--
https://donmontalvo.com

azevon
New Contributor

Yes!!! Now I have just one more question. If I want this (for one of my students) to only be effective while he's in school, I know how to make the policy work during those hours. However what script would I write to make this reverse during the hours he's not in school? Basically it's ok for him to read Kindle via the browser at home, just not at school.

Thank you so much for that script. Was awesome.

Dan1987
New Contributor III

Hey, Adding lines via script works great but what about removing an exact entry?
Thanks

mschroder
Valued Contributor

To remove a line containing a pattern you can use 'sed -i~ /pattern/d /etc/hosts'. Try to make the pattern as unique as possible, but also make sure you get all potential variants.

CesarT_T
New Contributor II

anyone ever figure this out?

achilcott
New Contributor

Why wouldn't you do this on the network end? Sounds insane trying to set this for each individual device.