Connect to Linux by name rather than IP

77,014

Solution 1

Zero-configuration LAN discovery protocols, in order of preference:

  • mDNS
    • Used primarily by Apple products and some Linux programs, but also available for Windows and possibly other platforms; uses IP multicast.
    • Provides both name lookup and service discovery.
    • Host names are always in the form name.local.
    • Software:
      • natively supported on Windows 10.1709 and later (must be enabled via Registry);
      • Bonjour on older Windows;
      • Avahi with nss_mdns on Linux;
      • natively supported on OS X and iOS (and, supposedly, Android).
  • LLMNR
    • Used for name resolution by Windows Vista and above, as a modern alternative to NetBIOS name services.
    • Very similar to mDNS, but has fewer features – in particular, lacks service discovery (Windows uses the WS-Discovery protocol for that).
    • Uses bare hostnames (like NBNS, but unlike mDNS).
    • Software:
      • Natively supported by Windows Vista and later;
      • systemd-resolved with nss_resolve (part of systemd 216) on Linux. .
  • NetBIOS name services
    • Part of the old NetBIOS network protocol suite used by Windows, OS/2, MS-DOS... Still in use. Not an excuse to start using it on new networks.
    • Provides name resolution and (to some extent) service discovery – aka "Network Neighbourhood" or "My Network Places" in Windows.
    • Uses IPv4 broadcasts (somewhat chatty), and has its own complex and stupid "browser election" protocol to reduce the chattiness. No IPv6 support at all.
    • Software:
      • Natively supported on Windows and OS/2;
      • nmbd with nss_wins (part of Samba) on Linux and BSD's;
      • Apparently OS X supports it natively as well?

Methods that are often pre-configured for you:

  • DNS using a local (internal) domain name
    • Home gateways tend to have this built-in, as part of the local DNS cache. Often it's just regular dnsmasq, which you could run manually if you're setting up a Linux/BSD-based gateway.
    • They take the hostname from your DHCP request, register it within dnsmasq under a domain like home, and offer themselves as the main DNS server (act as DNS cache).
    • Works by default with most operating systems, but only with DHCP (the gateway doesn't know hostnames of static-IP hosts), and generally tends to be rather flaky in my experience.
    • Software:
      • A regular DHCP client on the hosts. Must send the hostname option.
      • dnsmasq on the gateway. (Larger setups could use dhcpd + named.)

Methods involving manual configuration:

  • DNS using your own domain name
    • Works everywhere. (Won't help you to actually connect over the Internet, though.)
    • Dynamic DNS possible if you use DHCP and control a DNS server; otherwise all data is static.
    • Need to own a domain name (which will cost a few bucks).
  • free DNS subdomains
    • Still DNS, just free (or much cheaper than a domain), but also quite limited. Services like FreeDNS and Dyn offer registration of individual subdomains under a domain they control (for example myhost.dyndns.com).
      • Way too often, the subdomain has already been picked by someone else...
    • Dynamic DNS updates are often allowed (DynDNS-style).
  • /etc/hosts
    • A text file listing IP address – hostname pairs, which must be manually configured on each client machine.
    • (%SystemRoot%\system32\drivers\etc\hosts on Windows)
  • SSH configuration
    • The "Hostname" field in PuTTY, or ~/.ssh/config on OpenSSH.
    • Must be manually configured.
  • PostIt notes all over your desk
    • Very cheap. No naming policy. Infinite data types.
    • Rather unreliable. Must be manually distributed. No TTL, which often results in stale information being cached for months until someone notices. Query algorithms are inefficient. Responses to queries tend to get lost easily, sometimes leaving just a glue record on your monitor. (Three months later, you might find them buffered behind your desk.)

Solution 2

Two options.

  • All services:

Put an entry for it in /etc/hosts. Don't touch existing lines, add a new one.

  • ssh only:

Add a Host stanza to ~/.ssh/config with the name you want to use, then add desired options below that. See man 5 ssh_config for more details.

Host myserver
    Hostname 192.168.123.234
    Protocol 2

Solution 3

From Windows 7 (per your edit)...

Start -> Run -> notepad c:\windows\system32\drivers\etc\hosts

When notepad starts, go to the bottom of the file and add your ip address and hostname:

x.x.x.x    mylinuxhostname mylinuxhostname.domain.com

Save the file, then try pinging it from a command prompt:

c:\> ping mylinuxhostname
c:\> ping mylinuxhostname.domain.com

This will only work from your LAN. Connecting to it from "the world" (per your comment added) is an entirely different beast involving a DNS (dyndns, godaddy, etc) and router configuration like (NAT) network address translation et al.

Solution 4

If you want to access the Linux box from an arbitrary computer, you would need a domain name. Use a dynamic DNS service to point a domain name to your Linux computer (or the router it is connected to). Once you set this up, it will give yo the additional benefit of not having to worry about the IP of your Linux computer ever changing.

Solution 5

I'm also using Ubuntu 10.04 and I can connect to my machines using their name. Say I have computers named ernie and bert that are on the same network:

My prompt looks like this on ernie:

ernie:~$ 

If I type this:

ernie:~$ ssh [email protected]

I'll be root on bert:

bert:~#

I do this between two ubuntu machines, but I'd guess it would work in putty too. (I don't know about connecting to windows from Ubuntu, see other answers about samba.)

You have to install sshd in Ubuntu for this to work: sudo apt-get install openssh-server

Share:
77,014
Mr Fox
Author by

Mr Fox

Updated on September 17, 2022

Comments

  • Mr Fox
    Mr Fox over 1 year

    I'm new to Linux (currently running Ubuntu 10.04) and I have just finished setting up SSH access to my Linux Machine. Currently, I have to use ssh [email protected] to connect but would much rather be able to swap the IP for the computer name (like in Windows with \\name) or a domain name (like computername.example.com).

    I don't really know where to start so any help would be most appreciated. Please go slowly, as mentioned - I am still new to this.


    EDIT 1

    Completely forgot to mention that I am trying to connect from Windows 7 (via PuTTY) - sorry.

  • David Z
    David Z over 13 years
    Just to clarify, the IP address of the computer may still change (depending on how your ISP manages that stuff), but you won't have to worry about it since the dynamic DNS service provides a domain name that always maps to whatever the current IP address of the computer is.
  • Mr Fox
    Mr Fox over 13 years
    Haha, I like the PostIt notes option :)
  • Broam
    Broam over 13 years
    My network does this because of Avahi (Zeroconf networking). I do not know the viability of a Zeroconf client/server on Windows.
  • Broam
    Broam over 13 years
    This is because of Avahi, a Zeroconf daemon that is installed by default. It's not going to help him on Windows, unless he has a client due to some other reason (maybe iTunes installs one? I can't say.)
  • Broam
    Broam over 13 years
    +1 for mDNS / Avahi. Avahi is installed by default on many distros, but may be a package install for yours.
  • Martin Ueding
    Martin Ueding almost 11 years
    In Debian, you will have to install Avahi manually.
  • gilgwath
    gilgwath over 9 years
    @grawity Good answer. One thing to improve though: Have in mind that local DNS/DHCP works perfectly fine without a registered domain. Just throw a Raspberry PI under your desk and setup a small server with e.g dnsmasq.
  • user1686
    user1686 over 9 years
    @paradoxon: Thanks, I forgot to mention that. (Most home gateways do already run dnsmasq or something similar but crappier.)