linux内核参数小解

由于Linux的内核参数信息都存在内存中,因此可以通过命令直接修改,并且修改后直接生效,这个只是引进变量的一种方法。
但是,当系统重新启动后,原来设置的参数值就会丢失,而系统每次启动时都会自动去/etc/sysctl.conf文件中读取内核参数,因此将内核的参数配置写入这个文件中,永久性的一个方法。

配置方法

首先打开/etc/sysctl.conf文件,查看如下两行的设置值,这里是:

1
2
3
4
5
6
# 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

如果系统默认的配置比这里给出的值大,就不要修改原有配置。同时在/etc/sysctl.conf文件最后,添加以下内容:

1
2
3
4
5
6
7
8
fs.file-max = 6553600
kernel.shmmni = 4096
kernel.sem = 250 32000 100 128
net.ipv4.ip_local_port_range = 1024 65000
net.core.rmem_default = 4194304
net.core.rmem_max = 4194304
net.core.wmem_default = 262144
net.core.wmem_max = 262144

这里的“fs.file-max = 6553600”其实是由“fs.file-max = 512 PROCESSES”得到的,我们指定PROCESSES的值为12800,即为“fs.file-max =512 12800”。 sysctl.conf文件修改完毕后,接着执行“sysctl -p”使设置生效。

root@localhost ~]# sysctl -p

常用的内核参数的含义如下。

kernel.shmmax:表示单个共享内存段的最大值,以字节为单位,此值一般为物理内存的一半,不过大一点也没关系,这里设定的为4GB,即
“4294967295/1024/1024/1024=4G”。
kernel.shmmni:表示单个共享内存段的最小值,一般为4kB,即4096bit.
kernel.shmall:表示可用共享内存的总量,单位是页,在32位系统上一页等于4kB,也就是4096字节。
fs.file-max:表示文件句柄的最大数量。文件句柄表示在Linux系统中可以打开的文件数量。
ip_local_port_range:表示端口的范围,为指定的内容。
kernel.sem:表示设置的信号量,这4个参数内容大小固定。
net.core.rmem_default:表示接收套接字缓冲区大小的缺省值(以字节为单位)。
net.core.rmem_max :表示接收套接字缓冲区大小的最大值(以字节为单位)。
net.core.wmem_default:表示发送套接字缓冲区大小的缺省值(以字节为单位)。
net.core.wmem_max:表示发送套接字缓冲区大小的最大值(以字节为单位)。

生产环境案例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
net.ipv4.ip_forward = 0
net.ipv4.conf.default.rp_filter = 1
net.ipv4.conf.default.accept_source_route = 0
kernel.sysrq = 0
kernel.core_uses_pid = 1
net.ipv4.tcp_syncookies = 1
kernel.msgmnb = 65536
kernel.msgmax = 65536
kernel.shmmax = 68719476736
kernel.shmall = 4294967296
vm.overcommit_memory = 1
vm.swappiness = 1
fs.file-max = 1617992
fs.inotify.max_user_instances = 128
fs.inotify.max_user_watches = 1048576
fs.inotify.max_queued_events = 1048576
net.core.somaxconn = 262144
net.core.wmem_max = 16777216
net.core.rmem_max = 16777216
net.core.wmem_default = 131072
net.core.rmem_default = 131072
net.core.netdev_max_backlog = 262144
net.ipv4.tcp_timestamps = 1
net.ipv4.tcp_syn_retries = 1
net.ipv4.tcp_synack_retries = 1
net.ipv4.tcp_max_orphans = 262144
net.ipv4.tcp_max_tw_buckets = 1048576
net.ipv4.tcp_keepalive_time = 60
net.ipv4.tcp_keepalive_probes = 3
net.ipv4.tcp_keepalive_intvl = 15
net.ipv4.tcp_retries1 = 3
net.ipv4.tcp_retries2 = 5
net.ipv4.tcp_fin_timeout = 1
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_tw_recycle = 0
net.ipv4.tcp_max_syn_backlog = 262144
net.ipv4.ip_local_port_range = 10000 65535
net.ipv4.tcp_wmem = 4096 131072 16777216
net.ipv4.tcp_rmem = 4096 131072 16777216
net.ipv4.tcp_tw_reuse = 1