- How do I set advanced security options of the TCP/IP stack and virtual memory to improve security and performance of my system? How do I configure Linux kernel to prevent certain kinds of attacks using /etc/sysctl.conf? How do I set Linux kernel parameters?
- 怎样设置 TCP/IP 堆栈和虚拟内存的高级的安全选项,以提高我的系统的安全性和性能?如何配置以防止某些类型的攻击,使用 /etc/sysctl.conf 的 Linux 内核?如何设置 Linux 内核参数?
- sysctl is an interface that allows you to make changes to a running Linux kernel. With /etc/sysctl.conf you can configure various Linux networking and system settings such as:
- Limit network-transmitted configuration for IPv4
- Limit network-transmitted configuration for IPv6
- Turn on execshield protection
- Prevent against the common ‘syn flood attack’
- Turn on source IP address verification
- Prevents a cracker from using a spoofing attack against the IP address of the server.
- Logs several types of suspicious packets, such as spoofed packets, source-routed packets, and redirects.
- sysctl 是允许您更改运行的 Linux 内核的一个借口。通过 /etc/sysctl.conf,您可以配置各种 Linux 网络和系统设置,如:
- 限制 IPv4 的网络传输配置
- 限制 IPv6 的网络传输配置
- 打开 execshield 保护
- 防止常见 syn 洪水攻击
- 打开源 IP 地址验证
- 可以防止骇客使用服务器的 IP 地址欺骗攻击。
- 记录几种类型的可疑的数据包,如伪造的数据包,源路由的数据包和重定向。
- sysctl command
- The sysctl command is used to modify kernel parameters at runtime. /etc/sysctl.conf is a text file containing sysctl values to be read in and set by sysct at boot time. To view current values, enter:
- # sysctl -a
- # sysctl -A
- # sysctl mib
- # sysctl net.ipv4.conf.all.rp_filter
- To load settings, enter:
- # sysctl -p
- Sample /etc/sysctl.conf
- Edit /etc/sysctl.conf and update it as follows. The file is documented with comments. However, I recommend reading the official Linux kernel sysctl tuning help file (see below):
- # The following is suitable for dedicated web server, mail, ftp server etc.
- # —————————————
- # BOOLEAN Values:
- # a) 0 (zero) – disabled / no / false
- # b) Non zero – enabled / yes / true
- # ————————————–
- # Controls IP packet forwarding
- net.ipv4.ip_forward = 0
- # Controls source route verification
- net.ipv4.conf.default.rp_filter = 1
- # Do not accept source routing
- net.ipv4.conf.default.accept_source_route = 0
- # Controls the System Request debugging functionality of the kernel
- kernel.sysrq = 0
- # Controls whether core dumps will append the PID to the core filename
- # Useful for debugging multi-threaded applications
- kernel.core_uses_pid = 1
- # Controls the use of TCP syncookies
- #net.ipv4.tcp_syncookies = 1
- net.ipv4.tcp_synack_retries = 2
- ########## IPv4 networking start ##############
- # Send redirects, if router, but this is just server
- net.ipv4.conf.all.send_redirects = 0
- net.ipv4.conf.default.send_redirects = 0
- # Accept packets with SRR option? No
- net.ipv4.conf.all.accept_source_route = 0
- # Accept Redirects? No, this is not router
- net.ipv4.conf.all.accept_redirects = 0
- net.ipv4.conf.all.secure_redirects = 0
- # Log packets with impossible addresses to kernel log? yes
- net.ipv4.conf.all.log_martians = 1
- net.ipv4.conf.default.accept_source_route = 0
- net.ipv4.conf.default.accept_redirects = 0
- net.ipv4.conf.default.secure_redirects = 0
- # Ignore all ICMP ECHO and TIMESTAMP requests sent to it via broadcast/multicast
- net.ipv4.icmp_echo_ignore_broadcasts = 1
- # Prevent against the common ‘syn flood attack’
- net.ipv4.tcp_syncookies = 1
- # Enable source validation by reversed path, as specified in RFC1812
- net.ipv4.conf.all.rp_filter = 1
- net.ipv4.conf.default.rp_filter = 1
- ########## IPv6 networking start ##############
- # Number of Router Solicitations to send until assuming no routers are present.
- # This is host and not router
- net.ipv6.conf.default.router_solicitations = 0
- # Accept Router Preference in RA?
- net.ipv6.conf.default.accept_ra_rtr_pref = 0
- # Learn Prefix Information in Router Advertisement
- net.ipv6.conf.default.accept_ra_pinfo = 0
- # Setting controls whether the system will accept Hop Limit settings from a router advertisement
- net.ipv6.conf.default.accept_ra_defrtr = 0
- #router advertisements can cause the system to assign a global unicast address to an interface
- net.ipv6.conf.default.autoconf = 0
- #how many neighbor solicitations to send out per address?
- net.ipv6.conf.default.dad_transmits = 0
- # How many global unicast IPv6 addresses can be assigned to each interface?
- net.ipv6.conf.default.max_addresses = 1
- ########## IPv6 networking ends ##############
- #Enable ExecShield protection
- kernel.exec-shield = 1
- kernel.randomize_va_space = 1
- # TCP and memory optimization
- # increase TCP max buffer size setable using setsockopt()
- #net.ipv4.tcp_rmem = 4096 87380 8388608
- #net.ipv4.tcp_wmem = 4096 87380 8388608
- # increase Linux auto tuning TCP buffer limits
- #net.core.rmem_max = 8388608
- #net.core.wmem_max = 8388608
- #net.core.netdev_max_backlog = 5000
- #net.ipv4.tcp_window_scaling = 1
- # increase system file descriptor limit
- fs.file-max = 65535
- #Allow for more PIDs
- kernel.pid_max = 65536
- #Increase system IP port limits
- net.ipv4.ip_local_port_range = 2000 65000
- This entry was posted in 内核参数, 系统性能 and tagged Iptables, systcl. Bookmark the permalink.
- Sysctl是一个允许您改变正在运行中的Linux系统的接口。它包含一些 TCP/IP 堆栈和虚拟内存系统的高级选项,这可以让有经验的管理员提高引人注目的系统性能。用sysctl可以读取设置超过五百个系统变量。基于这 点,sysctl提供两个功能:读取和修改系统设置。vim /etc/sysctl.conf
- kernel.shmall = 268435456
- net.ipv4.tcp_syncookies = 1
- net.ipv4.tcp_tw_reuse = 1
- net.ipv4.tcp_tw_recycle = 1
- net.ipv4.tcp_fin_timeout = 30
- net.ipv4.tcp_keepalive_time = 1200
- net.ipv4.ip_local_port_range = 1024 65000
- net.ipv4.tcp_max_tw_buckets = 5000
- net.ipv4.tcp_max_tw_buckets = 5000
- net.ipv4.tcp_fin_timeout = 30
- net.ipv4.tcp_keepalive_time = 300
- net.ipv4.tcp_syncookies = 1
- net.ipv4.tcp_tw_reuse = 1
- net.ipv4.tcp_tw_recycle = 1
- net.ipv4.ip_local_port_range = 5000 65000
- net.ipv4.tcp_mem = 786432 1048576 1572864
- net.core.wmem_max = 873200
- net.core.rmem_max = 873200
- net.ipv4.tcp_wmem = 8192 436600 873200
- net.ipv4.tcp_rmem = 32768 436600 873200
- net.core.somaxconn = 256
- net.core.netdev_max_backlog = 1000
- net.ipv4.tcp_max_syn_backlog = 2048
- net.ipv4.tcp_retries2 = 5
- net.ipv4.tcp_keepalive_time = 500
- net.ipv4.tcp_keepalive_intvl = 30
- net.ipv4.tcp_keepalive_probes = 3
- net.ipv4.conf.lo.arp_ignore = 0
- net.ipv4.conf.lo.arp_announce = 0
- net.ipv4.conf.all.arp_ignore = 0
- net.ipv4.conf.all.arp_announce = 0
- 几个解释:
- net.ipv4.tcp_syncookies = 1
- #表示开启SYN Cookies。当出现SYN等待队列溢出时,启用cookies来处理,可防范少量SYN攻击,默认为0,表示关闭;
- net.ipv4.tcp_tw_reuse = 1
- #表示开启重用。允许将TIME-WAIT sockets重新用于新的TCP连接,默认为0,表示关闭;
- net.ipv4.tcp_tw_recycle = 1
- #表示开启TCP连接中TIME-WAIT sockets的快速回收,默认为0,表示关闭。
- net.ipv4.tcp_fin_timeout = 30
- #表示如果套接字由本端要求关闭,这个参数决定了它保持在FIN-WAIT-2状态的时间。
- net.ipv4.tcp_keepalive_time = 1200
- #表示当keepalive起用的时候,TCP发送keepalive消息的频度。缺省是2小时,改为20分钟。
- net.ipv4.ip_local_port_range = 1024 65000
- #表示用于向外连接的端口范围。缺省情况下很小:32768到61000,改为1024到65000。
- net.ipv4.tcp_max_tw_buckets = 5000
- #表示系统同时保持TIME_WAIT套接字的最大数量,如果超过这个数字,
- #TIME_WAIT套接字将立刻被清除并打印警告信息。默认为180000,改为5000。
- #对于Apache、Nginx等服务器,上几行的参数可以很好地减少TIME_WAIT套接字数量,
- #但是对于Squid,效果却不大。此项参数可以控制TIME_WAIT套接字的最大数量,避免Squid服务器被大量的TIME_WAIT套接字拖死。