[HOWTO] - Use niload to add hosts

kilowatt

mach-o mach-o man
Ok, here is what this trick will allow you to do:
instead of typing commands like:
ssh 192.168.1.136
and instead of connecting to servers like this:
http://192.168.1.142/test.shtml
all you have to do, with a properly set up hosts entree is something like this:
ssh macho
or
http://bigmac/test.shtml

A Host file, or host entry, allows for a simple hostname-to-ipaddress system to be set up. While not as versital as a custom DNS server, this is MUCH easier to use and maintain, especially if you only have a couple of hosts on your network.

Under linux and many unix's, one simply edits the file /etc/hosts and adds entries like this:
192.168.1.1 router.localdomain router
192.168.1.121 eroom.localdomain eroom
192.168.1.122 mach.localdomain mach
192.168.1.123 powerbook.localdomain powerbook

the localdomain deal is something I like to set up, so every host thinks its under a domain. If you don't use the localdomain stuff, don't bother with it.

Under linux, with this set up, any time you mention powerbook, or powerbook.localdomain, the computer at 192.168.1.123 is referenced.

However, under Mac OS X, we have something called 'NetInfo' which I believe is from Next. Its a little more complex, but it is quite effective and powerfull if used to its fullest extent.

Here is how to add host entires to the netinfo database easily.

First, open up the terminal, which is located in /Applications/Utilities. Its different from that application called the Console ('Console' monitors errors sent to the console log, at /var/log/system.log).

You will need to be root for this to work, and I recomend typing the following commands:
sudo -s
(enter YOUR password at the password prompt)

Now, lets just see if anything currently resides in netinfo's host database:
[mach4:/tmp] root# nidump hosts /
127.0.0.1 localhost mach4 mach4.localdomain localhost.localdomain
255.255.255.255 broadcasthost
255.255.255.255 -DHCP-
192.168.1.1 router router.localdomain
192.168.1.100 100.localdomain
192.168.1.101 101.localdomain
192.168.1.102 102.localdomain
192.168.1.103 103.localdomain
192.168.1.104 104.localdomain
192.168.1.105 105.localdomain
192.168.1.132 bigbox
[mach4:/tmp] root#
ok, looks like I allready have a few in there, no prob, netinfo is somewhat intelligent, and will allow you to non-destructively add to this db.
So, at this point, we need to create a hosts file.
pico /etc/hosts (return)
Now you have a new file in pico. Type what you wish to become your host file in here, use the example above if you want, leave out all the 255.255 stuff and 127.0.0.1, they should allready be there, so I personally wouldn't mess with them.
Now that you have a nice host file (use tab between ip and names, it looks nice), press control-x. you will be prompted to save the file, press 'y', and then press return.

Ok, now we have a file in /etc called hosts. Under Linux and mosts unix's, we would be finished. But we have to load it into netinfo under Mac OS X. Here's how:
niload hosts . < /etc/hosts
Wow, that was easy, wasn't it?
to check your entries, just type:
nidump hosts .
For some reason, nidump hosts / works as well.

you can also do this in the GUI, I'll post that in a bit.

For more information on nidump and niload, just issue one of these commands in a large terminal window:
man nidump
or
man niload
Press q to quit the man page viewer, and / to search for a string.

Hope this clears things up, and keep in mind that using a hosts file/entry can greatly speed up ssh and any other service that (stupidly) attempts to resolve hosts like 90.0.0.20 and other subnet-only ip addresses.
 
Here's how to add hosts via the GUI. I think the cli is a bit easier because its less tedius, but if you've never used the terminal before, this might not be the best thing to start with. And so I bring you the GUI method:

Launch netinfo, located at /Applications/Utilities/NetInfoManager
Under the Domain menu, select Security, and Authenticate. Type a username and password with admin prividledges.

Select, in the Directory Browser, / and then 'machines'.

Either press apple-n, the icon that looks like 'new folder' or go to the Directory menu, and select 'new sub directory'.

This should insert a flag at /machines/net_directory

Unless you are creating an entry for a host named 'new_directory' you'll want to rename it.

Select, in the 'Directory: new_directory' plane that has 'property and Value(s)' stuff the first, and only entry ("name | new_directory").

Under the 'Directory' menu, select 'Delete value'.

The entry should now read 'name | <no value>'. Which is good.

Now, enter the hostname, which can be something simple like myfileserver or as 'complex' as files.danz.quons.net or whatever.

So, select the name and <no value> line, and go to the directory menu, and select 'insert value'. (option-apple-i)
Now type the hostname.


Next, we need to input an ip address.

First, insert a new property, called 'ip_address'.
apple-shift-n or apple-shift-i (eye - look in the directory menu, you'll see it).

where it says 'new_property', click and type 'ip_address'.

Where it says 'new_value', type the ipaddress that goes with the host you inserted.

If you wish, you can enter more than one hostname, so if for example you wish matrixbaby.graphics.ddn.net and matrixbaby both to go to the same IP, just insert an additional value to the name property:

Select 'name' and under, the 'directory' menu, select 'insert value' (apple-option-i).

That should do it, close out of netinfo, and you're set. If you wish to properly name your computer, either insert/modify the entry called 'localhost' and give it a name, in addition to localhost. I think thats what I did. you can also modify the /etc/hostconfig file, change the HOSTNAME= localhost to whatever you wish.
 
After you make your changes with niload, you will also need to do a

sudo killall -HUP launchd

to make your running system update its caches with the net host information. At least, that's what I had to do.
 
Hi, I'm new to the world of Macs, so I wonder if you can help me.

In the past, on Windows then Linux, I've simply imported the hosts file from:

http://www.mvps.org/winhelp2002/hosts.htm

and reimported it as it gets updated. This hosts file is used to block connections to unwelcome sites by giving them the IP address of 127.0.0.1 (my computer). It is very large, and is in Windows/Linux format.

I have read the HOWTOs for a hosts file in Mac OSX and it seems that the hosts file is in a different format, and the directions are for adding new addresses and IP addresses one by one.

Is there any way of converting a Windows/Linux hosts file with the correct format for Mac OSX, and then importing it?
 
I don't understand, on 10.5 I not only don't have NetInfo, but also, niload and nidump aren't there either;

CrazyMcphee:~ root# niload
-sh: niload: command not found
CrazyMcphee:~ root# nidump hosts
-sh: nidump: command not found
CrazyMcphee:~ root# man niload
No manual entry for niload

Spotlight turns up nothing too.

even 'find / -name ni\* -type f' doesn't turn up anything promising either.

Very confused, as everywhere I've googled so far says to use niload and mentions nothing about 10.5 differences?!
 
Back
Top