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
- 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.
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:
127.0.0.1 localhost localhost.localdomain
198.51.100.42 darkstar darkstar.bands
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:
eth1 Link encap:Ethernet HWaddr 01:23:45:67:89:ab
inet addr:10.3.1.42 Bcast:10.3.1.255 Mask:255.255.255.0
In this example, the IP address of B is 10.3.1.42. If there are several
inet addr: lines, pick the one that corresponds to your network card, never the
lo entry or a tunnel or virtual entry.
- Edit the hosts file on A. If A is running some unix system, you'll need to edit
/etc/hosts as the super user; see How do I run a command as the system administrator (root).
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.
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.
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, No-IP provides a free dynamic DNS service, if you use their own domains (e.g.