当修改一个Linux系统参数或限制,比如文件打开数时,之前用到过的方式有ulimit、limits.conf、sysctl和/proc文件系统。
对这几个东西一直比较困惑,翻了几篇文档,简单写一下它们之间的区别。
ulimit是linux shell的内键命令,它具有一套参数集,用于对 shell进程 及其 子进程 进行 资源限制。
例如用户同时运行了两个shell终端进程,只在其中一个环境中执行了ulimit – s 100,则该shell进程里创建文件的大小会有相应的限制,而另一个shell终端包括其上运行的子程序都不会受其影响。
ulimit的设定值是 per-process 的,也就是说,每个进程有自己的limits值。
使用ulimit进行修改,是 立即生效 的。
ulimit只影响shell进程及其子进程,用户登出后失效。
可以在profile中加入ulimit的设置,变相的做到永久生效。
使用ulimit -a可以查看所有的设定值:
pi@raspberrypi:~ $ ulimit -a core file size (blocks, -c) 0 data seg size (kbytes, -d) unlimited scheduling priority (-e) 0 file size (blocks, -f) unlimited pending signals (-i) 7336 max locked memory (kbytes, -l) unlimited max memory size (kbytes, -m) unlimited open files (-n) 65536 pipe size (512 bytes, -p) 8 POSIX message queues (bytes, -q) 819200 real-time priority (-r) 95 stack size (kbytes, -s) 8192 cpu time (seconds, -t) unlimited max user processes (-u) 7336 virtual memory (kbytes, -v) unlimited file locks (-x) unlimited
注意:
可以在以下位置进行ulimit设置:
/etc/profile 所有用户有效,永久生效~/.bash_profile 当前用户有效,永久生效limits.conf文件实际是Linux PAM中 pam_limits.so 的配置文件,pam_limits模块对 用户的会话 进行 资源限制。
一个shell的初始limits就是由pam_limits设定的,用户登录后,pam_limits会给用户的shell设定在limits.conf定义的值。
pam_limits的设定值也是 per-process 的。
pam_limits的设置是 永久生效 的。
/etc/security/limits.conf
示例:
* soft nofile 655360 * hard nofile 655360 * soft noproc 655360 * hard noproc 655355 * soft core 0 * hard core 0
4个字段的含义分别为: domain type item value
sysctl是一个允许改变正在运行中的Linux系统的接口,修改的是针对 整个系统 的 内核参数。
sysctl的修改是 立即 且 临时 的(重启后失效)。
可以通过修改sysctl.conf配置文件,达到 永久 生效。
sysctl fs.file-max fs.file-max = 93796
sysctl -w fs.file-max=100000 # 设置文件打开数
sysctl -w net.ipv4.ip_forward=1 # 开启IP转发
/etc/sysctl.conf
修改/etc/sysctl.conf可以做到永久生效:
vi /etc/sysctl.conf fs.file-max = 100000
最后使用-p命令从配置文件中加载内核参数,使其立即生效:
sysctl -p
下面是一个摘抄的配置示例,出处:http://www.jianshu.com/p/9a8e383b5b49
# Kernel sysctl configuration file for Red Hat Linux # # For binary values, 0 is disabled, 1 is enabled. See sysctl(8) and # sysctl.conf(5) for more details. # 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 # 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 # Disable netfilter on bridges. net.bridge.bridge-nf-call-ip6tables = 0 net.bridge.bridge-nf-call-iptables = 0 net.bridge.bridge-nf-call-arptables = 0 # Controls the default maxmimum size of a mesage queue kernel.msgmnb = 65536 # Controls the maximum size of a message, in bytes kernel.msgmax = 65536 # Controls the maximum shared segment size, in bytes kernel.shmmax = 68719476736 # Controls the maximum 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 /proc文件系统
Linux内核提供了一种通过/proc文件系统,在运行时访问内核内部数据结构、改变内核设置的机制。
proc文件系统是一个伪文件系统,它只存在内存当中,而不占用外存空间。它以文件系统的方式为访问系统内核数据的操作提供接口。
最初开发/proc文件系统是为了提供有关系统中进程的信息。但是由于这个文件系统非常有用,因此内核中的很多元素也开始使用它来报告信息,或启用动态运行时配置。
对/proc中内核文件的修改,针对的是 整个系统 的 内核参数 ,修改后 立即生效 ,但修改是 临时 的(重启后失效)。
/proc/sys下内核文件与配置文件sysctl.conf中变量的对应关系:
例如:
下面几个是经常会用到的文件:
proc 中的每个文件都有一组分配给它的非常特殊的文件许可权,并且每个文件属于特定的用户标识。
以开启IP转发为例:
当然,也可以使用sysctl来配置这些内核条目
以下是/proc目录中进程N的信息:
[转帖]ulimit、limits.conf、sysctl和proc文件系统
原文:https://www.cnblogs.com/jinanxiaolaohu/p/10643016.html