# The right way to set hostname!

## Kalin

Ok, I started this posting as a cry for help, but eventually after a lot of stracing I found I had some forgotten stuff in the /etc/hosts file :-)

My problem was that `hostname -d` was returning an emty string, because I had

127.0.0.1       localhost

11.22.33.44    my-host

in /etc/hosts ...

So now that I found the right way to do it, just wanted to post it here:

Reading in /etc/init.d/hostname

 *Quote:*   

> 
> 
> # To have a proper FQDN, you need to setup /etc/hosts and /etc/resolv.conf
> 
> # properly (domain entry in /etc/resolv.conf, and FQDN in /etc/hosts).
> ...

 

I figured out the usuall setup is:

/etc/hosts :

```
127.0.0.1       localhost
```

/etc/hostname :

```
myhost
```

/etc/resolv.conf :

```
search mydomain.topdomain

nameserver <your dns cache IP>
```

Then:

```
$ hostname -s

myhost

$ hostname -d

mydomain.topdomain

$ hostname -f

myhost.mydomain.topdomain

$ hostname -s

myhost

$ uname -n

myhost

```

That setup works OK, except when you cannot contackt your dns cache (i.e. you have no network connection for example).

Any idea how to fix this?

----------

## derelm

well, this doesn't work for me,

i still get mymachine.nkown_domain as fqnd at the login prompt.

hostname (-s -d -f) doesn't even work, it says : 

```
hostname: No address associated with name
```

i'd love to get this solved!

btw: i use dhcp, it should set the domainname and the ypdomain automagically (which it does in /etc/resolve.conf and yp.conf)

----------

## XavierPenguin

Here's what I had to do to get this working.  Don't know if it's the 'correct' way to do it, but works for me.

1.  in /etc/hosts, put FQDN prior to hostname

```
127.0.0.1 localhost

192.168.0.1  host.home.net host

# had this, and 'domainname -d' would return nothing

# 192.168.0.1 host host.home.net

192.168.0.2  host2.home.net host2

```

2.  start domainname service, make sure domainname is in /etc/dnsdomainname.

```
$ cat /etc/dnsdomainname

home.net

$ /etc/init.d/domainname start

$ rc-update add domainname boot

```

3.  still ended up w/o domainname on tty login, need to update issue file changing \o to \O

```
This is \n.\O (\s \m \r) \t
```

----------

## jaanv

On my system there was a typo in /etc/init.d/domainname.

IIRC like this:

ebegin "Setting DNS domainname to ${mydnsdomain}"

/bin/domainname $mydnsdomainname

the second line should be:

/bin/domainname $mydnsdomain

jaanv

----------

## illc0mm

Okay, here is the solution:

As the install docs say:

Set your server name

```
$ echo mymachine > /etc/hostname
```

Set your Domain Name:

```
$ echo mydomain.com > /etc/dnsdomainname
```

And if you use NIS, set your NIS Domain Name:

```
$ echo nis.mydomain.com > /etc/nisdomainname
```

And then, for some reason this very important is left out:

```
$ rc-update add hostname default
```

That will take care of the dreaded:

```
This is hostname.(none)...
```

Prompt

Now, my next question. Is this a bug or a feature?

-illc0mm

----------

## ixion

I've done ALL of this, but $HOSTNAME still report my old hostname.. this is quite frustrating seeing how I'm utilizing dyndns and apache won't start because it can't get the right hostname.... I have to login as root and 'export HOSTNAME=newname.mydomain.org' to get anything to work.. why is this? /etc/hosts is correct, /etc/hostname is correct, /etc/dnsdomainname is correct, /etc/nisdomainname is correct, /etc/domainname is correct, domainname and hostname are added to runlevel default... $HOSTNAME still reports old hostname... /etc/profile doesn't seem to have any references to the old hostname.. what gives??  :Evil or Very Mad: 

----------

## illc0mm

Well, the only thing else I can suggest is:

```
$ find /etc -type f | xargs grep -i "oldhostname"
```

Should find any "hidden" references to that hostname. It has to be defined somewhere, the server won't "remember" it between reboots.

If that finds nothing, last result would be:

```
$ find / -type f | xargs grep -i "oldhostname"
```

Which will search from the root.

-illc0mm

----------

## sy5t3mfux

The only way I could get it to work was by putting the following line into /etc/conf.d/local.start

```
domainname -vF /etc/dnsdomainname
```

which sets up your domain name as the very last thing before you get to login

----------

