内核通过在/proc/sys/fs目录中文件对文件系统进行一些管理,管理员可以修改某些参数以达到优化系统的目的。对于这些文件可以通过三种方式进行修改。

临时修改

修改内核文件

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

命令行修改

sysctl -w fs.file-max=102400
安全修改

修改 /etc/sysctl.conf

echo 'fs.file-max = 6527711' >> /etc/sysctl.conf
sysctl -p

下面是/proc/sys/fs/下的通用的配置进行一些简单的介绍。

  • 1./proc/sys/fs/dentry-state
  • 2./proc/sys/fs/dir-notify-enable
  • 3./proc/sys/fs/file-max
  • 4./proc/sys/fs/file-nr
  • 5./proc/sys/fs/inode-nr
  • 6./proc/sys/fs/inode-state
  • 7./proc/sys/fs/lease-break-time
  • 8./proc/sys/fs/leases-enable
  • 9./proc/sys/fs/nr_open
  • 10./proc/sys/fs/overflowuid
  • 11./proc/sys/fs/overflowgid
  • 12./proc/sys/fs/suid_dumpable
  • 13./proc/sys/fs/inode-max

1.1. 1./proc/sys/fs/dentry-state


[root@localhost ~]# cat /proc/sys/fs/dentry-state 
1005427 1000315 45      0       0       0

1.1.1. 理解分析

dentry缓存的目的,为了减少对慢速磁盘的访问,每当VFS文件系统对底层的数据进行访问时,都会将访问的结果缓存下来,保存成一个dentry对象。

而且dentry对象的组织与管理,是和inode缓存极其相似的,也有一个hash表,和一个lru队列。

而且当内存压力较大时,也会调用prune_dcache来企图释放lru中优先级较低的dentry项目。

1.1.2. 翻译

这个文件包含目录缓存(dcache)状态信息。这个文件包含6个数字,nr_dentry,nr_unused,age_limit,wang_pages还有两个保留未使用的值。

  • nr_dentry 是已经分配的目录项。这个字段在Linux2.2时还未使用。

  • nr_unused 是未使用的目录项数量。

  • age_limit 当内存紧缺时,延迟多少秒后会回收目录项

  • wang_pages 当内核调用shrink_dcache_pages()函数并且dcache没有pruned时这个值是非0的。


/proc/sys/fs/dentry-state (since Linux 2.2)

This file contains information about the status of the directory cache (dcache). The file contains six numbers, nr_dentry, nr_unused, age_limit (age in seconds), want_pages (pages requested by system) and two dummy values.

  • nr_dentry is the number of allocated dentries (dcache entries). This field is unused in Linux 2.2.

  • nr_unused is the number of unused dentries.

  • age_limit is the age in seconds after which dcache entries can be reclaimed when memory is short.

  • want_pages is non-zero when the kernel has called shrink_dcache_pages() and the dcache isn't pruned yet.

1.2. 2./proc/sys/fs/dir-notify-enable

[root@localhost ~]$ cat /proc/sys/fs/dir-notify-enable 
1

1.2.1. 分析理解

设置是否启用dnotify,已被inotify取代,因为dnotify 需要您为每个打算监控是否发生改变的目录打开一个文件描述符。当同时监控多个目录时,这会消耗大量的资源,因为有可能达到每个进程的文件描述符限制。并且不允许卸载(unmount)支持的设备。

1.2.2. 翻译

这个文件被用来禁用或者启用在fcntl(2)的dnotify接口描述 ,他是系统级别的。0值表示禁用这个接口,1是表示开启。


/proc/sys/fs/dir-notify-enable This file can be used to disable or enable the dnotify interface described in fcntl(2) on a system-wide basis. A value of 0 in this file disables the interface, and a value of 1 enables it.

1.3. 3./proc/sys/fs/file-max

1.3.1. 理解分析

[root@localhost ~]#cat /proc/sys/fs/file-max 
6527711

Linux中/proc/sys/fs/file-max决定了整个系统能够打开的最大文件数量,是系统级别的限制。与此相关的概念还有/proc/sys/fs/file-nr显示当前系统打开的文件句柄状态、ulimit设置打开文件的句柄数、用户打开文件句柄数限制。

首先,建议file-max中的值设置为内存大小以K为单位时的10%。这只是一个建议值,root可以修改为其他需要的值。

[root@localhost ~]#free -k
             total       used       free     shared    buffers     cached
Mem:      62914560    7479548   55435012          0          0    7361384
-/+ buffers/cache:     118164   62796396
Swap:      2088956          0    2088956

