Amongst other things, I am trying to change my hostname with a script. Basically the script seds the values of ubuntu from /etc/hostname and /etc/hosts.

I tried this on Ubuntu 16.04.4 in the past and worked well.

Now, on 16.04.6 (or just by chance), I am getting errors with the same script. Just as I change the hostname, the change on the hosts files takes a long time (probably because sudo is trying to reach the new host which has not yet placed in the hosts file but I did not know sudo uses such information) and I get an error:

sudo: unable to resolve host xxxx

Sometimes also sudo: unable to resolve host ubuntu. Following script commands have the same problems or fail.

But obviously it is not possible to change at once the values of both files

Why did this happen? Is it something new in that Linux distribution version? I read other people having the same problem but never happened to me before, I was always able to edit both files with sudo with no other inconveniences.


sudo uses glibc's libnss to determine hostnames. Usually (with 16.04) nscd is running. You may be racing against its cache expiration. Relevant config in /etc/nscd.conf:

    enable-cache            hosts           yes
    positive-time-to-live   hosts           3600
    negative-time-to-live   hosts           20
    check-files             hosts           yes
    persistent              hosts           yes

check-files will caused nscd to "periodically check...the modification time of.../etc/hosts."

I am ignoring complications introduced by nonstandard configuration in /etc/nsswitch.conf.

You should have both the old and new hostnames in /etc/hosts until the old is no longer cached. Stopping ncsd and if persistent is yes then deleting /var/cache/nscd/hosts, and then starting nscd should work.

/etc/hostname is only consulted during boot and is irrelevant in this case.

Incidentally you should also set the hostname with hostnamectl and/or hostname which informs the kernel and any services that ask the kernel for the hostname e.g., via the uname system call, of the new hostname. hostnamectl does all sorts of other hostname settings as well because systemd.

| improve this answer | |
  • Yes, I actually updated the sed with hostnamectl trying to fix the issue. The strange thing is that this never happened before.... will take your recommendation and update /etc/hosts before – user1156544 Apr 5 '19 at 17:31

From sudoers(5) man page:

A user specification determines which commands a user may run (and as what user) on specified hosts. By default, commands are run as root, but this can be changed on a per-command basis.

Sudo configuration file (/etc/sudoers) allows to specify what command(s) an user can run, as another user(s) and on which machine(s). It is usefull if you have a lot of machines, as you can deploy the same sudoers file on different machines. Probably you have a line specifying the hostname as ubuntu (the former hostname). Run the visudo command, identify the line and edit it with the new hostname.

| improve this answer | |
  • Mmm.. there is no line with any hostname in the sudoers file. – user1156544 Apr 5 '19 at 17:23
  • Can you post your sudoers file (you can remove the coments with the comand sudo egrep -v '^\s*#|^$' /etc/sudoers). Also, check for files in /etc/sudoers.d directory (files in this directory are an extension of sudoers file). – JucaPirama Apr 5 '19 at 18:32
  • It is the standard Ubuntu file: askubuntu.com/questions/1059813/… (like in the question, except the pkaramol stuff) – user1156544 Apr 5 '19 at 18:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.