Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Detailed explanation of sysctl and / proc

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

Catalogue

A brief introduction to sysctl

Second, the use of sysctl command

Third, sysctl configuration file / etc/sysctl.conf detailed explanation

4. Brief introduction of / proc directory

5. The corresponding relationship between the kernel file and the variables in the configuration file sysctl.conf under / proc/sys

A brief introduction to sysctl

Sysctl is a tool for viewing and adjusting system parameters during system operation. Some sysctl parameters are only used to view the current system status, such as the current boot time, the version of the operating system used, the core name, and so on, while others allow us to modify the parameters to adjust the behavior of the system, such as the size of network temporary memory, the maximum number of users, and so on.

/ etc/sysctl.conf is the configuration file of sysctl, and these adjustable parameters must be set before the boot system executes other programs, and some can be adjusted arbitrarily after boot. Like most configuration files, we can configure sysctl.conf to optimize system performance.

Second, the use of sysctl command

[root@www] # sysctl-- helpusage: sysctl [- n] [- e] variable. Sysctl [- n] [- e] [- Q]-w variable=value... Sysctl [- n] [- e]-a sysctl [- n] [- e] [- Q]-p (default / etc/sysctl.conf) sysctl [- n] [- e]-A

Common parameters:

-w temporarily change the value of a specified parameter, such as

Sysctl-w net.ipv4.ip_forward=1

-a displays all system parameters

-p loads system parameters from the specified file, or from / etc/sysctl.conf if not specified

III. Sysctl configuration file / etc/sysctl.conf

[root@ElementServer ~] # cat / etc/sysctl.conf | grep-v'^ #\ | ^ $'net.ipv4.ip_forward = 0net.ipv4.conf.default.rp_filter = 1net.ipv4.conf.default.accept_source_route = 0kernel.sysrq = 0kernel.core_uses_pid = 1net.ipv4.tcp_syncookies = 1net.bridge.bridge-nf-call-ip6tables = 0net.bridge.bridge-nf-call-iptables = 0net.bridge.bridge-nf-call-arptables = 0kernel.msgmnb = 5368760912kernel.msgmax = 16777216kernel.shmmax = 68719476736kernel.shmall = 4294967296kernel.shmmni = 8192fs.file-max = 6553560

Explain the meaning of the parameters in the sysctl.conf file:

Net.ipv4.ip_forward = 0:

For security reasons, the Linux system prohibits packet forwarding by default. The so-called forwarding means that when the host has more than one network card, one of them receives the data packet and sends the packet to another local network card according to the destination ip address of the data packet, which continues to send the data packet according to the routing table. This is usually what the router is supposed to do.

Configure the ip forwarding function of the Linux system, first ensure hardware connectivity, and then turn on the forwarding function of the system.

Less / proc/sys/net/ipv4/ip_forward, which contains 0, forbids packet forwarding, 1 indicates permission, and modifies it to 1. You can use the command echo "1" > / proc/sys/net/ipv4/ip_forward to modify the contents of the file, and the effect will no longer be achieved after restarting the network service or host. For automatic execution, you can net.ipv4.ip_forward = 1 in / etc/sysctl.conf

Hold the command echo "1" > / proc/sys/net/ipv4/ip_forward to write the script / etc/rc.d/rc.local

Or add FORWARD_IPV4= "YES" to the / etc/sysconfig/network script

Kernel.shmmax:

Is one of the most important core parameters that defines the maximum value of a single shared memory segment. The setting should be large enough to accommodate the entire SGA under one shared memory segment, and a low setting may result in the need to create multiple shared memory segments, which may lead to system performance degradation. The main reason for the system decline is that when the instance is started and the ServerProcess is created, multiple small shared memory segments may lead to a slight degradation of system performance at that time (multiple virtual address fields need to be created at startup, and there will be some impact when the process is created to let the process "identify" multiple segments), but it will not be affected at other times.

Official recommended values:

32-bit linux system: the maximum preferred value is 4GB (4294967296bytes)-1byte, which is 4294967295. The recommended value is more than half of the memory, so if 32 is the system, the value is generally 4294967295. 32-bit systems have limits on the size of the SGA, so the SGA can certainly be included in a single shared memory segment.

64-bit linux system: the maximum preferred value is physical memory value-1byte, and the recommended value is more than half of physical memory. Generally, the value is greater than SGA_MAX_SIZE, and you can take physical memory-1byte. For example, if it is 12GB physical memory, the preferred 12884901887 memory will definitely be included in a single shared memory segment.

