Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?

share|improve this question
add comment

2 Answers

up vote 6 down vote accepted

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.

share|improve this answer
    
Thanks! I actually somehow solved the problem by becoming root –  wrongusername May 9 '12 at 23:28
    
That probably means that the permissions of the /etc/hosts file on our system is wrong. Does host woofy work as a non-root user? –  James Youngman May 10 '12 at 0:24
add comment

Unix systems have a notion of host name that isn't related to the network subsystem. That name is what the hostname command shows and what turns up in logs. When you make an ssh connection, it performs a DNS lookup, to find out what the name means in the context of the TCP/IP protocol family that SSH operates on.

For your sanity, you had better make sure that this host name agrees with what your neighborhood DNS server says, and with what (if anything) your system thinks it's called. In fact, DNS names are not associated with machines, but with network interfaces, so your DNS name will be associated with one of your network interfaces. On machines with no actual network connection, the host name should be associated with the loopback interface.

In practice, the hostname is stored in the file /etc/hostname on most machines. You can change it at runtime with the hostname command; this is done early in the boot process. The local idea of the DNS name is stored in /etc/hosts, on the line corresponding to either the loopback interface (127.0.0.1) or to one of the machine's network interfaces.

share|improve this answer
add comment

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.