• VMware

    Learn about VMware virtualization for its products like vsphere ESX and ESXi, vCenter Server, VMware View, VMware P2V and many more

  • Linux

    Step by step configuration tutorials for many of the Linux services like DNS, DHCP, FTP, Samba4 etc including many tips and tricks in Red Hat Linux.

  • Database

    Learn installation and configuration of databases like Oracle, My SQL, Postgresql, etc including many other related tutorials in Linux.

  • Life always offers you a second chance ... Its called tomorrow !!!

    Friday, March 08, 2013

    Setting up custom TCP/IP Keep Alive Parameter in Linux

    Why do we use Keep Alive parameter ?

    I guess the name itself explains the 90% of the definition and the rest let me try to explain you.

    Now many a times most of you have been in a situation when you are using putty and running some important script or application which takes days to process and complete like 2-3 days or may be more now in that case it happens the session gets disconnected automatically in between. It can happen with two reason either the ssh session got disconnected or the machine you have logged into has stopped receiving/sending  packets from the network. Now for the 1st case like if the ssh session has disconnected you from the network then that can be fixed with the below shown link

    But in case the machine stops transmitting packets or stops responding to the network packets then that is some thing which has to be taken care of to waste your time running the script every time and making sure your machine is sending packets by doing some network activities like pinging to some machine in network.

    Now that is not a solution. You can increase the Keep Alive parameter in your machine so that the machine automatically send a network packet to the network at a regular interval so that the machine does not disconnects. It uses the same technology as used in the case of heartbeat.

    How do we change Keep Alive parameter in Linux?

    The files responsible for TCP Keep Alive are

    These already have some pre-defined default values which you check using the following
    # cat /proc/sys/net/ipv4/tcp_keepalive_time

    # cat /proc/sys/net/ipv4/tcp_keepalive_intvl

    # cat /proc/sys/net/ipv4/tcp_keepalive_probes

    The numerical value in the first 2 files are defined in seconds and the third one is just a numerical value.

    Now the above 3 files explains you that send the first network packet(keepalive probes) after 7200 seconds and send it nine times before you get response. So if your machine gets a response before completing 9 times you will be stucked to the network after which you will be disconnected. The second keepalive probe will be sent after 75 seconds and the same above procedure will be followed. Similarly the third probe would start after 75 seconds.

    We should change these default values as per our requirement. You would not be allowed to change the values using vi editor so this is another method you can follow
    # echo 300 > /proc/sys/net/ipv4/tcp_keepalive_time
    # echo 60 > /proc/sys/net/ipv4/tcp_keepalive_intvl
    # echo 20 > /proc/sys/net/ipv4/tcp_keepalive_probes

    Here we have change these values so now the first keepalive probe will be sent after 300 seconds i.e. 5 minutes and 20 probes would be sent before the network is disconnected. And the next packet will be sent again after 60 seconds i.e. 1 minute so this can obviously help resolve my network dis connectivy problem.

    Now I need to refresh these settings on my machine

    To refresh the sysctl kernel parameters
    # sysctl -p
    To view all the kernel parameters
    # sysctl -a
    Reboot your machine once to take the changes affect and load your kernel with these new parameters.

    Let me know your success and failure.

    Related Articles
    Putty session disconnects when idle
    Reset the hard value of the file descriptors in Linux

    Follow the below links for more tutorials

    Configure Red Hat Cluster using VMware, Quorum Disk, GFS2, Openfiler
    Tutorial for Monitoring Tools SAR and KSAR with examples in Linux
    How to secure Apache web server in Linux using password (.htaccess)
    How to register Red Hat Linux with RHN (Red Hat Network )
    Red hat Enterprise Linux 5.5 Installation Guide (Screenshots)
    15 tips to enhance security of your Linux machine
    Why is Linux more secure than windows and any other OS
    What is the difference between "su" and "su -" in Linux?
    What is swappiness and how do we change its value?
    How to log iptables messages in different log file
    Basic iptables tutorial in Linux I
    Basic iptables tutorial in Linux II
    How to auto start service after reboot in Linux
    What is virtual memory, paging and swap space?


    1. Do you really need to refresh with sysctl -p and restart your computer for the changes to take effect? Thanks

      1. The settings you are playing with are kernel parameters which will only take affect if the system boots with the new parameters.

        But still sysctl -p command does the same work. So in case you can not restart the machine run sysctl -p and verify if it worked by using sysctl -a which will show you the currently loaded parameters in kernel.

      2. I changed the parameters without restarting and without doing a sysctl -p. When i view the parameters..it looks like it took the new ones.

      3. What command are you using to view those parameters?

        and by verifying I meant does it affects your work due to which you were changing those values for example "disconnecting of putty connection automatically after provided value of minutes?"

        Let me give you an example
        For selinux even if you disable the service using console it won't make any change even though you can see the service is disabled until and unless you restart your system with that new value. So the kernel has to boot with the new values.

      4. I'm running sysctl -a to view the parameters and it shows the 1800 instead of the 7200 for tcpkeepalive.

      5. Check whether you have properly saved the file with new values

        Then run # sysctl -p to refresh the parameters
        When you run that command it will throw you the new values for each parameter.

        Then restart your machine and run
        # sysctl -a

      6. But that is the issue...It looks like it already took the new parameters when i do sysctl -a ...since it is showing the correct value....do i still need to restart the machine ?

      7. I have already explained myself in the above comments for your question.