Kernel.shmall:

This parameter controls the total number of pages of shared memory that can be used. The page size of Linux shared memory is 4KB, and the size of shared memory segments is an integral multiple of the size of shared memory pages. The maximum size of a shared memory segment is 16G, so the number of pages of shared memory needs to be 16GB/4KB=16777216KB / 4KB=4194304 (pages), that is, 16GB physical memory under 64Bit system. Set kernel.shmall = 4194304 to meet the requirement (almost double the original setting of 2097152). At this time, the shmmax parameter can be adjusted to 16G, and the SGA_MAX_SIZE and SGA_TARGET can be changed to 12G (the maximum size of SGA you want to set, of course, can also be 2G~14G, etc., and coordinate the use of PGA parameters and other memory such as OS. Do not set too full, for example, 16G).

Kernel.shmmni:

This parameter is the maximum number of shared memory segments. The default value of shmmni is 4096, which is generally sufficient.

Fs.file-max:

This parameter determines the maximum number of file handles allowed in the system, and the file handle setting represents the number of files that can be opened in the linux system.

Fs.aio-max-nr:

This parameter limits the concurrency of outstanding requests and should be set to avoid the failure of the Istroke O subsystem.

The recommended value is: 1048576, in fact, it is equal to 1024K 1024K.

Kernel.sem:

Take kernel.sem = 250 32000 100128 as an example:

250 is the value of the parameter semmsl, indicating the maximum number of semaphores that can be contained in a semaphore set.

32000 is the value of the parameter semmns, indicating the maximum number of semaphores allowed in the system.

100 is the value of the parameter semopm, which represents the number of operations that a single semopm () call can perform on a semaphore set.

The value of the parameter semmni represents the total set of system semaphores.

Net.ipv4.ip_local_port_range:

Represents the range of IPv4 ports that the application can use.

Net.core.rmem_default:

The default value that represents the size of the socket receive buffer.

Net.core.rmem_max:

Represents the maximum size of the socket receive buffer.

Net.core.wmem_default:

The default value that represents the size of the socket send buffer.

Net.core.wmem_max:

Represents the maximum size of the socket send buffer

4. Brief introduction of / proc directory

Everyone knows that the processes are in memory, and the data in memory is written to a file in the / proc/ directory, so it is necessary to analyze the contents of this folder. Because the information of the system, such as the process, changes dynamically, when the user or application reads the proc file, the proc file system dynamically reads the required information from the system kernel and submits it.

1. Overview of proc file system

A good abstract philosophy is embodied in the Unix-like system, that is, almost all data entities are abstracted into a unified interface-file, so that we can use some simple basic tools to complete a large number of complex operations.

There is a special class of pseudo file systems in Linux, which are used to perform various functions using operations unified with the file interface, such as ptyfs, devfs, sysfs, and procfs. Procfs is one of the most widely used pseudo file systems.

Procfs is the abstract file interface of Linux kernel information. A large number of kernel information and adjustable parameters are mapped to a directory tree as regular files, so that we can simply and directly query and adjust the system information through file operation commands such as echo or cat. At the same time, procfs also provides an interface so that our own kernel modules or user-mode programs can pass parameters through procfs. In today's Linux systems, a large number of system tools also obtain kernel parameters through procfs, such as ps, lspci, and so on. Without procfs, they may not work properly.

Procfs is used just like a regular file system, such as loading a procfs (first make sure your kernel already supports procfs by default):

# mount-t proc none / proc

You can also add the following to / etc/fstab that the system automatically loads procfs at startup (which is also the default on most systems):

None / proc proc defaults 0 0

In the proc file system, there are mainly three categories of content, process-related part, system information part, and system subsystem part.

We can see a typical procfs layout below:

[root@www] # ls / proc1 1230 147 2198 283 41 750 cpuinfo ipmi misc slabinfo vmstat10 1238 14822 284 42 8 crypto irq modules softirqs zoneinfo1007 1249 15 2265 295 8958 devices kallsyms mounts stat1037 1262 22958 293 586 9 diskstats kcore mpt swaps1046 1264 157 22966 3 6 9271 dma keys mtd sys1047 1266 158 23 30 7 935 driver key-users mtrr sysrq-trigger1076 1268 16 23062 31 705 951 execdomains kmsg net sysvipc1091 1271 17 23215 32 706 acpi fb kpagecount pagetypeinfo timer_list11 1273 18 24 33 707 asound filesystems kpageflags partitions timer_stats1110 1274 19 25 369 708 buddyinfo fs loadavg sched_debug tty1198 1275 2 26 38 709 bus interrupts locks schedstat uptime12 13 20 27 39 710 cgroups iomem mdstat scsi version1222 14 21 28 4 72 cmdline ioports meminfo self vmallocinfo

