Fixed: Sudo takes ages to run

There has been a noticeable lag in my terminal whenever I’ve tried to run a command with sudo. Basically you have to wait 5 seconds and then the password prompt finally appears.

It turns out this happens if you change the hostname of the machine from its default. For me this would have happened during installation. Editing the /etc/hosts files to include my laptop’s hostname [1] now ensures that running commands with sudo is instantaneous. For example, something like sudo echo 127.0.0.1 $(hostname) >> /etc/hosts should work (but don’t just copy/paste that).

My /etc/hosts file was empty, btw. I don’t know if the standard localhost mappings are now defined somewhere else and I’m supposed to be editing a different file these days, but the above worked fine so I’m leaving it.

Posting this here in the hope that the PureOS devs might program this fix into the distro.

[1] https://serverfault.com/questions/38114/why-does-sudo-command-take-long-to-execute

2 Likes

You could also put it here: https://tracker.pureos.net/

1 Like

I imagine that the problem here is that the absence of the hostname in the /etc/hosts file triggers a DNS lookup on the hostname each time, and the lookup fails slowly. (So one workaround, albeit a bit dubious, would be to ensure that the lookup succeeds in a timely fashion - which realistically you can only do if you have your own DNS server.)

I don’t think that 127.0.0.1 is the right IP address to use though. Some computers that I have seem to want to use 127.0.1.1 by default as the IP address that is associated with the hostname.

However locally I like to be able to copy /etc/hosts from one computer to another and hence I am associating the actual LAN IP address with the hostname (whereas the use of any 127 address will prevent that from giving a usable result). Realistically you can only do this if each computer has a fixed IP address.

PS sudo can’t be used with shell redirection operators (like >>) because the redirection occurs in the context of the original process and hence gets ‘Permission denied’.

I believe the yuk Linux idiom is echo stuff | sudo tee -a file