[root@localhost ~]#cat /proc/sys/fs/file-max 
6527711

其次,要注意的是file-max值是整个系统全局的一个打开文件数量的限制,是一个硬上线,当文件打开的数量超过该值时就会报错。too many open files之类的messages报错可能会暂满磁盘。

于此相关的一个文件是/proc/sys/fs/file-nr,他是当前系统中已经打开的文件句柄的一个状态。他有三个值,分别是: 1.已经分配的文件句柄数 2.已经分配但没有使用的文件句柄数 3.最大文件句柄数 具体详细内容请阅读【/proc/sys/fs/file-nr】文章

[root@localhost ~]#cat /proc/sys/fs/file-nr 
1248    0       6527711

于此相关的还有ulimit中对打开文件的限制,ulimit是限制当前shell所有进程打开的文件数量。

但是一个用户可以打开多个shell,那么对某个用户打开文件的量的限制在/etc/security/limits.conf或/etc/security/limits.d/中配置。

1.3.2. 中文翻译

这个文件定义了系统系别的限制,即所有进程能够打开文件的总数量。(同时一些程序可以通过setrlimit调用,设置每个进程打开文件量的限制。)如果你有大量的文件句柄不足的错误告警,可以尝试修改增大这个值。

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

file-max的默认值是内核常量NR_OPEN,但这个上线可以被值修改。

如果你要增加/proc/sys/fs/file-max的值,请确定/proc/sys/fs/inode-max的值是/proc/sys/fs/file-max新值的3-4倍,否则可能会inodes不足。


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:

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

The kernel constant NR_OPEN imposes an upper limit on the value that may be placed in file-max.

If you increase /proc/sys/fs/file-max, be sure to increase /proc/sys/fs/inode-max to 3-4 times the new value of /proc/sys/fs/file-max, or you will run out of inodes.


1.3.3. 参考资料

http://zhangxugg-163-com.iteye.com/blog/1108402 http://www.opstool.com/article/166 http://wushank.blog.51cto.com/3489095/1617874

1.4. 4./proc/sys/fs/file-nr

1.4.1. 理解分析

[root@localhost ~]# cat /proc/sys/fs/file-nr
7456 0 6583463

这个文件是一个只读文件,不能进行修改,应为它是当前系统文件句柄使用的一个概况。这里的最大值是/proc/sys/fs/file-max中设置的,具体设置值可以查看【/proc/sys/fs/file-max】文章。

但在kernel 2.6版本中第二项的值总为0,这并不是一个错误,它实际上意味着已经分配的文件句柄无一浪费的都已经被使用了。如果有值说明以前遇到过使用句柄的峰值了,现在用不了那么多的句柄就会有空闲句柄,句柄分配后系统不会回收。

1.4.2. 配置设置

只读文件,不可修改

1.4.3. 中文翻译

这个文件(只读)获取当前打开的文件数量。它包含三个数字: 1.已经分配的文件句柄数, 2.已经分配但没有使用的文件句柄数, 3.最大文件句柄数。 内核动态分配文件句柄,但是不会再次释放他们。如果已经分配的文件值接近最大值时,你应该考虑增大这个最大值。如果空闲文件句柄很大,那么你可能遇到了一个文件句柄使用峰值,你不需要增大这个最大值。


This (read-only) file gives the number of files presently opened. It contains three numbers: the number of allocated file handles; the number of free file handles; and the maximum number of file handles. The kernel allocates file handles dynamically, but it doesn't free them again. If the number of allocated files is close to the maximum, you should consider increasing the maximum. When the number of free file handles is large, you've encountered a peak in your usage of file handles and you probably don't need to increase the maximum.

1.4.4. 参考资料

http://qingwang.blog.51cto.com/505009/579064

1.5. 5./proc/sys/fs/inode-nr

1.5.1. 分析

[root@localhost ~]#cat /proc/sys/fs/inode-state 
79484   329     0       0       0       0       0

[root@localhost ~]#cat /proc/sys/fs/inode-nr 
79480   329

这个文件是inode-state的前两个值,具体内容查看inode-state

1.5.2. 翻译

/proc/sys/fs/inode-nr 这个文件只包含inode-state的前两个值


/proc/sys/fs/inode-nr This file contains the first two values from inode-state.

1.6. 6./proc/sys/fs/inode-state

1.6.1. 理解分析

[root@localhost ~]#cat /proc/sys/fs/inode-state 
79482   329     0       0       0       0       0

inode-state是当前内存管理的inode使用的一个状态。后4个值是虚拟值不知道有啥作用,前三个值分别是: nr_inodes : 系统已经分配的inode句柄