In the above we can see what procfs contains:

* process related parts (read-only)

These files are all subdirectories with the name of a number, which is the process ID of the related process. The structure and information of the process-related subdirectories will be described in detail later.

It is important to note that a special point in the process subsystem part of procfs is / proc/self, which is a symbolic link to the current execution process, or-- telepathy to the instructions you are about to execute in the future:

> cat self/cmdline

Catself/cmdline

In this command, we want it to display the cmdline parameters of the current process, which precisely shows the current process-- the command we are executing, cat self/cmdline. Fortunately, the information displayed in this instruction ignores spaces, causing the display to be slightly abnormal, otherwise we can easily use commands such as `cat self/ cmdline` to create a process that runs forever. ; >

* Kernel information section (read-only)

These files are also in the top-level directory of / proc, but most of them are regular, read-only text files that can be viewed directly with cat. As an abstraction of the kernel executor of the system, we can also think of it as the information part of the kernel "process", although of course this process entity does not exist. A special file here is / proc/cmdline:

> cat cmdline

Ro root=/dev/hda2

In this file are the command-line arguments when the system kernel is booted.

* relevant parts of each subsystem of the kernel (partially adjustable)

This part is an important part of the system kernel parameter adjustment. In procfs, in addition to the two parts mentioned above, a large number of information files are stored in special directories that are not named by numbers. The information in these directories is the information and adjustable parameters of the important subsystems of the kernel.

The main ones are:

Bus bus information (read-only)

Drivers driver information (read-only)

Fs file system special information (read-only)

Ide IDE interface information (read-only)

Irq IRQ information (read-only)

Net Network Subsystem Information (read-only)

Scsi SCSI system Information (read-only)

Sysvipc IPC subsystem information (read-only)

Tty tty subsystem information (read-only)

Sys kernel tunable parameters (adjustable)

As an abstract file interface for Linux kernel parameters, most of the default tunable parameters of the Linux kernel are placed in the / proc/sys directory. These parameters are in the form of regular files and can be adjusted with file manipulation commands such as echo/cat. The effect of the adjustment is immediate and effective throughout the life cycle of the system (until they are changed again or the system is rebooted).

Of course, Linux also provides another way to adjust these parameters by sysctl. Sysctl is a method of dynamic adjustment of system parameters inherited from the BSD system. Sysctl is easier to use, and you can use / etc/sysctl.conf to save the configuration to load these settings automatically at next startup.

Most of the kernel parameters are stored in the / proc/sys directory, and are designed to be changed while the system is running. You can modify the kernel parameters by changing the file corresponding to the kernel parameters in / proc/sys (after modification, the saved configuration file will automatically take effect), but the parameter values modified before rebooting the machine will become invalid, so it can only be a temporary parameter change scheme. (suitable for debugging kernel parameter optimization values. If there is a problem with the setting value, restart the server to restore the original setting parameter value. Simple and convenient.)

But if you need to permanently save the parameter values after debugging the kernel parameter optimization values, you need to permanently save the changes by modifying the kernel parameters in / etc/sysctl.conf. However, you only modify the parameter values in the sysctl file. After confirming that the modified file is saved, the parameter values set will not take effect immediately. If you want to make the parameter value changes take effect immediately and do not restart the server, you can execute the following command:

# sysctl-p

5. The corresponding relationship between the kernel file and the variables in the configuration file sysctl.conf under / proc/sys

Because the kernel parameters that can be modified are all in the / proc/sys directory, the variable name of sysctl.conf omits the first part of the directory (/ proc/sys).

Converting files in / proc/sys to variables in sysctl is based on the following two simple rules:

1. Remove the previous part / proc/sys

2. Change the slash in the file name to a dot

These two rules can convert any file name in / proc/sys to a variable name in sysctl.

For example:

/ proc/sys/net/ipv4/ip_forward = "net.ipv4.ip_forward

/ proc/sys/kernel/hostname = "kernel.hostname

You can query all modifiable variable names using the following command

# sysctl-a

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report