Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I tried to change my hostname according to this guide for Debian based system:

  1. I edited /etc/hostname
  2. I ran /etc/init.d/hostname start

Though there are errors. Always when I write a sudo command it tells me it can't recognize the hostname. After I restarted the system, the graphical environment wasn't working, the startx command wasn't successful. So, I changed the hostname back to the old name.

Can I change the hostname without errors?

share|improve this question
up vote 6 down vote accepted

The problem is that your new hostname can't be resolved to an ip address. There are basically two solutions for your problem.

First one but little bit more complex solution is to adjust the hostname in /etc/hosts as well. You will have to adjust /etc/hosts every time you change your hostname.

The second and simpler solution is to install nss-myhostname. The debian package name is libnss-myhostname so just install it with apt-get install libnss-myhostname. This will allow you to always resolve your current hostname.

share|improve this answer
I installed it. What should I run after installation? – xralf May 26 '12 at 12:22
@xralf nothing, that's all you need to do. You should now be able to change your hostname and should be able to resolve it via nss. Just try to run sudo and the error should be gone – Ulrich Dangel May 26 '12 at 12:23
There may be running programs whose NSS implementation has already read /etc/nsswitch.conf. Those programs may need to be restarted. – James Youngman May 26 '12 at 13:59
If you want to permanently change your hostname, does doing hostname NEW_HOST.TLD suffice if I've already installed libnss-myhostname? – Alix Axel May 14 '13 at 16:39
@AlixAxel no it just changes the hostname temporarily till the next reboot – Ulrich Dangel May 17 '13 at 10:54

Your Answer


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.