sysctl_config.sh 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. #!/bin/bash
  2. # vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4:
  3. #
  4. # Author : Nicolas Brousse <nicolas@brousse.info>
  5. # From : https://www.shell-tips.com/2010/09/13/linux-sysctl-configuration-and-tuning-script/
  6. #
  7. # Added kernel version < 2.6.33 set net.ipv4.tcp_congestion_control=htcp
  8. # Notes :
  9. # This script is a simple "helper" to configure your sysctl.conf on linux
  10. # There is no silver bullet. Don't expect the perfect setup, review comments
  11. # and adapt the parameters to your needs and application usage.
  12. #
  13. # Use this script at your OWN risk. There is no guarantee whatsoever.
  14. #
  15. # License :
  16. # This work is licenced under the CC-GNU LGPL version 2.1 or later.
  17. # To view a copy of this licence, visit http://creativecommons.org/licenses/LGPL/2.1/
  18. # or send a letter to :
  19. #
  20. # Creative Commons
  21. # 171 Second Street, Suite 300
  22. # San Francisco, California 94105, USA
  23. #
  24. # May 2012, Jon Zobrist <jon@jonzobrist.com> http://www.jonzobrist.com/
  25. # Things added :
  26. # Apache max file handlers update to /etc/security/limits.conf
  27. # Check and add pam_limits.so is loaded by the su program (as many things run via su)
  28. # Backing up of previous sysctl.conf file
  29. # Merging of previous sysctl.conf settings if new settings don't override
  30. # tcp_available_congestion_control detection and setting
  31. # Updates hosted on github at https://github.com/jonzobrist/Bash-Admin-Scripts
  32. host=$(hostname)
  33. ARCH=$(uname -m)
  34. KERNEL_STRING=$(uname -r | sed -e 's/[^0-9]/ /g')
  35. KERNEL_VERSION=$(echo "${KERNEL_STRING}" | awk '{ print $1 }')
  36. MAJOR_VERSION=$(echo "${KERNEL_STRING}" | awk '{ print $2 }')
  37. MINOR_VERSION=$(echo "${KERNEL_STRING}" | awk '{ print $3 }')
  38. echo "${KERNEL_VERSION}.${MAJOR_VERSION}.${MINOR_VERSION}"
  39. CURRENT_SYSCTL_FILE=/tmp/sysctl-existing-$(date +%F-%s)
  40. touch ${CURRENT_SYSCTL_FILE}
  41. #chmod og-rwx ${CURRENT_SYSCTL_FILE}
  42. grep -v '^#' /etc/sysctl.conf | grep . >> ${CURRENT_SYSCTL_FILE}
  43. BACKUP_SYSCTL="sysctl.conf-$(date +%F-%s)"
  44. echo "moving sysctl.conf to /etc/${BACKUP_SYSCTL}"
  45. mv /etc/sysctl.conf /etc/${BACKUP_SYSCTL}
  46. which bc
  47. if [ $? -ne 0 ]; then
  48. echo "This script require GNU bc, cf. http://www.gnu.org/software/bc/"
  49. echo "On Linux Debian/Ubuntu you can install it by doing : apt-get install bc"
  50. fi
  51. echo "Update sysctl for $host"
  52. mem_bytes=$(awk '/MemTotal:/ { printf "%0.f",$2 * 1024}' /proc/meminfo)
  53. shmmax=$(echo "$mem_bytes * 0.90" | bc | cut -f 1 -d '.')
  54. echo "shmmax = " $shmmax
  55. shmall=$(expr $mem_bytes / $(getconf PAGE_SIZE))
  56. echo "shmall = " $shmall
  57. max_orphan=$(echo "$mem_bytes * 0.10 / 65536" | bc | cut -f 1 -d '.')
  58. echo "max_orphan = " $max_orphan
  59. file_max=$(echo "$mem_bytes / 4194304 * 256" | bc | cut -f 1 -d '.')
  60. echo "file_max = " = $file_max
  61. max_tw=$(($file_max*2))
  62. echo "max_tw =" $max_tw
  63. min_free=$(echo "($mem_bytes / 1024) * 0.01" | bc | cut -f 1 -d '.')
  64. echo "min_free = " $min_free
  65. if [ "${KERNEL_VERSION}" -lt 3 ] && [ "${MAJOR_VERSION}" -lt 7 ] && [ "${MINOR_VERSION}" -lt 33 ]
  66. then
  67. CONGESTION_CONTROL="htcp"
  68. else
  69. if [ "$(sysctl net.ipv4.tcp_available_congestion_control | grep reno)" ]
  70. then
  71. CONGESTION_CONTROL="reno"
  72. else
  73. CONGESTION_CONTROL="cubic"
  74. fi
  75. fi
  76. if [ "$1" != "ssd" ]; then
  77. vm_dirty_bg_ratio=5
  78. vm_dirty_ratio=15
  79. else
  80. # This setup is generally ok for ssd and highmem servers
  81. vm_dirty_bg_ratio=3
  82. vm_dirty_ratio=5
  83. fi
  84. >/etc/sysctl.conf cat << EOF
  85. # manage by puppet
  86. # Kernel sysctl configuration file for Red Hat Linux
  87. #
  88. # For binary values, 0 is disabled, 1 is enabled. See sysctl(8) and
  89. # sysctl.conf(5) for more details.
  90. # Controls IP packet forwarding
  91. net.ipv4.ip_forward = 0
  92. # Controls source route verification
  93. net.ipv4.conf.default.rp_filter = 1
  94. # Controls the use of TCP syncookies
  95. net.ipv4.tcp_syncookies = 1
  96. # Do not accept source routing
  97. net.ipv4.conf.default.accept_source_route = 0
  98. # Disable source routing and redirects
  99. net.ipv4.conf.all.send_redirects = 0
  100. net.ipv4.conf.all.accept_redirects = 0
  101. net.ipv4.conf.all.accept_source_route = 0
  102. # Controls the System Request debugging functionality of the kernel
  103. kernel.sysrq = 0
  104. # Controls whether core dumps will append the PID to the core filename.
  105. # Useful for debugging multi-threaded applications.
  106. kernel.core_uses_pid = 1
  107. # Basic TCP tuning
  108. net.ipv4.tcp_keepalive_time = 600
  109. net.ipv4.tcp_synack_retries = 1
  110. net.ipv4.tcp_syn_retries = 1
  111. # Disable netfilter on bridges.
  112. # net.bridge.bridge-nf-call-ip6tables = 0
  113. # net.bridge.bridge-nf-call-iptables = 0
  114. # net.bridge.bridge-nf-call-arptables = 0
  115. # RFC1337
  116. net.ipv4.tcp_rfc1337 = 1
  117. # Defines the local port range that is used by TCP and UDP
  118. # to choose the local port
  119. net.ipv4.ip_local_port_range = 1024 65535
  120. # Log packets with impossible addresses for security
  121. net.ipv4.conf.all.log_martians = 1
  122. # Disable Explicit Congestion Notification in TCP
  123. net.ipv4.tcp_ecn = 0
  124. # Enable window scaling as defined in RFC1323
  125. net.ipv4.tcp_window_scaling = 1
  126. # Enable timestamps (RFC1323)
  127. net.ipv4.tcp_timestamps = 1
  128. # DISable select acknowledgments
  129. net.ipv4.tcp_sack = 0
  130. # Enable FACK congestion avoidance and fast restransmission
  131. net.ipv4.tcp_fack = 1
  132. # DISABLE Allows TCP to send "duplicate" SACKs
  133. net.ipv4.tcp_dsack = 0
  134. # No controls source route verification (RFC1812)
  135. net.ipv4.conf.default.rp_filter = 0
  136. # Make room for more TIME_WAIT sockets due to more clients,
  137. # and allow them to be reused if we run out of sockets
  138. # Also increase the max packet backlog
  139. net.core.netdev_max_backlog = 20000
  140. # TODO : change TCP_SYNQ_HSIZE in include/net/tcp.h
  141. # to keep TCP_SYNQ_HSIZE*16<=tcp_max_syn_backlog
  142. net.ipv4.tcp_max_syn_backlog = 65536
  143. net.core.somaxconn = 65000
  144. # Enable fast recycling TIME-WAIT sockets
  145. net.ipv4.tcp_tw_recycle = 1
  146. #/*打开快速回收time_wait状态的socket*/
  147. net.ipv4.tcp_tw_reuse = 1
  148. # tells the kernel how many TCP sockets that are not attached
  149. # to any user file handle to maintain
  150. net.ipv4.tcp_max_orphans = $max_orphan
  151. # How may times to retry before killing TCP connection, closed by our side
  152. net.ipv4.tcp_orphan_retries = 1
  153. # how long to keep sockets in the state FIN-WAIT-2
  154. # if we were the one closing the socket
  155. net.ipv4.tcp_fin_timeout = 10
  156. # maximum number of sockets in TIME-WAIT to be held simultaneously
  157. net.ipv4.tcp_max_tw_buckets = $max_tw
  158. # don't cache ssthresh from previous connection
  159. net.ipv4.tcp_no_metrics_save = 1
  160. net.ipv4.tcp_moderate_rcvbuf = 1
  161. # Increase Linux autotuning TCP buffer limits
  162. # Set max to 16MB for 1GE and 32M (33554432) or 54M (56623104) for 10GE
  163. # Don't set tcp_mem itself! Let the kernel scale it based on RAM.
  164. net.core.rmem_default = 16777216
  165. net.core.wmem_default = 16777216
  166. net.core.optmem_max = 40960
  167. # increase Linux autotuning TCP buffer limits
  168. net.ipv4.tcp_rmem = 4096 87380 16777216
  169. net.ipv4.tcp_wmem = 4096 65536 16777216
  170. # increase TCP max buffer size
  171. net.core.rmem_max = 16777216
  172. net.core.wmem_max = 16777216
  173. net.core.netdev_max_backlog = 2500
  174. net.core.somaxconn = 65000
  175. # Discourage Linux from swapping idle processes to disk (default = 60)
  176. vm.swappiness = 10
  177. # Disable TCP slow start on idle connections
  178. net.ipv4.tcp_slow_start_after_idle = 0
  179. # You can monitor the kernel behavior with regard to the dirty
  180. # pages by using grep -A 1 dirty /proc/vmstat
  181. vm.dirty_background_ratio = $vm_dirty_bg_ratio
  182. vm.dirty_ratio = $vm_dirty_ratio
  183. # required free memory (set to 1% of physical ram)
  184. vm.min_free_kbytes = $min_free
  185. vm.overcommit_memory = 1
  186. # system open file limit
  187. fs.file-max = $file_max
  188. # Core dump suidsafe
  189. fs.suid_dumpable = 2
  190. kernel.core_pattern = core.%e.%p.%t
  191. kernel.printk = 4 4 1 7
  192. kernel.core_uses_pid = 1
  193. kernel.sysrq = 0
  194. kernel.msgmax = 65536
  195. kernel.msgmnb = 65536
  196. # This file (new in Linux 2.5) specifies the value at which PIDs wrap around
  197. # (i.e., the value in this file is one greater than the maximum PID). The
  198. # default value for this file, 32768, results in the same range of PIDs as
  199. # on earlier kernels. On 32-bit platfroms, 32768 is the maximum value for
  200. # pid_max. On 64-bit systems, pid_max can be set to any value up to 2^22
  201. # (PID_MAX_LIMIT, approximately 4 million).
  202. kernel.pid_max = 4194303
  203. # Maximum shared segment size in bytes
  204. # Controls the maximum shared segment size, in bytes
  205. # On 64-bit systems, this is a theoretical 2^64bytes.
  206. # So the "theoretical limit" for SHMMAX is the amount of physical RAM that you have.
  207. # However, to actually attempt to use such a value could potentially lead to a situation where no system memory is available for anything else.
  208. # Therefore a more realistic "physical limit" for SHMMAX would probably be "physical RAM - 2Gb".
  209. # 60 * 1024 * 1024 * 1024
  210. kernel.shmmax = $shmmax
  211. # Maximum number of shared memory segments in pages
  212. kernel.shmall = $shmall
  213. net.ipv4.tcp_congestion_control=${CONGESTION_CONTROL}
  214. # net.nf_conntrack_max = 655360
  215. # net.netfilter.nf_conntrack_tcp_timeout_established = 20
  216. EOF
  217. SAVEIFS=$IFS
  218. IFS=$(echo -en "\n\b")
  219. for LINE in $(grep -v '^#' ${CURRENT_SYSCTL_FILE} | grep . )
  220. do
  221. unset RESULT
  222. MY_VAR=$(echo ${LINE} | awk '{ print $1 }')
  223. RESULT=$(grep ${MY_VAR} /etc/sysctl.conf)
  224. if [ "${RESULT}" ]
  225. then
  226. echo "${MY_VAR} exists in new sysctl.conf, skipping"
  227. else
  228. echo "Adding ${MY_VAR} from old sysctl.conf to new"
  229. echo "${LINE}"
  230. echo "${LINE}" >> /etc/sysctl.conf
  231. fi
  232. done
  233. IFS=$SAVEIFS
  234. ##
  235. # add mod ip_conntrack and bridge
  236. ##
  237. # modprobe ip_conntrack
  238. # modprobe bridge
  239. /sbin/sysctl -p /etc/sysctl.conf
  240. exit $?