4

sysctl.conf 和 limits.conf 学习和调优

 2 years ago
source link: https://wsgzao.github.io/post/sysctl/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
sysctl.conf 和 limits.conf 学习和调优

记得第一次接触 /etc/security/limits.conf/etc/sysctl.conf 时是因为部署 Oracle 时要按需修改内核参数。limits.conf 文件实际是 Linux PAM(插入式认证模块,Pluggable Authentication Modules)中 pam_limits.so 的配置文件,突破系统的默认限制,对系统访问资源有一定保护作用。 limits.conf 和 sysctl.conf 区别在于 limits.conf 是针对用户,而 sysctl.conf 是针对整个系统参数配置。

sysctl.conf 和 limits.conf 学习和调优


2019 年 05 月 13 日 - 更新 limits.conf 的最佳实践
2015 年 08 月 10 日 - 初稿

阅读原文 - https://wsgzao.github.io/post/sysctl/

设置 Sysctl.conf 用以提高 Linux 的性能(最完整的 sysctl.conf 优化方案) - http://blog.csdn.net/21aspnet/article/details/6584792
limits.conf 工作原理 - http://my.oschina.net/987openlab/blog/94634
ulimit 命令 - http://man.linuxde.net/ulimit
Sysctl 学习 - http://pengyao.org/sysctl-1.html
Kernel sysctl configuration file for Linux - https://klaver.it/linux/sysctl.conf
LTMP 索引 - https://wsgzao.github.io/index/#LTMP


limits.conf 工作原理

limits.conf 是 pam_limits.so 的配置文件,然后 /etc/pam.d/ 下的应用程序调用 pam_***.so 模块。譬如说,当用户访问服务器,服务程序将请求发送到 PAM 模块,PAM 模块根据服务名称在 /etc/pam.d 目录下选择一个对应的服务文件,然后根据服务文件的内容选择具体的 PAM 模块进行处理。

limits.conf 文件格式

username|@groupname   type  resource  limit 

1)username|@groupname
设置需要被限制的用户名,组名前面加 @和用户名区别。也可用通配符 * 来做所有用户的限制

2)type
类型有 soft,hard 和 -,其中 soft 指的是当前系统生效的设置值。hard 表明系统中所能设定的最大值。soft 的限制不能比 hard 限制高。用 - 就表明同时设置了 soft 和 hard 的值

3)resource: 表示要限制的资源

  1. nofile - 打开文件的最大数目
  2. noproc - 进程的最大数目

ulimit 命令

ulimit 命令用来限制系统用户对 shell 资源的访问,常用参数解释如下

ulimit(选项)

-a:显示目前资源限制的设定;
-c <core 文件上限 & gt;:设定 core 文件的最大值,单位为区块;
-d < 数据节区大小 & gt;:程序数据节区的最大值,单位为 KB;
-f < 文件大小 & gt;:shell 所能建立的最大文件,单位为区块;
-H:设定资源的硬性限制,也就是管理员所设下的限制;
-m < 内存大小 & gt;:指定可使用内存的上限,单位为 KB;
-n < 文件数目 & gt;:指定同一时间最多可开启的文件数;
-p < 缓冲区大小 & gt;:指定管道缓冲区的大小,单位 512 字节;
-s < 堆叠大小 & gt;:指定堆叠的上限,单位为 KB;
-S:设定资源的弹性限制;
-t <CPU 时间 & gt;:指定 CPU 使用时间的上限,单位为秒;
-u < 程序数目 & gt;:用户最多可开启的程序数目;
-v < 虚拟内存大小 & gt;:指定可使用的虚拟内存上限,单位为 KB。

sysctl.conf 工作原理

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

limits.conf 设置

1)暂时生效,适用于通过 ulimit 命令登录 shell 会话期间

ulimit -SHn 65535

2)永久生效,通过将一个相应的 ulimit 语句添加到由登录 shell 读取的文件之一(例如 ~/.profile),即特定于 shell 的用户资源文件;或者通过编辑 /etc/security/limits.conf

# 比如添加到 / etc/profile
echo ulimit -SHn 65535 >> /etc/profile
source /etc/profile

# 修改最大进程和最大文件打开数限制
vi /etc/security/limits.conf
* soft nofile 655350
* hard nofile 655350
* soft nproc unlimited
* hard nproc unlimited
* soft core unlimited
* hard core unlimited

# 生产环境中的配置,同时兼容 RedHat/CentOS/Ubuntu