## ixion

 *illc0mm wrote:*   

> Well, the only thing else I can suggest is:
> 
> ```
> $ find /etc -type f | xargs grep -i "oldhostname"
> ```
> ...

 

Thank You!!! Fixed it!  :Very Happy:   :Very Happy:   :Very Happy: 

There were remnants of it left in /etc/profile.env and /etc/csh.env... editing those to proper values did that trick... thanks again!

----------

## Kalin

2013-04-09 edit:

 That post is OLD! I'll leave it here for archivval purposes, but have a look at http://www.gentoo.org/doc/en/handbook/handbook-x86.xml?part=1&chap=8#doc_chap2 instead.

Well, I am still fighting with this problem from time to time on new (or old, forgotten) istallations.... 

Trying to clear it for myself:

 *Quote:*   

> Problem:
> 
> Set the FQDN hostname to "pe.do.net" of a Gentoo box (linux-2.6, glibc-2.3.2), so that it stays constant without regard of DNS cache reachability.

 

This is the desired output:

```
$ hostname

me

$ dnsdomainname

zo.net

$ hostname -s

me

$ hostname -d

zo.net

$ hostname -f

me.zo.net
```

The files to tweak:

```
/etc/hostname

/etc/resolv.conf

/etc/hosts

```

I tried all possible combinations of settings in the above files, and somehow got it. Now that I think of it, it was obvious...

Here is how to do it:

```

#/bin/bash

# Copyright Kalin KOZHUHAROV <kalin@ThinRope.net>

HOST="me"

DOMAIN="zo.net"

# the IP address of our DNS cache (some call it server, WRONG!)

CACHE="192.168.1.1"

# If you have tried before, clean up

echo "Cleaning..."

for f in /etc/{{host,{dns,nis}domain}name,resolv.conf,hosts}; do mv $f $f-bAcKUP 2>/dev/null;done

rc-update del domainname >/dev/null 2>&1

# Create the files needed:

echo "Creating the files needed..."

echo ${HOSTNAME} >/etc/hostname

echo -e "127.0.0.1\t\t${HOST}.${DOMAIN} ${HOST} localhost" >/etc/hosts

echo -e "domain ${DOMAIN}\nsearch ${DOMAIN}\nnameserver ${CACHE}"    >/etc/resolv.conf

echo "Updating your config..."

hostname -F /etc/hostname

rc-update add hostname boot >/dev/null 2>&1

# check what we have done

echo "Checking..."

set -x

hostname

dnsdomainname

hostname -s

hostname -d

hostname -f

set +x

echo

echo "====================================================================="

echo "If you are satisfied with the results, delete the old files by doing:"

echo -e '\trm -f /etc/*-bAcKUP'

echo

echo "You can also see how did your config change by:"

echo -e '\tfor f in /etc/{{host,{dns,nis}domain}name,resolv.conf,hosts}; do diff -u $f-bAcKUP $f;done'

echo "====================================================================="

```

Some notes:

1. /etc/init.d/domainname is a not a good one, as it does nothing for dns domain name, but prints that it does something!

2. See `man hosts` for the format and see the examples. Should work as I suggest.

3. Roughly speaking /etc/resolve.conf is used only when dnscache is reachable and etc/hosts when not. So test at both states.

4. It is not tested with DHCP, but if you can reach your DHCP server, you won't need it anyway. 

5. For IPv6, add the necesary entries from your old /etc/hosts file, or they will be added anyway.

6. That's all folks! Smile!

Last edited by Kalin on Tue Apr 09, 2013 4:34 am; edited 1 time in total

----------

## capt.Hij

Sorry for the necro-post, but I just came across this problem. This page had a high ranking on google and was second on the list, and unfortunately the information above is a bit out of date. The solution to the problem can be found at the following link:

http://www.gentoo.org/doc/en/handbook/handbook-x86.xml?part=1&chap=8

See section 8b. 

As of this writing the solution is to set the host name in the file /etc/conf.d/hostname

----------

## friesia

How do we set DNS domain name with systemd then?

----------

## cord

I use udhcpc and it get ip/mask,route from provider, but no hostname (always 'localhost').

Earlier I was using NetworkManager (same, udhcpc) and it was setting appropriate hostname on system startup.

```
May 29 16:07:33 [NetworkManager] <info>  [1496063253.1764] policy: set-hostname: set hostname to 'xxx.xxxxx.xxx' (from address lookup)
```

How to get hostname from provider (with no NM)?

----------

