This may need to change depending on your network interface
Manual Process
# Only friendly names networksetup -listallnetworkservices # Useful information networksetup -listallhardwareports
Running the above will display something like this
An asterisk (*) denotes that a network service is disabled. Ethernet FireWire Wi-Fi Bluetooth PAN Thunderbolt Bridge
# Optional but a good start, clear the custom or DHCP DNS sudo networksetup -setdnsservers Wi-Fi empty # Statically assign Google DNS sudo networksetup -setdnsservers Wi-Fi 8.8.8.8 8.8.4.4 # Clear DNS Cache sudo killall -HUP mDNSResponder
scutil --dns | grep 'nameserver\[[0-9]*\]'
More Automatic Process
Prerequisite 1: Optional but useful, this script originally developed by VMWare has been modified to get some basic
identification for linux based computers. More details at Bash – Unix Identification
curl -O https://gitlab.com/Kittell-Projects/Unix/unixidentification/raw/master/UnixIdentification.sh
Prerequisite 2: The script has some different options
curl -O https://gist.githubusercontent.com/dkittell/34fe7b7422323fba546948b7448933a5/raw/69924ace7c922aab2e18487514f58baee7ea3554/SetDNS.sh
Basic parameters of the script
- No parameters passed, you will get information like below
sh SetDNS.sh
KMacMini -------------- Computer OS: Mac OS X - High Sierra 10.13.6 Computer Name: KMacMini Current User Name: dkittell Serial Number: <Serial Number of Mac> Remote IP v4 Address: <Public IP> Active Network Port(s): Interface: Ethernet (en0) MAC Address: 3c:07:54:0b:f3:9e (Apple Inc.) IP Address v6: fe80::875:3f99:1e31:d84a IP Address v4: 10.40.20.4 Subnet Mask: 255.255.255.0 Router: 10.40.20.1 IP CIDR: 10.40.20.4 /24 Network Speed: 1.00 Gbps Link quality: 100 (good) DNS Server: Set With DHCP -------------- Network Name: <Wi-Fi Name> Interface: Wi-Fi (en1) MAC Address: 20:c9:d0:8d:6a:1b (Apple Inc.) IP Address v6: fe80::a9:e7bf:6761:11e3 IP Address v4: 10.40.20.222 Subnet Mask: 255.255.255.0 Router: 10.40.20.1 IP CIDR: 10.40.20.222 /24 Network Speed: 5.74 Mbps Link quality: 100 (good) DNS Server: Set With DHCP --------------
-
Set DNS parameter, note if you select something other than DHCP the IPv4 IP(s) will be checked to make sure they are valid
- To set DHCP run the command below and it will set the DNS to DHCP (automatic)
sh SetDNS.sh dhcp
- To set CleanBrowsing my chosen home DNS
sudo sh SetDNS.sh home # or sudo sh SetDNS.sh cleanbrowsing
- To set OpenDNS
sudo sh SetDNS.sh opendns
- To set Google DNS
sudo sh SetDNS.sh google
$ sudo sh SetDNS.sh google KMacMini -------------- Computer OS: Mac OS X - High Sierra 10.13.6 Computer Name: KMacMini Current User Name: root Serial Number: <Serial Number of Mac> Remote IP v4 Address: <Public IP> Active Network Port(s): Interface: Ethernet (en0) MAC Address: 3c:07:54:0b:f3:9e (Apple Inc.) IP Address v6: fe80::875:3f99:1e31:d84a IP Address v4: 10.40.20.4 Subnet Mask: 255.255.255.0 Router: 10.40.20.1 IP CIDR: 10.40.20.4 /24 Network Speed: 1.00 Gbps Link quality: 100 (good) DNS Server: Set With DHCP Validating 8.8.8.8 is accessible... 1 Validating 8.8.4.4 is accessible... 1 DNS Updated DNS Server: 8.8.8.8 8.8.4.4 Validating 8.8.8.8 is accessible... 1 Validating 8.8.4.4 is accessible... 1 DNS Updated DNS Server: 8.8.8.8 8.8.4.4 2001:4860:4860::8888 2001:4860:4860::8844 -------------- Network Name: <Wi-Fi Name> Interface: Wi-Fi (en1) MAC Address: 20:c9:d0:8d:6a:1b (Apple Inc.) IP Address v6: fe80::a9:e7bf:6761:11e3 IP Address v4: 10.40.20.222 Subnet Mask: 255.255.255.0 Router: 10.40.20.1 IP CIDR: 10.40.20.222 /24 Network Speed: 5.74 Mbps Link quality: 100 (good) DNS Server: Set With DHCP Validating 8.8.8.8 is accessible... 1 Validating 8.8.4.4 is accessible... 1 DNS Updated DNS Server: 8.8.8.8 8.8.4.4 2001:4860:4860::8888 2001:4860:4860::8844 --------------
- To set DHCP run the command below and it will set the DNS to DHCP (automatic)
Automatic Process Script
#!/bin/sh # SetDNS.sh # # Created by David Kittell on 6/14/19. # # For updates download # curl -O https://gist.githubusercontent.com/dkittell/34fe7b7422323fba546948b7448933a5/raw/69924ace7c922aab2e18487514f58baee7ea3554/SetDNS.sh # Variables - Start sExternalIPService="http://dns.kittell.net/ip.php" sExternalMACALService="http://dns.kittell.net/macaltext.php?address=" # Get remote/public IP address remoteip=$(curl -sSf -m 5 --connect-timeout 5 $sExternalIPService) # Get computer name computername=$(scutil --get ComputerName) # Get serial number sSerialNumber=$(system_profiler SPHardwareDataType |grep "Serial Number (system)" |awk '{print $4}' | cut -d/ -f1) #echo $sSerialNumber # Variables - Stop # Run Once - Start #curl -O https://gitlab.com/Kittell-Projects/Unix/unixidentification/raw/master/UnixIdentification.sh # Run Once - Stop # Functions - Start # Function to convert IP Subnet Mask to CIDR mask2cdr () { # Assumes there's no "255." after a non-255 byte in the mask local x=${1##*255.} set -- 0^^^128^192^224^240^248^252^254^ $(( (${#1} - ${#x})*2 )) ${x%%.*} x=${1%%$3*} echo $(( $2 + (${#x}/4) )) } IPUpStatus() { if ping -c 1 $1 &> /dev/null #if ping -c 1 8.8.8.8 &> /dev/null #if ping -c 1 2a0d:2a00:1:abab:23b9:93ef:2da9:dd2f &> /dev/null then rc=1 else rc=0 fi #rc=$(ping -c 1 -m 5 "$1" | grep "100.0% packet loss") echo $rc } DHCPDNS() { #sudo clear sudo networksetup -setwebproxystate "$ActiveNetworkName" Off sudo networksetup -setsecurewebproxystate "$ActiveNetworkName" Off sudo networksetup -setv6automatic "$ActiveNetworkName" sudo networksetup -setdnsservers "$ActiveNetworkName" empty sudo networksetup -setdhcp "$ActiveNetworkName" sudo networksetup -setsearchdomains "$ActiveNetworkName" empty sudo networksetup -setproxybypassdomains "$ActiveNetworkName" empty sudo dscacheutil -flushcache sudo killall -HUP mDNSResponder say "DNS Updated for $ActiveNetworkName" echo "\n DNS Updated" DHCPActive=$(networksetup -getinfo "$ActiveNetworkName" | grep DHCP) # echo $DHCPActive dnsserver=$(networksetup -getdnsservers "$ActiveNetworkName" ) # echo $dnsserver if [[ -z $dnsserver || $DHCPActive == "DHCP Configuration" ]]; then if [[ $DHCPActive ]]; then echo " DNS Server: Set With DHCP" else echo " DNS Server: Unknown" fi else echo " DNS Server: $dnsserver" fi } StaticDNS() { if [[ ! -z "$1" ]]; then echo "\nValidating $1 is accessible..." IPUpStatus "$1" if [[ $rc == 1 ]] ; then sudo networksetup -setv6automatic "$ActiveNetworkName" sudo networksetup -setdhcp "$ActiveNetworkName" empty # Disable DHCP if it's on sudo networksetup -setwebproxystate "$ActiveNetworkName" Off sudo networksetup -setsecurewebproxystate "$ActiveNetworkName" Off sudo networksetup -setdnsservers "$ActiveNetworkName" empty sudo networksetup -setsearchdomains "$ActiveNetworkName" empty sudo networksetup -setproxybypassdomains "$ActiveNetworkName" empty if [[ ! -z "$2" ]]; then echo "Validating $2 is accessible..." IPUpStatus "$1" if [[ $rc == 1 ]] ; then sudo networksetup -setdnsservers "$ActiveNetworkName" $1 $2 $3 $4 else sudo networksetup -setdnsservers "$ActiveNetworkName" $1 fi else sudo networksetup -setdnsservers "$ActiveNetworkName" $1 fi sudo dscacheutil -flushcache sudo killall -HUP mDNSResponder say "DNS Updated for $ActiveNetworkName" echo "\n DNS Updated" DHCPActive=$(networksetup -getinfo "$ActiveNetworkName" | grep DHCP) dnsserver=$(networksetup -getdnsservers "$ActiveNetworkName" | cut -d/ -f1|tr '\n' ' ') if [[ -z $dnsserver ]]; then if [[ $DHCPActive ]]; then echo " DNS Server: Set With DHCP" else echo " DNS Server: Unknown" fi else echo " DNS Server: $dnsserver" fi else echo "Supplied DNS is not valid, setting DHCP" DHCPDNS fi fi } NetworkInfo () { # List all Network ports NetworkPorts=$(ifconfig -uv | grep '^[a-z0-9]' | awk -F : '{print $1}') echo "$computername" echo "--------------" echo " Computer OS: $(sh UnixIdentification.sh)" echo " Computer Name: $computername" echo " Current User Name: $(whoami)" echo " Serial Number: $sSerialNumber" if [[ $remoteip ]]; then echo "Remote IP v4 Address: $remoteip\n" else echo "Remote IP v4 Address: Unable To Determine\n" fi echo "Active Network Port(s):" for val in $(echo $NetworkPorts); do # Get for all available hardware ports their status activated=$(ifconfig -uv "$(echo $val)" | grep 'status: ' | awk '{print $2}') # Get VPN Information - Start if [[ ":${NetworkPorts[*]}:" =~ "ppp0" ]]; then VPNActivated=$(ifconfig -uv "ppp0" | grep 'state availability' | grep 'true') if [ ! -z "VPNActivated" ]; then #echo "VPN Active" VPNipaddress=$(ifconfig -uv "ppp0" | grep 'inet' | cut -d ' ' -f2) # VPNipaddressTranslation=$(ifconfig -uv "ppp0" | grep 'inet' | cut -d ' ' -f4) #echo $VPNipaddress fi fi if [[ ":${NetworkPorts[*]}:" =~ "gpd0" ]]; then VPNActivated=$(ifconfig -uv "gpd0" | grep 'state availability' | grep 'true') if [ ! -z "VPNActivated" ]; then #echo "VPN Active" VPNipaddress=$(ifconfig -uv "gpd0" | grep 'inet' | cut -d ' ' -f2 | sed 's/^[ \t]*//;s/[ \t]*$//') #echo $VPNipaddress fi fi # Get VPN Information - Stop if [ "$activated" == "active" ]; then label=$(ifconfig -uv "$(echo $val)" | grep 'type' | awk '{print $2}') # echo $label # ActiveNetwork=$(route get default | grep interface | awk '{print $2}') # ActiveNetworkName=$(networksetup -listallhardwareports | grep -B 1 "$label" | awk '/Hardware Port/{ print }'|cut -d " " -f3- | uniq) # ActiveNetworkName=$(networksetup -listnetworkserviceorder | grep $ActiveNetwork -B1 | head -1 | cut -d ")" -f2 | sed "s/^[ \t]*//") ActiveNetworkName=$(networksetup -listnetworkserviceorder | grep "$val" -B1 | grep -v "$val" | cut -d ')' -f2 | sed 's/^[ ]*//;s/[ ]*$//') #echo $ActiveNetwork #echo $ActiveNetworkName state=$(ifconfig -uv "$val" | grep 'status: ' | awk '{print $2}') #echo $state ipaddress=$(ifconfig -uv "$val" | grep 'inet ' | awk '{print $2}' | cut -d/ -f1|tr '\n' ' ') # echo $ipaddress ipaddressv6=$(ifconfig "$val" | grep inet6 | grep 'scopeid' | cut -d ' ' -f2 | cut -d '%' -f1 | cut -d/ -f1|tr '\n' ' ') netmask=$(ipconfig getpacket "$val" | grep 'subnet_mask (ip):' | awk '{print $3}' | tr -d '[:space:]') #echo $netmask if [[ -z $(ifconfig -uv "$val" | grep 'link rate: ' | awk '{print $3, $4}' | sed 'N;s/\n/ up /' ) ]]; then networkspeed="$(ifconfig -uv "$val" | grep 'link rate: ' | cut -d ':' -f2 | sed 's/^[ \t]*//;s/[ \t]*$//')" else networkspeed="$(ifconfig -uv "$val" | grep 'link rate: ' | awk '{print $3, $4}' | sed 'N;s/\n/ up /' ) down" fi quality=$(ifconfig -uv "$val" | grep 'link quality:' | awk '{print $3, $4}') #echo $quality macaddress=$(ifconfig -uv "$val" | grep 'ether ' | awk '{print $2}') #echo $macaddress macal=$(curl -s "$sExternalMACALService$macaddress" | sed 's/^[ ]*//;s/[ ]*$//') #echo $macal router=$(ipconfig getpacket "$val" | grep 'router (ip_mult):' | sed 's/.*router (ip_mult): {\([^}]*\)}.*/\1/') #echo $router DHCPActive=$(networksetup -getinfo "$ActiveNetworkName" | grep DHCP) #echo $DHCPActive #dnsserver=$(networksetup -getdnsservers "$ActiveNetworkName" | awk '{print $1, $2}' | sed 'N;s/\n//' ) dnsserver=$(networksetup -getdnsservers "$ActiveNetworkName" | cut -d/ -f1|tr '\n' ' ') #echo $dnsserver if [[ ! -z "$netmask" ]]; then if [[ $ipaddress ]]; then # Is this a WiFi associated port? If so, then we want the network name if [ "$label" = "Wi-Fi" ]; then WiFiName=$(/System/Library/PrivateFrameworks/Apple80211.framework/Versions/A/Resources/airport -I | grep '\sSSID:' | sed 's/.*: //') #echo $WiFiName echo " Network Name: $WiFiName" fi echo " Interface: $ActiveNetworkName ($val)" echo " MAC Address: $macaddress ($macal)\n" if [[ ! -z "$ipaddressv6" ]]; then echo " IP Address v6: $ipaddressv6" fi echo " IP Address v4: $ipaddress" echo " Subnet Mask: $netmask" echo " Router: $router" echo " IP CIDR: $ipaddress/$(mask2cdr $netmask)\n" echo " Network Speed: $networkspeed" echo " Link quality: $quality" if [ ! -z "$VPNipaddress" ]; then echo " VPN IP Address: $VPNipaddress\n" fi if [[ -z $dnsserver || $DHCPActive == "DHCP Configuration" ]]; then if [[ $DHCPActive ]]; then echo " DNS Server: Set With DHCP" else echo " DNS Server: Unknown" fi else echo " DNS Server: $dnsserver" fi case "$1" in "dhcp") DHCPDNS ;; "home") # StaticDNS 10.40.20.3 # StaticDNS 185.228.168.80 185.228.169.80 # IPv4 CleanBrowsing StaticDNS 185.228.168.80 185.228.169.80 2a0d:2a00:1:abab:23b9:93ef:2da9:dd2f 2a0d:2a00:2:abab:23b9:93ef:2da9:dd2f # IPv6 CleanBrowsing ;; "cleanbrowsing") # StaticDNS 185.228.168.80 185.228.169.80 # IPv4 CleanBrowsing StaticDNS 185.228.168.80 185.228.169.80 2a0d:2a00:1:abab:23b9:93ef:2da9:dd2f 2a0d:2a00:2:abab:23b9:93ef:2da9:dd2f # IPv6 CleanBrowsing ;; "opendns") # StaticDNS 208.67.222.222 208.67.220.220 # IPv4 OpenDNS StaticDNS 208.67.222.222 208.67.220.220 2620:119:35::35 2620:119:53::53 # IPv6 OpenDNS ;; "google") # StaticDNS 8.8.8.8 8.8.4.4 # IPv4 OpenDNS StaticDNS 8.8.8.8 8.8.4.4 2001:4860:4860::8888 2001:4860:4860::8844 # IPv6 OpenDNS ;; *) sConfig="0" ;; esac echo "--------------" fi fi fi done } # Functions - Stop # Check Online Status - Start #nc -z 8.8.8.8 53 >/dev/null 2>&1 #online=$? #if [ $online -eq 0 ]; then # echo "Online, continuing...\n" # NetworkInfo $(echo $1 | awk '{print tolower($0)}') # # remoteip=$(dig +short myip.opendns.com @resolver1.opendns.com) # echo "Remote IP: $remoteip" # #else # echo "Sorry you are offline, please connect and try again." # exit #fi # Check Online Status - Stop
Last Updated on June 4, 2020