nr_free_inodes : 系统剩余的inode句柄。以前使用inode句柄有过一个峰值,系统分配过大量的inode句柄,但是分配过的inode句柄是不会回收释放的。后来峰值过去了,一部分重新分配出去了,一部分就变成剩余的了。

preshrink : 如果当前分配的inode句柄超过inode-max值时,超过的数量这里会显示。但是一般inode-max文件不存在也就是没有限制,这个值一般就是0。

这里需要注意一个问题是:标准输入,标准输出、网络连接socket都会占用inode句柄。

1.6.2. 中文翻译

这个文件包含了7个数值:nr_inodes, nr_free_inodes, preshrink, 和4个虚拟值。nr_inodes 是系统已经分配的indoes数量。

能够轻微的超过inode-max中定义的最大值,因为Linux每次会分配inode时会分配一整页。nr_fre_inodes表示剩余的inode数量。

当nr_inodes大于inodex-max时,preshrink是一个非零的值,同时系统需要回收inode列表而不是继续分配inode。


This file contains seven numbers: nr_inodes, nr_free_inodes, preshrink, and four dummy values. nr_inodes is the number of inodes the system has allocated.

This can be slightly more than inode-max because Linux allocates them one page full at a time. nr_free_inodes represents the number of free inodes.

preshrink is non-zero when the nr_inodes > inode-max and the system needs to prune the inode list instead of allocating more.

1.7. 7./proc/sys/fs/lease-break-time

[root@localhost ~]# cat /proc/sys/fs/lease-break-time 
45

1.7.1. 分析理解

linux有一种lease锁,一个进程试图打开一个lease保护的文件时,该进程被阻塞,锁的持有者会接收到信号。收到信号,锁的持有者进程应该更新文件保持文件的一致性,然后释放锁,如果进程不释放,在一个给定时间后(45秒),内核自动删除该锁,让阻塞的进程继续执行.

这个文件就是保存租借锁的超时时间(以秒为单位)

1.7.2. 翻译

这个文件指定文件租约锁的超时时间,内核会分配一个进程处理一个文件租约锁,当其他进程需要打开这个文件时,它会发送一个信号到正在租用这个文件的进程。

如果这个占用该文件的进程在一定时间内没有删除或downgrade这个租约锁,那么操作系统会在超时后强制释放这个锁。


/proc/sys/fs/lease-break-time This file specifies the grace period that the kernel grants to a process holding a file lease (fcntl(2)) after it has sent a signal to that process notify-ing it that another process is waiting to open the file. If the lease holder does not remove or downgrade the lease within this grace period, the kernel forcibly breaks the lease.

1.8. 8./proc/sys/fs/leases-enable

[root@localhost ~]# cat /proc/sys/fs/leases-enable    
1

1.8.1. 翻译

这个文件可以在整个系统级别开启或关闭文件锁。如果这个文件包含0,锁就是禁用的。一个非0值就是开启锁。


/proc/sys/fs/leases-enable This file can be used to enable or disable file leases (fcntl(2)) on a system-wide basis. If this file contains the value 0, leases are disabled. A non- zero value enables leases.

1.9. 9./proc/sys/fs/nr_open

[root@localhost ~]# cat /proc/sys/fs/nr_open 
1048576

1.9.1. 理解分析

这是对整个系统中所有单个进程能够使用的文件句柄的限制,默认值是1024*1024。

int sysctl_nr_open __read_mostly = 1024*1024;

但实际依赖的是内核遍历RLIMIT_NOFILE。

# define RLIMIT_NOFILE        7    /* max number of open files */

1.9.2. 翻译

nr_open: 这个值表示一个进程最大能够分配使用的文件句柄数量。默认是1024*1024(1048576) ,这个值能够满足大多数的机器。实际限制依赖RLIMIT_NOFILE内核变量。


nr_open: This denotes the maximum number of file-handles a process can allocate. Default value is 1024*1024 (1048576) which should be enough for most machines. Actual limit depends on RLIMIT_NOFILE resource limit.

http://www.aikaiyuan.com/10408.html

1.10. 10./proc/sys/fs/overflowuid

1.11. 11./proc/sys/fs/overflowgid

[root@localhost ~]# cat /proc/sys/fs/overflowuid 
65534

[root@localhost ~]# cat /proc/sys/fs/overflowgid 
65534

1.11.1. 理解分析

