114

After modifying /etc/hosts, which service(s) need(s) to be restarted for the change to take effect?

3
  • 12
    For anyone coming back here through random web search... Obviously don't run sudo ifconfig eth0 down if you are on a SSH client for similar. – OskyEdz Snakehult Jan 4 '17 at 6:01
  • 1
    // , I've NEVER made that mistake before. :/ – Nathan Basanese May 1 '17 at 20:35
  • 1
    @OskyEdzSnakehult unless you run both in one line, separated by &&, inside tmux/screen/etc. so that the command completes even though you get disconnected temporarily. – Walf Oct 2 '18 at 3:10
90

You don't need to restart any services.

When tested on a 14.04 server installation, the changes were reflected immediately.

The only service that I think could be related is 'networking', but it's unnecessary to restart it.

6
  • // , Why do these not need a restart? – Nathan Basanese May 1 '17 at 20:34
  • 1
    // , Incidentally, this is also true of Mac OS X (10.12.4 (16E195)). – Nathan Basanese May 1 '17 at 20:34
  • 1
    rebooting is, technically, restarting every service. And it shouldn't be necessary... though, note that some applications (eg., firefox) may cache this data and the applications need to be restarted. – michael Nov 2 '17 at 5:31
  • 5
    No! It doesn't work for Ubuntu 16.04.4 LTS – Green Jun 8 '18 at 14:15
  • 1
    Doesn't work for updated Linux, – ChikuMiku Sep 14 '18 at 20:40
11

You can try: (Disclaimer: Don't run it if you're connected via SSH)

sudo ifconfig eth0 down
sudo ifconfig eth0 up

(substitute eth0 with your network card)

but Arronical is right in theory: You don't need it.

4
  • 7
    This is what I tend to do after any network changes, I normally put && between the two commands to make it a one-liner. – Arronical Aug 26 '15 at 13:31
  • 22
    Unless, err.. you've ssh'd into the machine remotely? – sharky May 19 '17 at 4:30
  • 4
    if connected via remote ssh, then a reboot might be just as (un)safe as a manual network stop/start (because, of course, after turning off networking, you're disconnected & can't turn it back on -- or even reboot!). However, to do a ifconfig down + up, then use something like gnu screen (or tmux), and run both commands at once via ifconfig up && ifconfig down or putting them into a two-line shell script and running that. Then reconnect to your gnu screen/tmux session afterwards -- if all goes well. – michael Nov 2 '17 at 5:37
  • PLEASE ADD SOME DISCLAIMER THAT DON'T RUN THIS COMMAND IF YOU ARE CONNECTED TO A SERVER THROUGH SSH. – Shehroz Ahmed Apr 1 at 11:49
5

Theoretically, changes to the /etc/hosts file take effect immediately.

But, if you get old hostname after this

$ hostname

then, either reboot or restart your network connection... that should show you the right hostname.

5

If you use dnsmasq (is set by default), then do following:

$ pkill -HUP dnsmasq
2
  • Thank you. This was the only one that worked for me. The ping worked, but none of the browsers could reach the site. – makkasi Dec 11 '20 at 10:54
  • Or systemctl restart dnsmasq.service for systemd systems. – Mr. White May 27 at 17:40
4

Actually I noticed that postfix might benefit of a restart to include the new hosts in its local host file version that is available here /var/spool/postfix/etc/ (ubuntu 16.04)

3

For the most part etc/hosts changes should be recognized immediately. If you add a new entry then ping it by name on the machine hosting the etc/hosts file it will most likely ping, that means the etc/hosts update has been recognized by DNS. On a pihole system if you restart the DNS resolver it will pick up the newly-added/edited etc/hosts entries. Most likely it's a function of whatever program is trying to utilize the hosts entries, did it read the current stuff or did it read just at boot time, or like pihole, will it reread by restarting a simple internal function?

2

You need to restart systemd-hostnamed service which is a system service that may be used to change the system's hostname and related machine metadata from user programs.

/bin/systemctl restart systemd-hostnamed

1

I kept getting an error, and google took me to this question. I thought I needed to restart some service, but actually I found it was my fault:

$ ping remote_hostname
ping: unknown host remote_hostname

The reason why hosts wasn't recognising a new entry was a typo in the IP address:

##.2480.##.##    remote_hostname
1

You probably don't need to restart anything, unless there is a temporary cache of the DNS somewhere. To restart networking and clear the DNS cache on Debian and Ubuntu:

sudo /etc/init.d/networking restart

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.