# 修改 / etc/security/limits.conf
cat /etc/security/limits.conf
* soft nofile 655350
* hard nofile 655350
* soft nproc unlimited
* hard nproc unlimited
* soft core unlimited
* hard core unlimited
root soft nofile 655350
root hard nofile 655350
root soft nproc unlimited
root hard nproc unlimited
root soft core unlimited
root hard core unlimited

# 创建软链接
[root@localhost wangao]# cd /etc/security/limits.d/
[root@localhost limits.d]# ll
total 4
-rw-r--r--. 1 root root 191 Apr 11 2018 20-nproc.conf
lrwxrwxrwx 1 root root 25 May 10 19:57 99-garena-server.conf -> /etc/security/limits.conf

# 修改 system.conf 中 DefaultLimitNOFILE=655350
cat /etc/systemd/system.conf
# This file is part of systemd.
#
# systemd is free software; you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# Entries in this file show the compile time defaults.
# You can change settings by editing this file.
# Defaults can be restored by simply deleting this file.
#
# See systemd-system.conf(5) for details.

[Manager]
#LogLevel=info
#LogTarget=journal-or-kmsg
#LogColor=yes
#LogLocation=no
#DumpCore=yes
#CrashShell=no
#ShowStatus=yes
#CrashChVT=1
#CtrlAltDelBurstAction=reboot-force
#CPUAffinity=1 2
#JoinControllers=cpu,cpuacct net_cls,net_prio
#RuntimeWatchdogSec=0
#ShutdownWatchdogSec=10min
#CapabilityBoundingSet=
#SystemCallArchitectures=
#TimerSlackNSec=
#DefaultTimerAccuracySec=1min
#DefaultStandardOutput=journal
#DefaultStandardError=inherit
#DefaultTimeoutStartSec=90s
#DefaultTimeoutStopSec=90s
#DefaultRestartSec=100ms
#DefaultStartLimitInterval=10s
#DefaultStartLimitBurst=5
#DefaultEnvironment=
#DefaultCPUAccounting=no
#DefaultBlockIOAccounting=no
#DefaultMemoryAccounting=no
#DefaultTasksAccounting=no
#DefaultTasksMax=
#DefaultLimitCPU=
#DefaultLimitFSIZE=
#DefaultLimitDATA=
#DefaultLimitSTACK=
#DefaultLimitCORE=
#DefaultLimitRSS=
#DefaultLimitNOFILE=
#DefaultLimitAS=
#DefaultLimitNPROC=
#DefaultLimitMEMLOCK=
#DefaultLimitLOCKS=
#DefaultLimitSIGPENDING=
#DefaultLimitMSGQUEUE=
#DefaultLimitNICE=
#DefaultLimitRTPRIO=
#DefaultLimitRTTIME=
DefaultLimitNOFILE=655350

sysctl.conf 设置

这是一个在网络上流传已久的 sysctl.conf 优化配置

