13

Sometimes (not too often at all) when I type "netstat", it takes over 5 seconds to return. While other times is instantaneous.

I have been observing this for weeks on my server (CentOS 6.4), but I don't understand why!?

Can anyone give an explanation?

Daniele B
  • 357
  • 4
  • 14

1 Answers1

21

It's all DNS resolution. If the DNS server is slow, it delays netstat. Pass -n to netstat when you invoke it to skip resolution and it'll often return immediately.

The other cause of the delay is if you use the -p (-b in Windows) to return the process owner since some processes don't like to be queried if you're not root/Administrator.

Nathan C
  • 14,901
  • 4
  • 42
  • 62
  • There are times (99%) when this does not happen at all. But when it happens, it happens for a few minutes, even on subsequent calls. Doesn't the DNS have some sort of caching system? Could it happen that the DNS is slow only at specific times? – Daniele B Apr 03 '14 at 14:55
  • Also, while this happens, also my network calls seem affected (about 5 seconds delay is added on each) – Daniele B Apr 03 '14 at 14:56
  • 1
    If you don't control the DNS servers anything could happen (high load, routing, etc). DNS does cache things, but `netstat` on a busy web server for example may have thousands of different IPs at the same time. Try the `-n` switch during periods of slowness to see what happens. – Nathan C Apr 03 '14 at 14:57
  • @DanieleB you can use `nscd` to cache lookups – Matthew Ife Apr 03 '14 at 15:17
  • @MatthewIfe, does this apply only to netstat lookups or to any domain lookup? I am asking this because I am not too worry about netstat (as Nathan said, you can always use -n). I am most worried for the slowdown to the network calls of my server-side application. – Daniele B Apr 03 '14 at 15:24
  • 1
    All (or nearly all) lookups. It also caches user and group lookups too, thats usually only important if you use ldap as a name service backend. – Matthew Ife Apr 03 '14 at 15:26
  • Thank you so much, we went from netstat taking well over 20 minutes to just a few seconds! – Jake Steffen Jul 29 '21 at 12:34