Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Here are details of the machine I want to access using its hostname:

$ hostname
hostname
$ cat /etc/hosts
127.0.0.1   localhost
127.0.1.1   hostname.company.local  hostname

It's a default Debian 6 (Squeeze) install, so I didn't fiddle with anything yet.

This is what I get from a machine (running Debian Unstable) trying to access above machine:

$ ping hostname
ping: unknown host hostname
$ ping hostname.company.local
ping: unknown host hostname.company.local
$ cat /etc/resolv.conf
nameserver 192.168.2.21
nameserver 192.168.2.51
search company.local
share|improve this question
    
You need to do something on the client (the machine where you run ping), or on a machine that client consults. What is the OS on the client? What is its DNS configuration? – Gilles Jul 18 '11 at 14:58
    
What is a DNS configuration? Also, see updated post. – Tshepang Jul 18 '11 at 15:21
1  
Is my answer the kind of things you were after? If so, the question needs simplifying — who knows about ping and /etc/hosts but not about DNS anyway? – Gilles Jul 18 '11 at 18:12
    
Thanks a lot. Will have a look. Oh, and it's more an article than an answer :) – Tshepang Jul 18 '11 at 18:33
3  
The answer is "install and configure dnsmasq". Done. :) – Warren Young Jul 18 '11 at 18:35
up vote 88 down vote accepted

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

share|improve this answer
    
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ădeanu Apr 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. – Gilles Apr 27 '13 at 14:26
    
I adited c:\windows\system32\drivers\etc\hosts and it works. Thanks! – Radu Rădeanu Apr 27 '13 at 15:29
    
I was pulling my hair out looking for this and found it in the router like you suggested. Thanks for the detailed information! – Jason Turan Dec 26 '16 at 3:39

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.

share|improve this answer
    
mDNS does not seem to be supported on Windows without installing something on every windows box. – Zitrax Mar 27 '16 at 13:44
    
@Zitrax Good point (this is a *nix site though). Relevant info for the Windows setup can be found there -- relatively straightforward. – tne Mar 28 '16 at 16:15

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.

share|improve this answer
vi /etc/dhcp3/dhclient.conf

send host-name "ubuntu-laptop";

and

/etc/init.d/networking restart
share|improve this answer
    
Should I run this on the client or host? – Tshepang Jul 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. – Caleb Jul 18 '11 at 21:54
    
As I remember I only need to run this on client side.. – LanceBaynes Jul 19 '11 at 5:11

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.