Skip to content

Setting OS TCP Keepalive

Brett Wooldridge edited this page Oct 17, 2024 · 3 revisions

On Linux

This is done by editing the /etc/sysctl.conf file:

# detect dead connections after 70 seconds
net.ipv4.tcp_keepalive_time = 60
net.ipv4.tcp_keepalive_intvl = 5
net.ipv4.tcp_keepalive_probes = 3

To activate the settings without rebooting the machine, run sysctl -p.

On newer MacOS versions (tested on 13)

Create the file /Library/LaunchDaemons/sysctl.plist:

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE plist PUBLIC '-//Apple//DTD PLIST 1.0//EN' 'http://www.apple.com/DTDs/PropertyList-1.0.dtd'>
<plist version='1.0'>
<dict>
	<key>Label</key>
	<string>sysctl</string>
	<key>Program</key>
	<string>/usr/sbin/sysctl</string>
	<key>ProgramArguments</key>
	<array>
		<string>/usr/sbin/sysctl</string>
		<string>net.inet.tcp.keepidle=60000</string>
		<string>net.inet.tcp.keepintvl=5000</string>
		<string>inet.inet.tcp.keepcnt=3</string>
	</array>
	<key>RunAtLoad</key>
	<true/>
</dict>
</plist>

You will have to reboot to activate the changes.

On older MacOS versions

Edit /etc/sysctl.conf:

# detect dead connections after 70 seconds
net.inet.tcp.keepidle = 60000
net.inet.tcp.keepintvl = 5000
net.inet.tcp.keepcnt = 3

You will have to reboot to activate the changes.

On Windows

Change the TCP keepalive settings by adding these registry keys:

HKEY_LOCAL_MACHINE/System/CurrentControlSet/Services/Tcpip/Parameters/KeepAliveTime
HKEY_LOCAL_MACHINE/System/CurrentControlSet/Services/Tcpip/Parameters/KeepAliveInterval

The registry keys must be of type DWORD and the values are in milliseconds rather than in seconds.

After changing these keys, restart Windows to activate them.