Linux系统的用户ID是32位的,但是有些文件系统支持的UID/GID是16位的。因此如果在Linux系统上,用户的ID超过了65535的话,该用户创建的文件不能保存到文件系统上。为了兼容这种16位uid的文件系统,Linux会将UID操作65535的用户的UID/GID转换成一个固定的值,这样就可以正常的报存文件了。默认这个值是16位的UID/GID 65534。

1.11.2. 翻译

这个值允许你去修改固定的UID和GID。默认值时65534.一些文件系统只支持16位的UIDs和GIDs,尽管Linux的UIDs和GIDs是32位的。当一个文件系统以可写的方式挂载使用时,任何UID和GID超过65535时会自动被转换为一个固定值,这个固定值保存在这个文件中。


These files allow you to change the value of the fixed UID and GID. The default is 65534. Some file systems only support 16-bit UIDs and GIDs, although in Linux UIDs and GIDs are 32 bits. When one of these file systems is mounted with writes enabled, any UID or GID that would exceed 65535 is translated to the overflow value before being written to disk.

http://www.cnblogs.com/tolimit/p/5065761.html

1.12. 12./proc/sys/fs/suid_dumpable

[root@localhost ~]# cat /proc/sys/fs/suid_dumpable 
0

1.12.1. 理解分析

若程序调用了seteuid()/setegid()改变了进程的有效用户或组,则在默认情况下系统不会为这些进程生成Coredump。很多服务程序都会调用seteuid(),如MySQL,不论你用什么用户运行 mysqld_safe启动MySQL,mysqld进行的有效用户始终是msyql用户。如果你当初是以用户A运行了某个程序,但在ps里看到的 这个程序的用户却是B的话,那么这些进程就是调用了seteuid了。为了能够让这些进程生成core dump,需要将/proc/sys/fs/suid_dumpable 文件的内容改为1(一般默认是0)。

1.12.2. 翻译

这个文件中的值决定是否对生成的core dump文件设置set-user-ID或者protected/tainted binaries。三个不同的整数可以被指定:

0(默认)提供传统的行为规则(在Linux2.6.13以前)。一个进程修改身份(通过调用setuid(2),setgid(2),或者类似的调用,或者执行set-user-ID或set-group-ID程序)将不会产生core dump文件或者他的二进制文件没有可读权限。

1(调试模式)dump出所有进程的core信息。core dump文件所属用户是文件系统的用户且没有设置粘贴位。这仅是为了调试,并没有检查ptrace。

2(安全模式)正常的输出不会dump出来(查看上面的0设置),且dump的文件只有root是可读的。允许其他用户删除core dump文件,但是不能阅读。处于安全考虑,这种模式下不允许core dump文件被其他进程或文件覆盖。这个模式适合管理员尝试在普通环境下调试一些问题


/proc/sys/fs/suid_dumpable (since Linux 2.6.13) The value in this file determines whether core dump files are produced for set-user-ID or otherwise protected/tainted binaries. Three different integervalues can be specified:

0 (default) This provides the traditional (pre-Linux 2.6.13) behavior. A core dump will not be produced for a process which has changed credentials (by calling seteuid(2), setgid(2), or similar, or by executing a set-user-ID or set-group-ID program) or whose binary does not have read permission enabled.

1 ("debug") All processes dump core when possible. The core dump is owned by the file system user ID of the dumping process and no security is applied. This is intended for system debugging situations only. Ptrace is unchecked.

2 ("suidsafe") Any binary which normally would not be dumped (see "0" above) is dumped readable by root only. This allows the user to remove the core dump file but not to read it. For security reasons core dumps in this mode will not overwrite one another or other files. This mode is appropriate when administrators are attempting to debug problems in a normal environment.

1.13. 13./proc/sys/fs/inode-max

1.13.1. 分析

这个文件可能不存在,他表示的是内存管理的inode句柄数量的最大值。如果不存在也就意味着没有限制,如果存在的话,这个值建议是/proc/sys/fs/file-max的3-4倍,否则可能会导致inode不足。

1.13.2. 翻译

这个文件是内存管理的最大inode句柄数量。在一些系统中,这个文件可能不存在。这个值应该是file-max值的3-4倍。来自标准输入,标准输出和网络sockets连接也需要一个inode来处理他们。如果你经常的出现inodes不足,你应该增大这个值。


This file contains the maximum number of in-memory inodes. On some (2.4) systems, it may not be present. This value should be 3-4 times larger than the value in file-max, since stdin, stdout and network sockets also need an inode to handle them. When you regularly run out of inodes, you need to increase this value.

Copyright © 温玉 2021 | 浙ICP备2020032454号 all right reserved,powered by Gitbook该文件修订时间: 2023-06-19 08:59:50

results matching ""

    No results matching ""