{"id":5904,"date":"2016-05-08T12:40:31","date_gmt":"2016-05-08T04:40:31","guid":{"rendered":"http:\/\/rmohan.com\/?p=5904"},"modified":"2016-05-08T12:40:31","modified_gmt":"2016-05-08T04:40:31","slug":"sysctl-centos-7","status":"publish","type":"post","link":"https:\/\/mohan.sg\/?p=5904","title":{"rendered":"sysctl centos 7"},"content":{"rendered":"<p>What is sysctl?<\/p>\n<p>sysctl is an interface to view and dynamically change parameters in Linux and other *NIX operating systems. In Linux, most of the dynamic Kernel settings can be changed via sysctl. The parameters set by sysctl are also available under the virtual \/proc filesystem.<\/p>\n<p>How do I use sysctl?<\/p>\n<p>To read values you\u2019ve two options:<\/p>\n<p>reading parametersShell<\/p>\n<p># Option 1: Using the sysctl command to read current parameters:<br \/>\nsysctl net.ipv4.ip_forward          # display specific parameter<br \/>\nsysctl net.ipv4                     # display all net.ipv4.* parameters<br \/>\nsysctl -a                           # display all parameters<\/p>\n<p># Option 2: Using the \/proc filesystem:<br \/>\ncat \/proc\/sys\/net\/ipv4\/ip_forward<\/p>\n<p># Option 1: Using the sysctl command to read current parameters:<br \/>\nsysctl net.ipv4.ip_forward          # display specific parameter<br \/>\nsysctl net.ipv4                     # display all net.ipv4.* parameters<br \/>\nsysctl -a                           # display all parameters<\/p>\n<p># Option 2: Using the \/proc filesystem:<br \/>\ncat \/proc\/sys\/net\/ipv4\/ip_forward<br \/>\nTo write values you can use both options again:<\/p>\n<p>changing parametersShell<\/p>\n<p># Option 1: Using the sysctl command to change a parameter:<br \/>\nsysctl net.ipv4.ip_forward=1<\/p>\n<p># Option 2: Using the \/proc filesystem to change a parameter:<br \/>\necho 1 >\/proc\/sys\/net\/ipv4\/ip_forward<br \/>\n1<br \/>\n2<br \/>\n3<br \/>\n4<br \/>\n5<br \/>\n# Option 1: Using the sysctl command to change a parameter:<br \/>\nsysctl net.ipv4.ip_forward=1<\/p>\n<p># Option 2: Using the \/proc filesystem to change a parameter:<br \/>\necho 1 >\/proc\/sys\/net\/ipv4\/ip_forward<br \/>\nHowever, these parameters are not persistent. You\u2019ve to configure them in \/etc\/sysctl.conf or \/etc\/sysctl.d\/* if you want them active after a reboot.<\/p>\n<p>sysctl configuration files<\/p>\n<p>\/etc\/sysctl.conf<br \/>\n\/etc\/sysctl.d\/<br \/>\n1<br \/>\n2<br \/>\n\/etc\/sysctl.conf<br \/>\n\/etc\/sysctl.d\/<br \/>\nPlease note that configuration changes will not be detected automatically. You\u2019ve to trigger the reload manually:<\/p>\n<p>reload sysctl configuration fileShell<\/p>\n<p>sysctl -p [filename]<br \/>\n1<br \/>\nsysctl -p [filename]<br \/>\nTuning Linux with sysctl<\/p>\n<p>Kernel<\/p>\n<p>To automatically reboot a system after a kernel panic, you can set the following parameter to the amount of seconds to wait before reboot:<\/p>\n<p>reboot system after kernel panicShell<\/p>\n<p>kernel.panic = 60<\/p>\n<p>kernel.panic = 60<br \/>\nLinux Kernels provide a magic SysRq key, which allows the user to perform low-level commands regardless of the systems state. To enable this magic key you\u2019ve to set:<\/p>\n<p>enable magic SysRq keyShell<\/p>\n<p>kernel.sysrq = 1<\/p>\n<p>kernel.sysrq = 1<br \/>\nTo make sure core dumps will always be written set the following parameter:<\/p>\n<p>write core dumpsShell<\/p>\n<p>fs.suid_dumpable = 2<\/p>\n<p>fs.suid_dumpable = 2<br \/>\nIt can be useful to have the PID appended on the filename of core dumps. This can be especially useful for debugging multi-threaded applications and it\u2019s easy to setup:<\/p>\n<p>add PID to core dumpsShell<\/p>\n<p>kernel.core_uses_pid = 1<\/p>\n<p>kernel.core_uses_pid = 1<br \/>\nTo increase the maximum number of used process IDs you can define the following parameter:<\/p>\n<p>increase maximum PIDShell<\/p>\n<p>kernel.pid_max = 65536<\/p>\n<p>kernel.pid_max = 65536<br \/>\nMemory<\/p>\n<p>To tune the memory (VM) behaviour in Linux, you can set some  vm.* parameters.<\/p>\n<p>For example to tell the Kernel how aggressively memory pages should be written to disk (aka swapping), you\u2019ve to change the swappiness value. The higher the value, the more aggressive the swapping:<\/p>\n<p>swappinessShell<\/p>\n<p>vm.swappiness<br \/>\n1<br \/>\nvm.swappiness<br \/>\nWhen you look at filesystems then most of the time some kind of cache is involved. The amount of filesystem cache is based on the percentage of total available memory. To set the maximum amount of filesystem cache can be defined with:<\/p>\n<p>maximum filesystem cacheShell<\/p>\n<p>vm.dirty_ratio = 40<\/p>\n<p>vm.dirty_ratio = 40<br \/>\nWhen the defined percentage of memory is reached, then all I\/O writes are blocked until enough dirty pages have been flushed to disk by pdflush. This is quite suboptimal because on a healthy system you don\u2019t want to have blocked I\/O writes at all. Therefor there\u2019s another parameter, which defines the minimal percentage of dirty memory before the background pdflush process starts to flush out dirty memory pages:<\/p>\n<p>background filesystem cache flushesShell<\/p>\n<p>vm.dirty_background_ratio = 10<\/p>\n<p>vm.dirty_background_ratio = 10<br \/>\nAs already described before, pdflush is in charge of flushing dirty pages to disk. So you can optionally change the flush interval by setting the following parameter (in hundredths of seconds, e.g. 500 = 5s):<\/p>\n<p>pdflush intervalShell<\/p>\n<p>vm.dirty_writeback_centisecs = 500<\/p>\n<p>vm.dirty_writeback_centisecs = 500<br \/>\nOf course pdflush needs to know when data can be removed from cache. Sometimes it makes sense to increase the time how long \u201cuntouched\u201d data lives be in the cache before it\u2019s marked as expired. Just overwrite the following parameter (again in hundredths of seconds):<\/p>\n<p>pdflush intervalShell<\/p>\n<p>vm.dirty_expire_centiseconds = 3000<\/p>\n<p>vm.dirty_expire_centiseconds = 3000<br \/>\nIf you want to have more informations about the memory on your system, just have a look at:<\/p>\n<p>display memory informationsShell<\/p>\n<p>cat \/proc\/meminfo<\/p>\n<p>cat \/proc\/meminfo<br \/>\nFilesystem<\/p>\n<p>To increase the maximum amount of file descriptors you can use.<\/p>\n<p>increase maximum filedescriptorsShell<\/p>\n<p>fs.file-max = 65535<\/p>\n<p>fs.file-max = 65535<br \/>\nExec Shield<\/p>\n<p>Exec Shield is a protection against worms and other automated remote attacks on Linux systems. It was invented by Red Hat in 2002. To enable Exec Shield:<\/p>\n<p>enable Exec Shield protectionShell<\/p>\n<p>kernel.exec-shield = 1<br \/>\nkernel.randomize_va_space = 1<\/p>\n<p>kernel.exec-shield = 1<br \/>\nkernel.randomize_va_space = 1<br \/>\nNetwork Core<\/p>\n<p>Some applications are configured for performance and sometimes an application can handle huge buffers. To increase the maximum buffer size for all sockets \/ connections (this will affect all buffers, e.g. net.ipv4.tcp_rmem) you can use:<\/p>\n<p>increase max buffer sizeShell<\/p>\n<p>net.core.rmem_max = 8388608<br \/>\nnet.core.wmem_max = 8388608<\/p>\n<p>net.core.rmem_max = 8388608<br \/>\nnet.core.wmem_max = 8388608<br \/>\nWhen a system is under heavy load and an interface receives a lot of packets, then the Kernel might not process them fast enough. You can increase the number of packets hold in the queue (backlog) by changing:<\/p>\n<p>increase maximum backlog size for net devicesShell<\/p>\n<p>net.core.netdev_max_backlog = 5000<\/p>\n<p>net.core.netdev_max_backlog = 5000<br \/>\nIPv4<\/p>\n<p>First of all we recommend you tune ICMP a bit. You can do that by ignoring ICMP broadcasts, which will protect you from ICMP floods. We also ignore bogus responses to broadcast frames (violation against RFC1122), so that our log isn\u2019t full of Kernel warnings:<\/p>\n<p>hardening ICMPShell<\/p>\n<p>net.ipv4.icmp_echo_ignore_broadcasts = 1<br \/>\nnet.ipv4.icmp_ignore_bogus_error_responses = 1<\/p>\n<p>net.ipv4.icmp_echo_ignore_broadcasts = 1<br \/>\nnet.ipv4.icmp_ignore_bogus_error_responses = 1<br \/>\nSYN floods are a type of DDoS and can harm your system. To protect from it you should enable SYN cookies, resize the SYN backlog (queue size) and reduce SYN\/ACK retries:<\/p>\n<p>enable SYN cookiesShell<\/p>\n<p># Turn on SYN cookies to protect from SYN flood attacks.<br \/>\nnet.ipv4.tcp_syncookies = 1<br \/>\nnet.ipv4.tcp_max_syn_backlog = 2048<br \/>\nnet.ipv4.tcp_synack_retries = 3<\/p>\n<p># Turn on SYN cookies to protect from SYN flood attacks.<br \/>\nnet.ipv4.tcp_syncookies = 1<br \/>\nnet.ipv4.tcp_max_syn_backlog = 2048<br \/>\nnet.ipv4.tcp_synack_retries = 3<br \/>\nTo log packets with impossible addresses simply enable:<\/p>\n<p>log impossible IPv4 addressesShell<\/p>\n<p>net.ipv4.conf.all.log_martians = 1<br \/>\nnet.ipv4.conf.default.log_martians = 1<\/p>\n<p>net.ipv4.conf.all.log_martians = 1<br \/>\nnet.ipv4.conf.default.log_martians = 1<br \/>\nTo disable IP source routing (SRR), so that nobody can tell us which path a packet should take:<\/p>\n<p>deny packets with SRR optionShell<\/p>\n<p>net.ipv4.conf.all.accept_source_route = 0<br \/>\nnet.ipv4.conf.default.accept_source_route  = 0<\/p>\n<p>net.ipv4.conf.all.accept_source_route = 0<br \/>\nnet.ipv4.conf.default.accept_source_route  = 0<br \/>\nBy default, routers router everything and even packages which don\u2019t belong to their network(s). To avoid that we\u2019ve to make sure strict reverse path filtering is enabled as defined in RFC3704:<\/p>\n<p>enable strict reverse path filteringShell<\/p>\n<p>net.ipv4.conf.all.rp_filter = 1<br \/>\nnet.ipv4.conf.default.rp_filter = 1<\/p>\n<p>net.ipv4.conf.all.rp_filter = 1<br \/>\nnet.ipv4.conf.default.rp_filter = 1<br \/>\nSome applications support higher read and write buffers for sockets. The buffer size parameters are defined by 3 values (min, default, max). To increase the maximum buffer set:<\/p>\n<p>increase max TCP buffer sizeShell<\/p>\n<p>net.ipv4.tcp_rmem = 4096 87380 8388608<br \/>\nnet.ipv4.tcp_wmem = 4096 87380 8388608<\/p>\n<p>net.ipv4.tcp_rmem = 4096 87380 8388608<br \/>\nnet.ipv4.tcp_wmem = 4096 87380 8388608<br \/>\nTo get better throughput in a network, it might make sense to enable TCP window scaling as defined in RFC1323:<\/p>\n<p>enable TCP window scalingShell<\/p>\n<p>net.ipv4.tcp_window_scaling = 1<\/p>\n<p>net.ipv4.tcp_window_scaling = 1<br \/>\nDisable (ICMP) redirects at all. Please note that the send_redirects parameters should be enabled on routers:<\/p>\n<p>disable redirectsShell<\/p>\n<p>net.ipv4.conf.all.accept_redirects = 0<br \/>\nnet.ipv4.conf.default.accept_redirects = 0<br \/>\nnet.ipv4.conf.all.secure_redirects = 0<br \/>\nnet.ipv4.conf.default.secure_redirects = 0<br \/>\nnet.ipv4.conf.all.send_redirects = 0        # Don&#8217;t disable this on routers!<br \/>\nnet.ipv4.conf.default.send_redirects = 0    # Don&#8217;t disable this on routers!<\/p>\n<p>net.ipv4.conf.all.accept_redirects = 0<br \/>\nnet.ipv4.conf.default.accept_redirects = 0<br \/>\nnet.ipv4.conf.all.secure_redirects = 0<br \/>\nnet.ipv4.conf.default.secure_redirects = 0<br \/>\nnet.ipv4.conf.all.send_redirects = 0        # Don&#8217;t disable this on routers!<br \/>\nnet.ipv4.conf.default.send_redirects = 0    # Don&#8217;t disable this on routers!<br \/>\nFinally disable IPv4 forwarding on non-routing systems:<\/p>\n<p>disable forwardingShell<\/p>\n<p>net.ipv4.ip_forward = 0<\/p>\n<p>net.ipv4.ip_forward = 0<br \/>\nIPv6<\/p>\n<p>Those who don\u2019t use IPv6 at all should disable it:<\/p>\n<p>disable IPv6Shell<\/p>\n<p>net.ipv6.conf.all.disable_ipv6 = 1<\/p>\n<p>net.ipv6.conf.all.disable_ipv6 = 1<br \/>\nIf you\u2019re already using IPv6 you might be interested in the following parameters.<\/p>\n<p>On non-routing systems you should disable router solicitations:<\/p>\n<p>disable router solicitationsShell<\/p>\n<p>net.ipv6.conf.default.router_solicitations = 0<br \/>\nnet.ipv6.conf.all.router_solicitations = 0<\/p>\n<p>net.ipv6.conf.default.router_solicitations = 0<br \/>\nnet.ipv6.conf.all.router_solicitations = 0<br \/>\nYou should also don\u2019t accept routing preferences from router advertisements:<\/p>\n<p>disable router preferences in RAShell<\/p>\n<p>net.ipv6.conf.default.accept_ra_rtr_pref = 0<br \/>\nnet.ipv6.conf.all.accept_ra_rtr_pref = 0<\/p>\n<p>net.ipv6.conf.default.accept_ra_rtr_pref = 0<br \/>\nnet.ipv6.conf.all.accept_ra_rtr_pref = 0<br \/>\nDon\u2019t try to learn prefix information in router advertisements:<\/p>\n<p>don&#8217;t learn prefix informations in RAShell<\/p>\n<p>net.ipv6.conf.default.accept_ra_pinfo = 0<br \/>\nnet.ipv6.conf.all.accept_ra_pinfo = 0<\/p>\n<p>net.ipv6.conf.default.accept_ra_pinfo = 0<br \/>\nnet.ipv6.conf.all.accept_ra_pinfo = 0<br \/>\nDon\u2019t accept hop limits from router advertisements:<\/p>\n<p>don&#8217;t accept hop limits from RAShell<\/p>\n<p>net.ipv6.conf.default.accept_ra_defrtr = 0<br \/>\nnet.ipv6.conf.all.accept_ra_defrtr = 0<\/p>\n<p>net.ipv6.conf.default.accept_ra_defrtr = 0<br \/>\nnet.ipv6.conf.all.accept_ra_defrtr = 0<br \/>\nDisable IPv6 auto configuration, so that no unicast addresses can automatically be configured on your interface from a router advertisement:<\/p>\n<p>disable auto configuration from RAShell<\/p>\n<p>net.ipv6.conf.default.autoconf = 0<br \/>\nnet.ipv6.conf.all.autoconf = 0<\/p>\n<p>net.ipv6.conf.default.autoconf = 0<br \/>\nnet.ipv6.conf.all.autoconf = 0<br \/>\nIf you don\u2019t want your system to be verbose about its neighbours, you should disable neighbour solicitations at all:<\/p>\n<p>disable auto configuration from RAShell<\/p>\n<p>net.ipv6.conf.default.dad_transmits = 0<br \/>\nnet.ipv6.conf.all.dad_transmits = 0<\/p>\n<p>net.ipv6.conf.default.dad_transmits = 0<br \/>\nnet.ipv6.conf.all.dad_transmits = 0<br \/>\nUnless you need more than one global unicast address, you should fix the number of assigned global unicast addresses per interface to 1:<\/p>\n<p>disable auto configuration from RAShell<\/p>\n<p>net.ipv6.conf.default.max_addresses = 1<br \/>\nnet.ipv6.conf.all.max_addresses = 1<\/p>\n<p>net.ipv6.conf.default.max_addresses = 1<br \/>\nnet.ipv6.conf.all.max_addresses = 1<br \/>\n.all &#038; .default<\/p>\n<p>A lot of sysctl parameters have several values, because there\u2019s a  .default, .all and sometimes even a .<interface> value. While the  .<interface> value is obvious, you\u2019ve to look closer on the other two.<\/p>\n<p>According to a comment on the linux-kernel mailing list, there\u2019s one major difference:<\/p>\n<p>The default value will only be applied ONCE, at the point when an interface is created.<br \/>\nThe all value will ALWAYS applied in addition.<br \/>\nThis means when an interface is created, the default value will be applied to it once. However, you can overwrite that with the interface-specific parameter. The global  .all parameter will always be applied in addition and in the end it depends of the logical operator how the \u201cfinal value\u201d looks like.<\/p>\n<p>For example there are parameters where all settings need to be 1 (aka  AND), where only one of the settings need to be 1 (aka  OR) or where the highest value will be used (aka MAX).<\/p>\n<p>So it\u2019s important to know that existing interfaces might have a different value than the one you\u2019ve set as default or all.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>What is sysctl?<\/p>\n<p>sysctl is an interface to view and dynamically change parameters in Linux and other *NIX operating systems. In Linux, most of the dynamic Kernel settings can be changed via sysctl. The parameters set by sysctl are also available under the virtual \/proc filesystem.<\/p>\n<p>How do I use sysctl?<\/p>\n<p>To read values you\u2019ve [&#8230;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"_links":{"self":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/5904"}],"collection":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5904"}],"version-history":[{"count":1,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/5904\/revisions"}],"predecessor-version":[{"id":5905,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/5904\/revisions\/5905"}],"wp:attachment":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5904"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5904"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5904"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}