# 优化 TCP
vi /etc/sysctl.conf
# 禁用包过滤功能
net.ipv4.ip_forward = 0
# 启用源路由核查功能
net.ipv4.conf.default.rp_filter = 1
# 禁用所有 IP 源路由
net.ipv4.conf.default.accept_source_route = 0
# 使用 sysrq 组合键是了解系统目前运行情况,为安全起见设为 0 关闭
kernel.sysrq = 0
# 控制 core 文件的文件名是否添加 pid 作为扩展
kernel.core_uses_pid = 1
# 开启 SYN Cookies,当出现 SYN 等待队列溢出时,启用 cookies 来处理
net.ipv4.tcp_syncookies = 1
# 每个消息队列的大小(单位:字节)限制
kernel.msgmnb = 65536
# 整个系统最大消息队列数量限制
kernel.msgmax = 65536
# 单个共享内存段的大小(单位:字节)限制,计算公式 64G*1024*1024*1024(字节)
kernel.shmmax = 68719476736
# 所有内存大小(单位:页,1 页 = 4Kb),计算公式 16G*1024*1024*1024/4KB(页)
kernel.shmall = 4294967296
#timewait 的数量,默认是 180000
net.ipv4.tcp_max_tw_buckets = 6000
# 开启有选择的应答
net.ipv4.tcp_sack = 1
# 支持更大的 TCP 窗口. 如果 TCP 窗口最大超过 65535(64K), 必须设置该数值为 1
net.ipv4.tcp_window_scaling = 1
#TCP 读 buffer
net.ipv4.tcp_rmem = 4096 131072 1048576
#TCP 写 buffer
net.ipv4.tcp_wmem = 4096 131072 1048576
# 为 TCP socket 预留用于发送缓冲的内存默认值(单位:字节)
net.core.wmem_default = 8388608
# 为 TCP socket 预留用于发送缓冲的内存最大值(单位:字节)
net.core.wmem_max = 16777216
# 为 TCP socket 预留用于接收缓冲的内存默认值(单位:字节)
net.core.rmem_default = 8388608
# 为 TCP socket 预留用于接收缓冲的内存最大值(单位:字节)
net.core.rmem_max = 16777216
# 每个网络接口接收数据包的速率比内核处理这些包的速率快时,允许送到队列的数据包的最大数目
net.core.netdev_max_backlog = 262144
#web 应用中 listen 函数的 backlog 默认会给我们内核参数的 net.core.somaxconn 限制到 128,而 nginx 定义的 NGX_LISTEN_BACKLOG 默认为 511,所以有必要调整这个值
net.core.somaxconn = 262144
# 系统中最多有多少个 TCP 套接字不被关联到任何一个用户文件句柄上。这个限制仅仅是为了防止简单的 DoS 攻击,不能过分依靠它或者人为地减小这个值,更应该增加这个值(如果增加了内存之后)
net.ipv4.tcp_max_orphans = 3276800
# 记录的那些尚未收到客户端确认信息的连接请求的最大值。对于有 128M 内存的系统而言,缺省值是 1024,小内存的系统则是 128
net.ipv4.tcp_max_syn_backlog = 262144
# 时间戳可以避免序列号的卷绕。一个 1Gbps 的链路肯定会遇到以前用过的序列号。时间戳能够让内核接受这种“异常” 的数据包。这里需要将其关掉
net.ipv4.tcp_timestamps = 0
# 为了打开对端的连接,内核需要发送一个 SYN 并附带一个回应前面一个 SYN 的 ACK。也就是所谓三次握手中的第二次握手。这个设置决定了内核放弃连接之前发送 SYN+ACK 包的数量
net.ipv4.tcp_synack_retries = 1
# 在内核放弃建立连接之前发送 SYN 包的数量
net.ipv4.tcp_syn_retries = 1
# 开启 TCP 连接中 time_wait sockets 的快速回收
net.ipv4.tcp_tw_recycle = 1
# 开启 TCP 连接复用功能,允许将 time_wait sockets 重新用于新的 TCP 连接(主要针对 time_wait 连接)
net.ipv4.tcp_tw_reuse = 1
#1st 低于此值, TCP 没有内存压力, 2nd 进入内存压力阶段, 3rdTCP 拒绝分配 socket(单位:内存页)
net.ipv4.tcp_mem = 94500000 915000000 927000000
# 如果套接字由本端要求关闭,这个参数决定了它保持在 FIN-WAIT-2 状态的时间。对端可以出错并永远不关闭连接,甚至意外当机。缺省值是 60 秒。2.2 内核的通常值是 180 秒,你可以按这个设置,但要记住的是,即使你的机器是一个轻载的 WEB 服务器,也有因为大量的死套接字而内存溢出的风险,FIN- WAIT-2 的危险性比 FIN-WAIT-1 要小,因为它最多只能吃掉 1.5K 内存,但是它们的生存期长些。
net.ipv4.tcp_fin_timeout = 15
# 表示当 keepalive 起用的时候,TCP 发送 keepalive 消息的频度(单位:秒)
net.ipv4.tcp_keepalive_time = 30
# 对外连接端口范围
net.ipv4.ip_local_port_range = 2048 65000
# 表示文件句柄的最大数量
fs.file-max = 102400

这是我在实际生产系统自动化部署中用的配置,不做过渡优化

# sysctl settings are defined through files in
# /usr/lib/sysctl.d/, /run/sysctl.d/, and /etc/sysctl.d/.
#
# Vendors settings live in /usr/lib/sysctl.d/.
# To override a whole file, create a new file with the same in
# /etc/sysctl.d/ and put new settings there. To override
# only specific settings, add a file with a lexically later
# name in /etc/sysctl.d/ and put new settings there.
#
# For more information, see sysctl.conf(5) and sysctl.d(5).
vm.dirty_writeback_centisecs=100
vm.dirty_expire_centisecs=100
vm.swappiness=1
net.ipv4.ip_local_port_range=10000 65001
net.ipv4.tcp_max_orphans=4000000
net.ipv4.tcp_timestamps=0
net.core.somaxconn=1024
net.netfilter.nf_conntrack_max=121005752
kernel.core_pattern=/var/coredump/core.%e.%p.%t

最后记得刷新立即生效

/sbin/sysctl -p

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK