Monday, September 19, 2011

Quick Tip: Increase port range available for applications

By default an average Linux distribution allows applications to use the following TCP port range for outgoing connections: 32,786-65,536. That’s why your system can handle up to 28,232 TCP sessions at time. Notice, this is more than enough if your Linux system is installed on the laptop or desktop and you just use it for occasional visits to, and (yeah!). But if you run proxy/webcache like squid or some other services which open a lot of outgoing TCP connections you will likely hit ceiling of 28,232 soon.
First of all, let’s see current port range available for TCP sessions:
cat /proc/sys/net/ipv4/ip_local_port_range
Most likely the output will show something like this one “32786 65536″. In order to expand this range you can either echo modified range into above file in /proc filesystem (temporary solution) or add corresponding line into /etc/sysctl.conf (constant solution).
To temporarily expand port range from 28,232 to 40,000 do the following:
sudo -s
echo "25000 65000" > /proc/sys/net/ipv4/ip_local_port_range

To make sure new port range will be applied after reboot add the following line to /etc/sysctl.conf:
net.ipv4.ip_local_port_range="25000 65000"
or just execute this:
sudo sysctl -n net.ipv4.ip_local_port_range="25000 65000"