Ssh – Unable to resolve hostname

hostnamessh

So I try to do:

ssh $(hostname)

and it tells me:

ssh: Could not resolve hostname woofy: Name or service not known

It knows that its own hostname is "woofy"; why can't it connect to itself?

Best Answer

So we know hostname returns woofy. But that name can't be resolved to an IP address.

The short answer is that you need to add an entry for woofy in /etc/hosts. Make it resolve to 127.0.0.1. Or if your system is IPv6-capable, ::1.

Keep a backup of the previous version of /etc/hosts in case you make a mistake (I use the handy etckeeper package for this, but if you prefer it ye olde way, you can use a manual backup or even RCS).

The long answer is that how hostnames are resolved to IP addresses is controlled by a set of configuration files which vary slightly between Unix variants. You can configure your Unix system to resolve host names by hosts file (/etc/hosts will work on almost any Unix system) or by DNS (systems which have direct IP reachability to the Internet will always do this). There are other alternatives too, mostly less widely used (including LDAP and NIS/NIS+). See the Wikipedia article about the Name Service Switch for more context on this.

Edit: if this still causes a DNS lookup, the problem is probably that your Name Service Switch configuration consults DNS before /etc/hosts so the change to /etc/hosts has no effect. Try looking at /etc/nsswitch.conf (how the NSS is configured varies between operating systems).

Related Question