Changing hostname using "hostname" breaks sudo

Bug #113778 reported by Yann Hamon
8
Affects Status Importance Assigned to Milestone
hostname (Ubuntu)
Incomplete
Undecided
Massimo Forti

Bug Description

When trying to change the hostname via the command hostname:
# sudo hostname newhost

The /etc/hostname file is updated, but...

root@newhost:~# sudo vi /etc/hosts
sudo: unable to lookup roller via gethostbyname()

You get this error. It means trying to change the hostname with the hostname command completely breaks your sudo.

To solve this you have to edit your /etc/hosts file and add your new hostname at the end of the 127.0.1.1 line.

Revision history for this message
Yann Hamon (yannh) wrote :

In fact it doesn't change the /etc/hostname neither, I don't know the file hostname actually changes.

Revision history for this message
Massimo Forti (slackwarelife) wrote :

Thanks for your bug report, which version of Ubuntu/Kubuntu you are using. Have you tryed to change you host name with the network tool to see the result ???

Thanks

Changed in hostname:
assignee: nobody → slackwarelife
status: Unconfirmed → Needs Info
Revision history for this message
Yann Hamon (yannh) wrote :

That's actually on Feisty 64 bits server... So i did not try the network tool.
I remember having the same issue some time ago with... I am not sure if it was sarge or dapper..

Revision history for this message
Massimo Forti (slackwarelife) wrote :

The hostname command reponds with errors ??? If it isn't, you can attach yor syslog file and messages file after you have done this command. Thanks

Changed in hostname:
assignee: slackwarelife → nobody
Changed in hostname:
assignee: nobody → slackwarelife
Revision history for this message
Công (congtruong) wrote :

I try to change my host name, and file hostname wasn't updated :)

root@myhost:~# cat /etc/hostname
myhost
root@myhost:~# hostname
myhost
root@myhost:~# sudo hostname newhost
root@myhost:~# cat /etc/hostname
myhost
root@myhost:~# hostname
newhost

Any idea?

Revision history for this message
pat72 (f-bastianello) wrote :

If you keep entering the same command with sudo, after a few times of sudo: unable to lookup $HOSTNAME via gethostbyname() system asks for the password and you can execute the command. I had to edit /etc/hosts /etc/hostname and run /etc/init.d/hostname start to have my sudo working correctly (and my hostname changed)

Revision history for this message
Massimo Forti (slackwarelife) wrote :

Hi Công. understand:

How to change the hostname of a Linux system

Normally we will set the hostname of a system during the installation process. Many peoples don’t care about this, and don’t change the hostname even if for example this was set to something really stupid by the datacenter that installed the system (most likely they will set this to “debian” on any debian installation, etc). For me, it is important to see on each one of the ssh screens I will have open at any time a different hostname that is relevant and will give me quickly the information on what system I am logged in.

Change the hostname on a running system

On any Linux system you can change its hostname with the command ‘hostname‘ (surprised?)… Here are some quick usages of the command line hostname:

hostname

without any parameter it will output the current hostname of the system.

hostname --fqdit

 will output the fully qualified domain name (or FQDN) of the system.

hostname NEW_NAME

will set the hostname of the system to NEW_NAME. This is active right away and will remain like that until the system will be rebooted (because at system boot it will set this from some particular file configurations - see bellow how to set this permanently). You will most probably need to exit the current shell in order to see the change in your shell prompt.

Permanent hostname change on Debian based systems
Debian based systems use the file /etc/hostname to read the hostname of the system at boot time and set it up using the init script /etc/init.d/hostname.sh

/etc/hostname
serverSo on a Debian based system we can edit the file /etc/hostname and change the name of the system and then run:

/etc/init.d/hostname.sh startto make the change active. The hostname saved in this file (/etc/hostname) will be preserved on system reboot (and will be set using the same script we used hostname.sh).

Permanent hostname change on RedHat based systems
RedHat based system use the file /etc/sysconfig/network to read the saved hostname at system boot. This is set using the init script /etc/rc.d/rc.sysinit

/etc/sysconfig/network
NETWORKING=yes
HOSTNAME=”plain.domainname.com”
GATEWAY=”192.168.0.1″
GATEWAYDEV=”eth0″
FORWARD_IPV4=”yes”So in order to preserve your change on system reboot edit this file and enter the appropriate name using the HOSTNAME variable.

Use sysctl to change the hostname
Why would someone need a different method of doing the same thing as above? No idea, but here is anyway: use sysctl to change the variable kernel.hostname:
Use:

sysctl kernel.hostnameto read the current hostname, and

sysctl kernel.hostname=NEW_HOSTNAMEto change it.

Revision history for this message
Massimo Forti (slackwarelife) wrote :

You can read that the only hostname command doesn't change the file /etc/hostname, but set the hostname on the way if you need to change it. If you want change the file you can use the last istruction.

After this I don't think this is a realy bug. Thanks many for your help.

Revision history for this message
rashelleym (moorere) wrote :

Using "Dapper Drake" 6.06 LTS for 386 architecture, I edited /etc/hostname directly, and somewhat later discovered that I was locked out of sudo with the message "sudo: unable to lookup [new server name" via gethostbyname()," after (I believe) at least one restart. I had no idea a script should be invoked when the hostname is changed.

Pat72's comment about simply sudoing the same command over and over allowed me to unlock it, after between 10 and 20 tries. So this method of bug reporting seems good to participate in, even to the minor extent of clarifying that it seems to happen when /etc/hostname is directly edited, too.

Revision history for this message
FrancisL (francislav) wrote :

I have the same problem on 8.04 server 64bit

# sudo hostname mynewhostname
No change at all

# sudo sysctl kernel.hostname=mynewhostname

Then I cannot sudo anymore and the hostname never get changes. (I need to reboot)

To change the hostname I need to sudo bash, then modify both /etc/hostname and /etc/hosts

To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.