MacOS X – Associating a name to an IP Address
What you should know
Please note: this is only valid until MacOSX 10.4, NOT for MacOSX 10.5 (Leopard)
MacOSX is a BSD system, the first thing that we think is:
"Easy, I'll change my files
/etc/hosts"
This is a good thought, but unfortunately by default on OS X, this file is never taken into account.
Name resolution via the NetInfo Manager. There are several methods to ensure that the file
/etc/hosts should be taken into account before or after the NetInfo Manager, but since NetInfo works well, as much use, especially as it simplifies troubleshooting when all information is in one place!
Use this step
Here is the method in command mode that is not so complicated and has the merit that it can be done without necessarily having physical access to the machine (via
SSH).
Everything goes from a bash terminal (
/Applications/Utilities/Terminal)
It must first retrieve the current configuration:
$ mkdir -p ~/hosts
$ cd ~/hosts
$ nidump hosts / > hosts.conf.ori
Copy the file to retain the original configuration (. Ori)
$ Cp hosts.conf.ori hosts.conf
Again, make the desired changes in the hosts.conf.
You should add a line and not to modify existing lines unless you know what you are doing!
Once modified, you must reload the file into NetInfo (as root, so using sudo)
$ sudo niload hosts . < hosts.conf
If everything goes without error (otherwise, you must either correct the error in the file hosts.conf or redo the manip with. Ori), restarting the search service name:
$ sudo killall -HUP lookupd
Now you have to test it
Example
Lets make a test: Associating www.ccm.net to www.commentcamarche.net (194.169.240.130)
$ ping www.ccm.net
ping: cannot resolve www.ccm.net: Unknown host
$ mkdir -p ~/hosts
$ cd ~/hosts
$ nidump hosts / > hosts.conf.ori
$ cp hosts.conf.ori hosts.conf
$ cat hosts.conf
::1 localhost
255.255.255.255 broadcasthost
127.0.0.1 java.localhost
127.0.0.1 php.localhost
127.0.0.1 mysql.localhost
$ echo "194.169.240.130 www.ccm.net" >> hosts.conf
$ cat hosts.conf
::1 localhost
255.255.255.255 broadcasthost
127.0.0.1 java.localhost
127.0.0.1 php.localhost
127.0.0.1 mysql.localhost
194.169.240.130 www.ccm.net
$ sudo niload hosts . < hosts.conf
$ sudo killall -HUP lookupd
$ ping -c4 www.ccm.net
PING www.ccm.net (194.169.240.130): 56 data bytes
64 bytes from 194.169.240.130: icmp_seq=0 ttl=56 time=29.936 ms
64 bytes from 194.169.240.130: icmp_seq=1 ttl=56 time=30.622 ms
64 bytes from 194.169.240.130: icmp_seq=2 ttl=56 time=30.053 ms
64 bytes from 194.169.240.130: icmp_seq=3 ttl=56 time=30.566 ms
--- www.ccm.net ping statistics ---
4 packets transmitted, 4 packets received, 0% packet loss
round-trip min/avg/max/stddev = 29.936/30.294/30.622/0.303 ms
And now, to remove this definition, it is not enough to look through niload file
hosts.conf.ori. Indeed, in addition niload works.
Here's how to use the command niutil to delete the entry
www.ccm.net
$ sudo niutil -destroy . /machines/www.ccm.net
Remember lookupd reload after each change! (
sudo killall-HUP lookupd)