Linux 内核 /etc/sysctl.conf 安全强化
如何设置 TCP/IP 堆栈和虚拟内存的高级安全选项,以提高基于 Linux 的系统的安全性和性能?如何配置 Linux 内核以防止使用 /etc/sysctl.conf 的某些类型的攻击?如何设置 Linux 内核参数?
sysctl 是一个允许您更改正在运行的 Linux 内核的接口。使用 /etc/sysctl.conf,您可以配置各种 Linux 网络和系统设置,例如:
让我们了解如何通过/etc/sysctl.conf文件保护 Linux 内核、网络堆栈和系统组件。
sysctl 是一个允许您更改正在运行的 Linux 内核的接口。使用 /etc/sysctl.conf,您可以配置各种 Linux 网络和系统设置,例如:
- 限制 IPv4 的网络传输配置
- 限制 IPv6 的网络传输配置
- 开启 execshield 保护
- 预防常见的‘syn flood 攻击’
- 开启源 IP 地址验证
- 防止破解者对服务器的IP地址进行欺骗攻击。
- 记录几种类型的可疑数据包,例如欺骗数据包、源路由数据包和重定向数据包。
教程详细信息 | |
---|---|
难度等级 | 中间的 |
Root 权限 | 是的 |
要求 | Linux 终端 |
类别 | 系统管理 |
操作系统兼容性 | Alma • Alpine • Amazon Linux • Arch • CentOS • Debian • Fedora • Linux • Mint • openSUSE • Pop!_OS • RHEL • Rocky • Stream • SUSE • Ubuntu |
预计阅读时间 | 3 分钟 |
Linux 内核 /etc/sysctl.conf 使用 sysctl 加强安全性
sysctl 命令用于在运行时修改内核参数。/etc/sysctl.conf 是一个文本文件,其中包含 sysctl 值,这些值将在启动时由 sysct 读取和设置。要查看当前值,请输入:
要加载设置,请输入:
# sysctl -a
# sysctl -A
# sysctl mib
# sysctl net.ipv4.conf.all.rp_filter
# sysctl -a --pattern 'net.ipv4.conf.(eth|wlan)0.arp'
# sysctl -p
警告!这些设置会修改重要的 Linux 内核配置。如果您缺乏 Linux 系统管理主题(如网络堆栈、TCP/IP 和命令行选项)方面的知识,某些配置可能会产生不良影响。仔细阅读所有说明和文档至关重要。示例或作者对任何错误配置概不负责。
用于 Linux 服务器强化的示例 /etc/sysctl.conf
编辑/etc/sysctl.conf或/etc/sysctl.d/99-custom.conf并按如下方式更新。该文件带有注释。不过,我建议阅读官方 Linux 内核 sysctl 调优帮助文件(更多信息请参见下文):
# 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 # 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 # Turn on SYN-flood protections net.ipv4.tcp_syncookies = 1 net.ipv4.tcp_synack_retries = 5 ########## IPv4 networking start ############## # Send redirects, if router, but this is just server # So no routing allowed 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 # Controls source route verification 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 #Set value to 1 or 2 (recommended) #kernel.exec-shield = 2 #kernel.randomize_va_space=2 # 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 # RFC 1337 fix net.ipv4.tcp_rfc1337=1
内核崩溃后立即重启机器
设置以下内核变量:
kernel.panic=10
mmap 基址、堆、栈和 VDSO 页面的地址都是随机的
kernel.randomize_va_space=2
忽略不良 ICMP 错误
net.ipv4.icmp_ignore_bogus_error_responses=1
在某些条件下防止创建或跟踪链接
fs.protected_hardlinks=1 fs.protected_symlinks=1
如何调整 Linux VM 子系统?
- 请参阅常见问题解答:Linux 调整 VM(内存)子系统
如何调整 Linux 网络堆栈?
其他 Linux 安全提示
总结
本页解释了使用 /etc/sysctl.conf 文件的 Linux 内核安全强化设置。
参考:
- Linux 内核 IP sysctl文档。