Sysctl.conf and limits.conf (转)

sysctl.conf学习和调优

·        limits.conf

 

·        sysctl.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是针对整个系统参数配置。

调整limits.confsysctl.conf参数是有必要的


更新历史

20150810 - 初稿

阅读原文http://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
索引http://wsgzao.github.io/index/#LTMP


原理

limits.conf工作原理

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

limits.conf文件格式

 username|@groupname   type  resource  limit

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

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

3resource表示要限制的资源

1.   nofile - 打开文件的最大数目

2.   noproc - 进程的最大数目

ulimit命令

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

ulimit(选项)

 

-a:显示目前资源限制的设定;

-c <core文件上限>:设定core文件的最大值,单位为区块;

-d <数据节区大小>:程序数据节区的最大值,单位为KB

-f <文件大小>shell所能建立的最大文件,单位为区块;

-H:设定资源的硬性限制,也就是管理员所设下的限制;

-m <内存大小>:指定可使用内存的上限,单位为KB

-n <文件数目>:指定同一时间最多可开启的文件数;

-p <缓冲区大小>:指定管道缓冲区的大小,单位512字节;

-s <堆叠大小>:指定堆叠的上限,单位为KB

-S:设定资源的弹性限制;

-t <CPU时间>:指定CPU使用时间的上限,单位为秒;

-u <程序数目>:用户最多可开启的程序数目;

-v <虚拟内存大小>:指定可使用的虚拟内存上限,单位为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

echoulimit -SHn 65535 >> /etc/profile

source /etc/profile

 

#修改最大进程和最大文件打开数限制

vi/etc/security/limits.conf

* soft nproc 11000

* hard nproc 11000

* soft nofile 655350

* hard nofile 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 

#TCPbuffer

net.ipv4.tcp_rmem= 40961310721048576

#TCPbuffer

net.ipv4.tcp_wmem= 40961310721048576  

#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并附带一个回应前面一个SYNACK。也就是所谓三次握手中的第二次握手。这个设置决定了内核放弃连接之前发送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= 94500000915000000927000000  

#如果套接字由本端要求关闭,这个参数决定了它保持在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 = 204865000

#表示文件句柄的最大数量

fs.file-max = 102400

 

这是我在实际生产系统自动化部署中用的配置

# Kernel sysctlconfiguration file for Red Hat Linux

#

# For binaryvalues, 0 is disabled, 1 is enabled.  Seesysctl(8) and

# sysctl.conf(5)for more details.

 

# Controls IPpacket forwarding

net.ipv4.ip_forward= 0

 

# Controlssource route verification

net.ipv4.conf.default.rp_filter= 1

 

# Do not acceptsource routing

net.ipv4.conf.default.accept_source_route = 0

 

# Controls theSystem Request debugging functionality of the kernel

 

# Controlswhether core dumps will append the PID to the core filename.

# Useful fordebugging multi-threaded applications.

kernel.core_uses_pid= 1

 

# Controls theuse of TCP syncookies

net.ipv4.tcp_syncookies= 1

 

# Disablenetfilter on bridges.

net.bridge.bridge-nf-call-ip6tables= 0

net.bridge.bridge-nf-call-iptables= 0

net.bridge.bridge-nf-call-arptables= 0

 

# Controls thedefault maxmimum size of a mesage queue

kernel.msgmnb = 65536

 

# Controls themaximum size of a message, in bytes

kernel.msgmax = 65536

 

# Controls themaximum shared segment size, in bytes

kernel.shmmax = 68719476736

 

# Controls themaximum number of shared memory segments, in pages

kernel.shmall = 4294967296

net.ipv4.conf.all.send_redirects= 0

net.ipv4.conf.default.send_redirects= 0

net.ipv4.conf.all.secure_redirects= 0

net.ipv4.conf.default.secure_redirects= 0

net.ipv4.conf.all.accept_redirects= 0

net.ipv4.conf.default.accept_redirects= 0

net.ipv4.conf.all.send_redirects= 0

net.ipv4.conf.default.send_redirects= 0

net.ipv4.conf.all.secure_redirects= 0

net.ipv4.conf.default.secure_redirects= 0

net.ipv4.conf.all.accept_redirects= 0

net.ipv4.conf.default.accept_redirects= 0

net.netfilter.nf_conntrack_max= 1000000

kernel.unknown_nmi_panic= 0

kernel.sysrq = 0

fs.file-max = 1000000

vm.swappiness = 10

fs.inotify.max_user_watches= 10000000

net.core.wmem_max= 327679

net.core.rmem_max= 327679

net.ipv4.conf.all.send_redirects= 0

net.ipv4.conf.default.send_redirects= 0

net.ipv4.conf.all.secure_redirects= 0

net.ipv4.conf.default.secure_redirects= 0

net.ipv4.conf.all.accept_redirects= 0

net.ipv4.conf.default.accept_redirects= 0

 

最后记得刷新立即生效,关于LTMP的搭建可以参考http://wsgzao.github.io/post/ltmp/

 /sbin/sysctl -p

2      file-maxulimit的关系与差别

2013-09-05 19:06 412人阅读 评论(0) 收藏 举报

 分类:

 

6) 

典型的,提供大量静态文件访问的web服务器,缓存服务器(如squid), 均要注意这个问题

 

网上的教程,大约只是简单说明了如何设置ulimit和file-max, 但并没有说清楚这两者之间的差别,让人一头雾水

 

1. file-max的含义

man proc,可得到file-max的描述:

/proc/sys/fs/file-max
              This  file defines a system-wide limit on the number of open files for all processes.  (See
              also setrlimit(2),  which  can  be  used  by  a  process  to  set  the  per-process  limit,
              RLIMIT_NOFILE,  on  the  number  of  files it may open.)  If you get lots of error messages
              about running out of file handles, try increasing this value:

即file-max是设置系统所有进程一共可以打开的文件数量。同时一些程序可以通过setrlimit调用,设置每个进程的限制。如果得到大量使用完文件句柄的错误信息,是应该增加这个值。

 

也就是说,这项参数是系统级别的。

 

echo  6553560 > /proc/sys/fs/file-max

或修改 /etc/sysctl.conf, 加入

fs.file-max = 6553560 重启生效

 

2. ulimit的

Provides control over the resources available to the shell and to processes started by it, on systems that allow  such control.

 

即设置当前shell以及由它启动的进程的资源限制。

 

显然,对服务器来说,file-max, ulimit都需要设置,否则就可能出现文件描述符用尽的问题,为了让机器在重启之后仍然有效,强烈建立作以下配置,以确保file-max, ulimit的值正确无误:

 

1. 修改/etc/sysctl.conf, 加入

fs.file-max = 6553560

 

2.系统默认的ulimit对文件打开数量的限制是1024,修改/etc/security/limits.conf并加入以下配置,永久生效

* soft nofile 65535 
* hard nofile 65535

 

修改完之后,重启即可生效

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值