3 Answers
3

On the Internet, including local networks, machines call each other by IP addresses. In order to access machine B from machine A using the name of machine B, machine A has to have some way to map the name of B to its IP address. There are three ways to declare machine names on A:

a hosts file. This is a simple text file that maps names to addresses.

the domain name system (DNS). This is the method used on the global Internet. For example, when you load this page in a browser, the first thing your computer does is to make a DNS request to know the address of unix.stackexchange.com.

other name databases such as NIS, LDAP or Active Directory. These are used in some corporate networks, but not very often (many networks that use NIS, LDAP or AD for user databases use DNS for machine names). If your network uses one of these, you have a professional network administrator and should ask him what to do.

There are many ways in which these can work in practice; it's impossible to cover them all. In this answer, I'll describe a few common situations.

Hosts file

The hosts file method has the advantage that it doesn't require any special method. It can be cumbersome if you have several machines, because you have to update every machine when the name of one machine changes. It's not suitable if the IP address of B is assigned dynamically (so that you get a different one each time you connect to the network).

A hosts file is a simple list of lines mapping names to IP addresses. It looks like this:

On unix systems, the hosts file is /etc/hosts. On Windows, it's c:\windows\system32\drivers\etc\hosts. Just about every operating system that you can connect to the Internet has a similar file; Wikipedia has a list.

To add an entry for B in the hosts file of A:

Determine the IP address of B. On B, run the command ifconfig (if the command is not found, try /sbin/ifconfig). The output will contain lines like this:

DHCP+DNS on home or small office networks

This method is by far the simplest if you have the requisite equipment. You only need to configure one device, and all your computers will know about each other's names. This method assumes your computers get their IP addresses over DHCP, which is a method for computers to automatically retrieve an IP address when they connect to the network. If you don't know what DHCP is, they probably do.

If your network has a home router, it's the best place to configure names for machines connected to that router. First, you need to figure out the MAC address of B. Each network device has a unique MAC address. On B, run the command ifconfig -a (if the command is not found, try /sbin/ifconfig -a). The output will contain lines like this:

eth1 Link encap:Ethernet HWaddr 01:23:45:67:89:ab

In this example the MAC address is 01:23:45:67:89:ab. You must pick the HWaddr line that corresponds to the network port that's connected to the router via a cable (or the wifi card if you're connected over wifi). If you have several entries and you don't know which is which, plug the cable and see which network device receives an IP address (inet addr line just below).

Now, on your router's web interface, look for a setting like “DHCP”. The name and location of the setting is completely dependent on the router model, but most have a similar set of basic settings. Here's what it looks like on a Tomato firmware:

Enter the MAC address, an IP address and the desired name. You can pick any IP address on your local network's address range. Most home routers are preconfigured for an address range of the form 192.168.x.y or 10.x.y.z. For example, on the Tomato router shown above, in the “Network” tab, there's a “router IP address” setting with the value 10.3.0.1 and a “subnet mask” setting with the value 255.255.255.0, which means that computers on the local network must have an address of the form 10.3.0.z. There's also a range of addresses for automatically assigned DHCP addresses (10.3.0.129–10.3.0.254); for your manually assigned DHCP address, pick one that isn't in this range.

Now connect B to the network, and it should get the IP address you specified and it'll be reachable by the specified name from any machine in the network.

Make your own DNS server with Dnsmasq

If you don't have a capable home router, you can set up the same functionality on any Linux machine. I'll explain how to use Dnsmasq to set up DNS. There are many other similar programs; I chose Dnsmasq because it's easy to configure and lightweight (it's what the Tomato router illustrated above uses, for example). Dnsmasq is available on most Linux and BSD distributions for PCs, servers and network equipment.

Pick a computer that's always on, that has a static IP address, and that's running some kind of Linux or BSD; let's call it S (for server). On S, install the dnsmasq package (if it's not already there). Below I'll assume that the configuration file is /etc/dnsmasq.conf; the location may vary on some distribution. Now you need to do several things.

Tell Dnsmasq to serve your host names in addition to the ones it gets from the Internet. The simplest way is to enter the names and IP addresses in /etc/hosts (see the “Hosts file” section above), and make sure that /etc/dnsmasq.conf does not have the no-hosts directive uncommented. (Lines that begin with a # are commented out.) You can put the names in a different file; if you do, put a line addn-hosts=/path/to/hosts/file in /etc/dnsmasq.conf.

Tell Dnsmasq how to obtain IP addresses for names of machines on the Internet.

If you're running Debian, Ubuntu or a derivative, install the resolvconf package. In most common cases, everything will work out of the box.

If your network administrator or your ISP gave you the addresses of DNS servers, enter them in /etc/dnsmasq.conf, for example:

server=8.8.8.8
server=8.8.4.4

If you don't know what your current DNS settings are, look in the file /etc/resolv.conf. If you see a line like nameserver 8.8.8.8, put a line server=8.8.8.8 in /etc/dnsmasq.conf.
After you've changed /etc/dnsmasq.conf, restart Dnsmasq. The command to do that depends on the distribution; typical possibilities include restart dnsmasq or /etc/init.d/dnsmasq restart.

Tell S to use the Dnsmasq service for all host name requests. Edit the file /etc/resolv.conf (as root), remove every nameserver line, and put nameserver 127.0.0.1 instead.

If you're using resolvconf on Debian or Ubuntu, the /etc/resolv.conf may be suboptimal if you installed the resolvconf package with the network up and running. Make sure that the files base, head and tail in the /etc/resolvconf/resolv.conf.d/ directory don't contain any nameserver entries, then run resolvconf -u (as root).

Tell the other machines to use S as the DNS server. Edit /etc/resolv.conf and replace all nameserver lines with a single nameserver 10.3.0.2 where 10.3.0.2 is the IP address of S (see above for how to find out S's IP address).

You can also use Dnsmasq as a DHCP server, so that machines can obtain the address corresponding to their name automatically. This is beyond the scope of this answer; consult the Dnsmasq documentation (it's not difficult). Note that there can only be a single DHCP server on a given local network (the exact definition of local network is beyond the scope of this answer).

Names on the global Internet

So far, I've assumed a local network. What if you want to give a name to a machine that's in a different corner of the world? You can still use any of the techniques above, except that the parts involving DHCP are only applicable within a local network. Alternatively, if your machines have public IP addresses, you can register your own public name for them. (You can assign a private IP address to a public name, too; it's less common and less useful, but there's no technical difficulty.)

Getting your own domain name

You can get your own domain name and assign IP addresses to host names inside this domain. You need to register the domain name with a domain name provider; this typically costs $10–$15/year (for the cheapest domains). Use your domain name provider's web interface to assign addresses to host names.

Dynamic DNS

If your machines have a dynamic IP address, you can use the dynamic DNS protocol to update the IP address associated to the machine's name when the address changes. Not all domain name providers support dynamic DNS, so shop before you buy. For personal use, DynDNS provides a free dynamic DNS service, if you use their own domains (e.g. example.dyndns.org).

Make your own DNS server with Dnsmasq: how can I tell to other machine to use S, when the other machine have Windows installed
–
Radu RădeanuApr 27 '13 at 13:10

@Radu You can change the DNS servers associated with a connection through the control panel somewhere. I think you need to pull up the connection properties of the network interface.
–
GillesApr 27 '13 at 14:26

Computers don't just magically know what hostnames belong to what IP addressees. Even on localhost, there is some kind of lookup involved.

You will need to configure your other systems to use some kind of name lookup service. This can be /etc/hosts on the client, ldap, nsswitch, or normal DNS servers. I use bind and enter all local machines inside a local domain, then have it serve DNS for that site.

Should I run this on the client or host?
–
TshepangJul 18 '11 at 21:15

The first bit would be done on the dhcp server (and you would want to run service restart dhcpd). The second part would be done on the client, and on most distros now should be run as service networking restart.
–
CalebJul 18 '11 at 21:54

As I remember I only need to run this on client side..
–
LanceBaynesJul 19 '11 at 5:11