Posted on 11-05-2018 12:27 AM
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
Posted on 11-05-2018 12:57 AM
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
Posted on 02-05-2019 02:49 PM
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!
Posted on 02-05-2019 03:47 PM
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.
Posted on 02-09-2019 03:05 PM
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?).
Posted on 02-10-2019 10:49 AM
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
Posted on 02-11-2019 11:41 AM
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.
Posted on 05-24-2019 12:19 AM
Hey,
Adding lines via script works great but what about removing an exact entry?
Thanks
Posted on 05-24-2019 12:48 AM
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.
Posted on 03-31-2021 04:52 PM
anyone ever figure this out?
Posted on 03-31-2021 05:32 PM
Why wouldn't you do this on the network end? Sounds insane trying to set this for each individual device.
Posted on 12-24-2021 06:33 AM
I use (didn't create) script
#!/bin/bash
# insert/update hosts entry
ip_address="$4"
host_name="$5"
# find existing instances in the host file and save the line numbers
matches_in_hosts="$(grep -n $host_name /etc/hosts | cut -f1 -d:)"
host_entry="${ip_address} ${host_name}"
echo "Please enter your password if requested."
if [ ! -z "$matches_in_hosts" ]
then
echo "Updating existing hosts entry."
# iterate over the line numbers on which matches were found
while read -r line_number; do
# replace the text of each line with the desired host entry
sed -i '' "${line_number}s/.*/${host_entry} /" /etc/hosts
done <<< "$matches_in_hosts"
else
echo "Adding new hosts entry."
echo "$host_entry" | tee -a /etc/hosts > /dev/null
fi
# Flushing the DNS Cache
dscacheutil -flushcache
Posted on 12-13-2022 08:50 AM
Per the State of Texas mandate to block tiktok from all government and institutional computers, this script was a lifesaver. Thanks!
Posted on 08-10-2023 07:31 AM
Are you able to post the script you used for TikTok?
Thanks
Posted on 08-10-2023 08:12 AM
The script I use is generic, allowing you to put any new sites into the Host Entry to block any future sites.
#!/bin/bash
# insert/update hosts entry
ip_address="$4"
host_name="$5"
# find existing instances in the host file and save the line numbers
matches_in_hosts="$(grep -n $host_name /etc/hosts | cut -f1 -d:)"
host_entry="${ip_address} ${host_name}"
echo "Please enter your password if requested."
if [ ! -z "$matches_in_hosts" ]
then
echo "Updating existing hosts entry."
# iterate over the line numbers on which matches were found
while read -r line_number; do
# replace the text of each line with the desired host entry
sed -i '' "${line_number}s/.*/${host_entry} /" /etc/hosts
done <<< "$matches_in_hosts"
else
echo "Adding new hosts entry."
echo "$host_entry" | tee -a /etc/hosts > /dev/null
fi
# Flushing the DNS Cache
dscacheutil -flushcache
exit 0
And then I use a policy to define the parameters ($4 and $5):
I hope that helps!
Posted on 08-10-2023 12:36 PM
Thanks for sharing