andreboyce

Untitled

Jul 15th, 2020
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 4.67 KB | None | 0 0
  1. # author: andre boyce
  2. # license: GPLV2
  3. # version: 1
  4.  
  5. # copy paste ready
  6. # Enable Google’s TCP BBR on a Linux VPS
  7. # creates 2 files enable_google_tcp_bbr disable_google_tcp_bbr
  8. # todo maybe add quiet option
  9. # maybe make sed commands fit on one line
  10.  
  11. # make a directory called scripts in the home directory
  12. mkdir -p ~/scripts
  13. # move in to the scripts directory
  14. cd ~/scripts
  15. # create a file called enable_google_tcp_bbr
  16. touch enable_google_tcp_bbr
  17. # make the file executable
  18. chmod +x enable_google_tcp_bbr
  19.  
  20. # echo this script in to the file
  21. echo '
  22. # Enable Google’s TCP BBR on a Linux VPS
  23. # tested on ununtu 20.04, centos 8
  24. # run as root or as user with sudo
  25.  
  26. # get the major_version of the kernel by splitting the result of uname -r by "." first column. e.g. 2.4.0 should give 2
  27. major_version=$(uname -r | awk -F"." '\''{print $1}'\'')
  28.  
  29. # get the minor_version of the kernel by splitting the result of uname -r by "." second column.  e.g. 2.4.0 should give 4
  30. minor_version=$(uname -r | awk -F"." '\''{print $2}'\'')
  31.  
  32. # if major_version of kernel less than 4.x.x
  33. if [ "$major_version" -lt "4" ]
  34. then
  35.   echo "Kernel version is: $major_version.$minor_version which is too low please upgrade kernel to version 4.9.0 or greater"
  36.   echo -e "\033[0;31mFailed\033[m" && exit 1;
  37. else
  38.   # if major_version of kernel is equal to 4.x.x then check if minor version less than 6  
  39.   if [ "$major_version" -eq "4" ] && [ $minor_version -lt "9" ]
  40.   then
  41.      echo "Kernel version is: $major_version.$minor_version which is too low please upgrade kernel to version 4.9.0 or greater"
  42.      echo -e "\033[0;31mFailed\033[m" && exit 1;
  43.   else
  44.      echo "Kernel version is: $major_version.$minor_version which is ok"
  45.   fi
  46. fi
  47.  
  48. # backup /etc/sysctl.conf
  49. yes | sudo cp /etc/sysctl.conf /etc/sysctl.conf.bak
  50.  
  51. # if we find "net.core.default_qdisc=fq" in /etc/sysctl.conf
  52. if sudo grep -Fq "net.core.default_qdisc=fq" /etc/sysctl.conf
  53. then
  54.   # echo "net.core.default_qdisc=fq present in /etc/sysctl.conf"
  55.   :
  56. else
  57.   echo "net.core.default_qdisc=fq" | sudo tee -a /etc/sysctl.conf
  58. fi
  59.  
  60. # if we find "net.ipv4.tcp_congestion_control=bbr" in /etc/sysctl.conf
  61. if sudo grep -Fq "net.ipv4.tcp_congestion_control=bbr" /etc/sysctl.conf
  62. then
  63.   # echo "net.ipv4.tcp_congestion_control=bbr present in /etc/sysctl.conf"
  64.   :
  65. else
  66.   echo "net.ipv4.tcp_congestion_control=bbr" | sudo tee -a /etc/sysctl.conf
  67. fi
  68.  
  69. # show differences
  70. # sudo diff /etc/sysctl.conf /etc/sysctl.conf.bak
  71.  
  72. # Reload configuration
  73. sudo sysctl -qp
  74.  
  75. # if it worked then expected output net.ipv4.tcp_congestion_control = bbr
  76. if sudo sysctl net.ipv4.tcp_congestion_control | grep -q "net.ipv4.tcp_congestion_control = bbr" ; then echo -e "\033[0;32mSuccess\033[m" && exit 0; else echo -e "\033[0;31mFailed\033[m" && exit 1; fi
  77.  
  78. ' > ~/scripts/enable_google_tcp_bbr
  79.  
  80. # execute the script we created
  81. ./enable_google_tcp_bbr
  82.  
  83. # create another file to disable google_tcp_bbr
  84. touch disable_google_tcp_bbr
  85. chmod +x disable_google_tcp_bbr
  86.  
  87. cat > ~/scripts/disable_google_tcp_bbr << EOF
  88. # remove net.core.default_qdisc=fq from /etc/sysctl.conf
  89. # remove net.ipv4.tcp_congestion_control=bbr from /etc/sysctl.conf
  90.  
  91. # if we find "net.core.default_qdisc=fq" in /etc/sysctl.conf
  92. if sudo grep -Fq "net.core.default_qdisc=fq" /etc/sysctl.conf
  93. then
  94.    # note would be nice to get sed to do this in one line
  95.    # remove net.ipv4.tcp_congestion_control=bbr from /etc/sysctl.conf
  96.    sudo sed -i "s/^net\.core\.default_qdisc\=fq//" /etc/sysctl.conf
  97.    # remove trailing empty lines
  98.    sudo sed -Ezi '$ s/\n+$//' /etc/sysctl.conf
  99.    echo -e "" | sudo tee -a /etc/sysctl.conf
  100.    echo "net.core.default_qdisc=fq removed from /etc/sysctl.conf"
  101. else
  102.    echo "net.core.default_qdisc=fq not found in /etc/sysctl.conf"
  103. fi
  104. # if we find "net.ipv4.tcp_congestion_control=bbr" in /etc/sysctl.conf
  105. if sudo grep -Fq "net.ipv4.tcp_congestion_control=bbr" /etc/sysctl.conf
  106. then
  107.    # note would be nice to get sed to do this in one line
  108.    # remove net.ipv4.tcp_congestion_control=bbr from /etc/sysctl.conf
  109.    sudo sed -i "s/^net\.ipv4\.tcp_congestion_control\=bbr//" /etc/sysctl.conf
  110.    # remove trailing empty lines
  111.    sudo sed -Ezi '$ s/\n+$//' /etc/sysctl.conf
  112.    echo -e "" | sudo tee -a /etc/sysctl.conf
  113.    echo "net.ipv4.tcp_congestion_control=bbr removed from /etc/sysctl.conf"
  114. else
  115.    echo "net.ipv4.tcp_congestion_control=bbr not found in /etc/sysctl.conf"
  116. fi
  117.  
  118. # Reload configuration
  119. sudo sysctl -qp
  120.  
  121. if ! sudo grep -Fq "net.core.default_qdisc=fq" /etc/sysctl.conf && ! sudo grep -Fq "net.ipv4.tcp_congestion_control=bbr" /etc/sysctl.conf ; then echo -e "\033[0;32mSuccess\033[m" ; exit 0; else echo -e "\033[0;31mFailed\033[m" ; exit 1; fi
  122. EOF
  123.  
  124. ls
Add Comment
Please, Sign In to add comment