sysctl

时动态地修改内核的运行参数

补充说明

sysctl命令 被用于在内核运行时动态地修改内核的运行参数,可用的内核参数在目录/proc/sys中。它包含一些TCP/ip堆栈和虚拟内存系统的高级选项, 这可以让有经验的管理员提高引人注目的系统性能。用sysctl可以读取设置超过五百个系统变量。

语法

  1. sysctl(选项)(参数)

选项

  1. -n:打印值时不打印关键字;
  2. -e:忽略未知关键字错误;
  3. -N:仅打印名称;
  4. -w:当改变sysctl设置时使用此项;
  5. -p:从配置文件“/etc/sysctl.conf”加载内核参数设置;
  6. -a:打印当前所有可用的内核参数变量和值;
  7. -A:以表格方式打印当前所有可用的内核参数变量和值。

参数

变量=值:设置内核参数对应的变量值。

实例

查看所有可读变量:

sysctl -a

读一个指定的变量,例如kern.maxproc

sysctl kern.maxproc kern.maxproc: 1044

要设置一个指定的变量,直接用variable=value这样的语法:

  1. sysctl kern.maxfiles=5000
  2. kern.maxfiles: 2088 -> 5000

您可以使用sysctl修改系统变量,也可以通过编辑sysctl.conf文件来修改系统变量。sysctl.conf看起来很像rc.conf。它用variable=value的形式来设定值。指定的值在系统进入多用户模式之后被设定。并不是所有的变量都可以在这个模式下设定。

sysctl变量的设置通常是字符串、数字或者布尔型。(布尔型用 1 来表示’yes’,用 0 来表示’no’)。

  1. sysctl -w kernel.sysrq=0
  2. sysctl -w kernel.core_uses_pid=1
  3. sysctl -w net.ipv4.conf.default.accept_redirects=0
  4. sysctl -w net.ipv4.conf.default.accept_source_route=0
  5. sysctl -w net.ipv4.conf.default.rp_filter=1
  6. sysctl -w net.ipv4.tcp_syncookies=1
  7. sysctl -w net.ipv4.tcp_max_syn_backlog=2048
  8. sysctl -w net.ipv4.tcp_fin_timeout=30
  9. sysctl -w net.ipv4.tcp_synack_retries=2
  10. sysctl -w net.ipv4.tcp_keepalive_time=3600
  11. sysctl -w net.ipv4.tcp_window_scaling=1
  12. sysctl -w net.ipv4.tcp_sack=1

配置sysctl

编辑此文件:/etc/sysctl.conf

如果该文件为空,则输入以下内容,否则请根据情况自己做调整:

  1. # Controls source route verification
  2. # Default should work for all interfaces
  3. net.ipv4.conf.default.rp_filter = 1
  4. # net.ipv4.conf.all.rp_filter = 1
  5. # net.ipv4.conf.lo.rp_filter = 1
  6. # net.ipv4.conf.eth0.rp_filter = 1
  7. # Disables IP source routing
  8. # Default should work for all interfaces
  9. net.ipv4.conf.default.accept_source_route = 0
  10. # net.ipv4.conf.all.accept_source_route = 0
  11. # net.ipv4.conf.lo.accept_source_route = 0
  12. # net.ipv4.conf.eth0.accept_source_route = 0
  13. # Controls the System Request debugging functionality of the kernel
  14. kernel.sysrq = 0
  15. # Controls whether core dumps will append the PID to the core filename.
  16. # Useful for debugging multi-threaded applications.
  17. kernel.core_uses_pid = 1
  18. # Increase maximum amount of memory allocated to shm
  19. # Only uncomment if needed!
  20. # kernel.shmmax = 67108864
  21. # Disable ICMP Redirect Acceptance
  22. # Default should work for all interfaces
  23. net.ipv4.conf.default.accept_redirects = 0
  24. # net.ipv4.conf.all.accept_redirects = 0
  25. # net.ipv4.conf.lo.accept_redirects = 0
  26. # net.ipv4.conf.eth0.accept_redirects = 0
  27. # enable Log Spoofed Packets, Source Routed Packets, Redirect Packets
  28. # Default should work for all interfaces
  29. net.ipv4.conf.default.log_martians = 1
  30. # net.ipv4.conf.all.log_martians = 1
  31. # net.ipv4.conf.lo.log_martians = 1
  32. # net.ipv4.conf.eth0.log_martians = 1
  33. # Decrease the time default value for tcp_fin_timeout connection
  34. net.ipv4.tcp_fin_timeout = 25
  35. # Decrease the time default value for tcp_keepalive_time connection
  36. net.ipv4.tcp_keepalive_time = 1200
  37. # Turn on the tcp_window_scaling
  38. net.ipv4.tcp_window_scaling = 1
  39. # Turn on the tcp_sack
  40. net.ipv4.tcp_sack = 1
  41. # tcp_fack should be on because of sack
  42. net.ipv4.tcp_fack = 1
  43. # Turn on the tcp_timestamps
  44. net.ipv4.tcp_timestamps = 1
  45. # Enable TCP SYN Cookie Protection
  46. net.ipv4.tcp_syncookies = 1
  47. # Enable ignoring broadcasts request
  48. net.ipv4.icmp_echo_ignore_broadcasts = 1
  49. # Enable bad error message Protection
  50. net.ipv4.icmp_ignore_bogus_error_responses = 1
  51. # make more local ports available
  52. # net.ipv4.ip_local_port_range = 1024 65000
  53. # set TCP Re-Ordering value in kernel to ‘5′
  54. net.ipv4.tcp_reordering = 5
  55. # Lower syn retry rates
  56. net.ipv4.tcp_synack_retries = 2
  57. net.ipv4.tcp_syn_retries = 3
  58. # Set Max SYN Backlog to ‘2048′
  59. net.ipv4.tcp_max_syn_backlog = 2048
  60. # Various Settings
  61. net.core.netdev_max_backlog = 1024
  62. # Increase the maximum number of skb-heads to be cached
  63. net.core.hot_list_length = 256
  64. # Increase the tcp-time-wait buckets pool size
  65. net.ipv4.tcp_max_tw_buckets = 360000
  66. # This will increase the amount of memory available for socket input/output queues
  67. net.core.rmem_default = 65535
  68. net.core.rmem_max = 8388608
  69. net.ipv4.tcp_rmem = 4096 87380 8388608
  70. net.core.wmem_default = 65535
  71. net.core.wmem_max = 8388608
  72. net.ipv4.tcp_wmem = 4096 65535 8388608
  73. net.ipv4.tcp_mem = 8388608 8388608 8388608
  74. net.core.optmem_max = 40960

如果希望屏蔽别人 ping 你的主机,则加入以下代码:

  1. # Disable ping requests
  2. net.ipv4.icmp_echo_ignore_all = 1

编辑完成后,请执行以下命令使变动立即生效:

  1. /sbin/sysctl -p
  2. /sbin/sysctl -w net.ipv4.route.flush=1