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

$ hostname
$ cat /etc/hosts   localhost   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
search company.local
  • 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? Jul 18, 2011 at 14:58
  • What is a DNS configuration? Also, see updated post.
    – tshepang
    Jul 18, 2011 at 15:21
  • 2
    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? Jul 18, 2011 at 18:12
  • Thanks a lot. Will have a look. Oh, and it's more an article than an answer :)
    – tshepang
    Jul 18, 2011 at 18:33
  • 4
    The answer is "install and configure dnsmasq". Done. :) Jul 18, 2011 at 18:35

6 Answers 6


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:       localhost localhost.localdomain   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:  Bcast:  Mask:

    In this example, the IP address of B is 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 and a “subnet mask” setting with the value, 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 (–; 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:

    • 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, put a line server= 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 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 where 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).

  • Make your own DNS server with Dnsmasq: how can I tell to other machine to use S, when the other machine have Windows installed Apr 27, 2013 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. Apr 27, 2013 at 14:26
  • 1
    I adited c:\windows\system32\drivers\etc\hosts and it works. Thanks! Apr 27, 2013 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! Dec 26, 2016 at 3:39
  • 4
    Although this answer is detailed and informative, it doesn't provide a practical solution to this problem. Setting up a DNS server requires configuring not only an always-on central DNS server, but also each client on the network. If a device moves to a new network, a new central DNS server will need to be configured for the new network and for every device on the network. On the other hand avahi-daemon multicast DNS solution doesn't require a central DNS server configuration nor for the clients to be aware of a local DNS.
    – user84207
    Dec 25, 2020 at 4:43

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.

P.S.: The .local TLD may be implied via the DNS search path of your resolvers, and the full domain may be automatically constructed from the OS-specific notion of hostname depending on the implementation (so you may not need to change your hostname in any way). For example if your hostname is svr, svr.local or even just svr should resolve on your LAN subnet.

  • 2
    mDNS does not seem to be supported on Windows without installing something on every windows box.
    – Zitrax
    Mar 27, 2016 at 13:44
  • 5
    @Zitrax Good point (this is a *nix site though). Relevant info for the Windows setup can be found there -- relatively straightforward.
    – tne
    Mar 28, 2016 at 16:15
  • 5
    Aka Zeroconf or Bonjour. Provided by avahi package(s). You can query your network via avahi-browse -alr for example.
    – DanMan
    Mar 13, 2018 at 14:55
  • @tshepang Thanks for the edit suggestion. I don't know if it's a universal thing however: do you know if most mdns resolvers do that implicitly or just some? Otherwise could you have observed that just because local is in the resolver's DNS search path/list? Mine is configured by NetworkManager with just .lan instead of .local for example.
    – tne
    Jun 21, 2021 at 4:55
  • 2
    With systemd mDNS should be enabled by default. If you have systemd but mDNS doesn't seem to be enabled, try sudo systemctl start systemd-resolved and (to make the change permanent) sudo systemctl enable systemd-resolved.
    – Socowi
    Nov 27, 2021 at 20:54

As tne suggested, Multicast DNS (mDNS) seems to be the easiest solution here, as long as you only want to access the machine from the same local network.

On Linux you can export your hostname to the network with mDNS using avahi. To install it on Ubuntu server 20.04, use apt install avahi-daemon.

After that your server should be reachable via: hostname.local

  • THIS is the right answer. Thank you. Feb 1, 2023 at 13:35
vi /etc/dhcp3/dhclient.conf

send host-name "ubuntu-laptop";


/etc/init.d/networking restart
  • Should I run this on the client or host?
    – tshepang
    Jul 18, 2011 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, 2011 at 21:54
  • As I remember I only need to run this on client side.. Jul 19, 2011 at 5:11
  • Correct me if I'm wrong but I believe this would be a dhcp client but it is the server in the original question. All on the same machine, edit the file and restart networking.
    – mchid
    Mar 20, 2021 at 8:25

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.


If you do not want to meddle with the hostfile, and by chance you own an Internet domain name and Internet access, you can create an A type subdomain that points to your internal IP. For example setup intranet-pc1.someperson.com and points to

  • 1
    I meaaaan you're right you can do this. but it also means I can query for your servers' LAN IP addresses which is not recommended Jul 24, 2021 at 22:38
  • @ThatRealtorProgrammerGuy I've seen "not recommended" before, but I can't really imagine a good reason for it. If, indeed, we have a domain resolving to, that information is of no use to a hacker unless the hacker is ON the local network. In which case, you have far worse problems than them knowing the domain name (which they could no doubt find from arp or avahi)
    – Auspex
    Aug 20, 2023 at 18:59
  • @Auspex well, there are many different use cases searching this question that might see and use this technique. Not just your production setup but home labs, gamers, power users.. you wouldn't want devices on your home's lan advertised for MitM attacks. Especially consider cases where the hostname reveals telling information about the attached device and the lack of control over local environment as in a datacenter. It is also just.. not really the place for LAN IPs. Aug 22, 2023 at 10:04
  • I agree it's not the place for LAN IPs, but I don't agree it's the problem many say it is. If you DO put a LAN IP in public DNS it has to be with the absolute understanding that you are not reaching that internal address from points on the Web. Anyway, as it was here that I got on track for the right solution, a shout out for pip install mdns-publisher (see andrewdupont.net/2022/01/27/…) which lets you make aliases for avahi/MDNS hosts.
    – Auspex
    Aug 22, 2023 at 12:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .