Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I'm using virtualbox for testing and have the latest stable Debian.

When I try to set the hostname of the machine using both:

hostname ayil

and editing /etc/hostname and running /etc/init.d/hostname.sh start to make sure it activated.

Whenever I run hostname -f it returns:

Unknown-08-00-27-47-ef-30.config

Rather than ayil.

I'm not sure what else to do, a helpful point in the right direction would be greatly appreciated.

share|improve this question
add comment

4 Answers

up vote 4 down vote accepted

Roughly speaking, hostname shows what your computer thinks it's called, whereas hostname -f shows what the world thinks your computer is called. Changing /etc/hostname affects what hostname displays. To change what hostname -f display, you should look into your network setup, specifically at your DNS setup.

Here's a relevant extract from the hostname(1) man page:

You can't change the FQDN (as returned by hostname --fqdn) or the DNS domain name (as returned by dnsdomainname) with this command. The FQDN of the system is the name that the resolver(3) returns for the host name.

Technically: The FQDN is the name gethostbyname(2) returns for the host name returned by gethostname(2). The DNS domain name is the part after the first dot.

Therefore it depends on the configuration (usually in /etc/host.conf) how you can change it. Usually (if the hosts file is parsed before DNS or NIS) you can change it in /etc/hosts.

Depending on what you want to use $(hostname -f) for, you should either edit /etc/hosts or configure the DNS server running on your host machine or network to know about ayil. Editing /etc/hosts is the simplest path to having hostname -f show ayil, but if there is a purpose to running hostname -f as opposed to hostname, it's likely that you want the information to be consistent with some information stored outside this machine.

share|improve this answer
    
I've set both host machine to respond to ayil on the correct IP and the guests /etc/hosts. But still hostname -f returns the .config one. It's not a huge deal but it would be nice if I could get it sorted. –  digital Aug 30 '10 at 13:43
add comment

Your current changes won't apply until you reboot the system since kernel itself has hostname and domain name in mind. To make things properly I would advise to perform these simple steps (some of them you have already done) on the host example.domain.com:

  • Edit /etc/hostname to contain your hostname (w/o domain name) example
  • Edit /etc/hosts to match your new hostname. Otherwise you'll get errors for host lookup during sudo or other system operations. It's normally to map your hostname to loopback. Remember to put FQDN first and then just the hostname. Part of this file can look like:
127.0.0.1 example.domain.com example localhost.localdomain localhost

This step can be omitted if DNS lookup will point on this server when prompting for example.domain.com record.

  • Update the kernel hostname and domainname records:
sudo sysctl -e kernel.hostname=example
sudo sysctl -e kernel.domainname=domain.com

The next time you log in your shell prompt will show you the new example.domain.com hostname.

share|improve this answer
add comment

Have you checked to make sure DHCP isn't setting your hostname? Do 'ps aux | grep dhclient' to see if a DHCP client is running. If it is, you can doublecheck if the DHCP server is assigning the hostname by looking at the DHCP lease in /var/lib/dhcp/. Find the lease file there of your interface (probably dhclient.eth0.leases if you're using eth0). cat the file, and you might find that the host-name and domain-name dhcp options are set.

For example, my hostname at the prompt is 'dhcpuser-82', with the full domain name being "dhcpuser82.dh.example.com". My /var/lib/dhcp/dhclient.eth0.leases file looks like this:

...blah blah
option host-name "dhcpuser-82";
option domain-name "dh.example.com";
... more blah blah

If so, this is the origin of your frustrations. Either stop using dhcp and go static (which, if you do, don't forget to issue a kill to the running dhclient since a network restart won't automatically kill it, and you'll find things back the way they were the following day), or check google for ways to bypass the host-name and domain-name DHCP options. BTW, don't try to edit the leases file to correct the problem. It'll just get overwritten the next time dhcp updates the lease, or worse it wont be able to renew your lease and your host will lose it's ip.

share|improve this answer
add comment

If your server is running the DHCP client, it may be resetting your hostname every time it renews your lease. Check /etc/default/dhcpcd and look for these lines:

# Uncomment this to allow dhcpcd to set hostname of the host to the
# hostname option supplied by DHCP server.
SET_HOSTNAME='yes'

You probably want to comment out the SET_HOSTNAME line, instead.

Another thing you can try is to set up the reverse DNS for your IP, if you host allows this.

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.