How to make a machine accessible from the LAN using its hostname

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:

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:

  1. 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.

  2. 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:

tomato screenshot

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, No-IP provides a free dynamic DNS service, if you use their own domains (e.g. example.ddns.net).


Use Multicast DNS (mDNS). This is a zero-configuration protocol that works on LAN subnets. No server required. Uses the .local TLD (which is what you already use).

Because you're asking, everything else seems overkill. If it wasn't, then you probably wouldn't be asking.


vi /etc/dhcp3/dhclient.conf

send host-name "ubuntu-laptop";

and

/etc/init.d/networking restart

Tags:

Networking