/Proc --- A Virtual File System
The/proc file system is a mechanism used by the kernel and kernel module to send information to the process (so it is called/proc ). This pseudo file system allows you to interact with the internal data structure of the kernel to obtain useful information about the process. On the fly, you can change the settings (by changing the kernel parameters ). Unlike other file systems,/proc exists in the memory rather than on the hard disk. If you view the file/proc/mounts (like the mount command to list all mounted file systems), you will see a line like this:
grep proc /proc/mounts
/proc /proc proc rw 0 0
/Proc is controlled by the kernel and there is no device carrying/proc. Because/proc stores state information controlled by the kernel, the logical location of most of this information is in the memory controlled by the kernel. If you perform 'LS-l' on/proc, you can see that most files are 0 bytes large. However, you can see some information when you view these files. How is this possible? This is because the/proc file system is consistent with other common file systems and registers itself to the virtual file system layer (VFS. However, the/proc file system will not establish the corresponding files and directories based on the information in the kernel until VFS calls it to request files and directories of I-nodes.
Load the proc file system
If the proc file system is not loaded yet, run the following command to load the proc file system:
Mount-T proc/proc
The above command will successfully load your proc file system. For more details, see the man page of the mount command.
View/proc files
/Proc files can be used to access information about the Kernel Status, computer attributes, and the status of running processes. Most files and directories in/proc provide the latest information about the physical environment of the system. Although files in/proc are virtual, they can still be viewed using any file editor or programs such as 'more', 'less ', or 'cat. When the editing program tries to open a virtual file, the file is created out of thin air by the information in the kernel. Here are some interesting results I got from my system:
$ ls -l /proc/cpuinfo
-r--r--r-- 1 root root 0 Dec 25 11:01 /proc/cpuinfo
$ file /proc/cpuinfo
/proc/cpuinfo: empty
$ cat /proc/cpuinfo
processor : 0
vendor_id : GenuineIntel
cpu family : 6
model : 8
model name : Pentium III (Coppermine)
stepping : 6
cpu MHz : 1000.119
cache size : 256 KB
fdiv_bug : no
hlt_bug : no
sep_bug : no
f00f_bug : no
coma_bug : no
fpu : yes
fpu_exception : yes
cpuid level : 2
wp : yes
flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca
cmov pat pse36 mmx fxsr xmm
bogomips : 1998.85
processor : 3
vendor_id : GenuineIntel
cpu family : 6
model : 8
model name : Pentium III (Coppermine)
stepping : 6
cpu MHz : 1000.119
cache size : 256 KB
fdiv_bug : no
hlt_bug : no
sep_bug : no
f00f_bug : no
coma_bug : no
fpu : yes
fpu_exception : yes
cpuid level : 2
wp : yes
flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca
cmov pat pse36 mmx fxsr xmm
bogomips : 1992.29
This is a result obtained from a dual-CPU system. Most of the above information clearly shows the useful hardware information of this system. Some/proc files are encoded. Different tools can be used to interpret the encoded information and output it into a readable form. Such tools include 'top', 'ps', and 'apm.
Obtain useful system/kernel information
The proc file system can be used to collect useful information about the system and the running kernel. The following are some important documents:
- /Proc/cpuinfo-CPU information (model, family, cache size, etc)
- /Proc/meminfo-physical memory, swap space, and other information
- /Proc/mounts-List of mounted file systems
- /Proc/devices-list of available devices
- /Proc/filesystems-Supported file systems
- /Proc/modules-loaded modules
- /Proc/version-kernel version
- /Proc/cmdline-kernel command line parameters entered at system startup
In Proc, there are more than the files listed above. Readers who want to learn more can get more information about every file in/proc or read references [1. I recommend that you use 'more' instead of 'cat' unless you know that this file is small, because some files (such as kcore) may be very long.
Information about running processes
The/proc file system can be used to obtain information about running processes. There are some numbered subdirectories in/proc. Each numbered directory corresponds to a process ID (PID ). In this way, each running process/proc has a directory named after its PID. These subdirectories contain files that provide important details about the process status and environment. Let's try to find a running process.
$ ps -aef | grep mozilla
root 32558 32425 8 22:53 pts/1 00:01:23 /usr/bin/mozilla
The preceding command shows that the PID of a running Mozilla process is 32558. Correspondingly,/proc should have a directory named 32558
$ ls -l /proc/32558
total 0
-r--r--r-- 1 root root 0 Dec 25 22:59 cmdline
-r--r--r-- 1 root root 0 Dec 25 22:59 cpu
lrwxrwxrwx 1 root root 0 Dec 25 22:59 cwd -> /proc/
-r-------- 1 root root 0 Dec 25 22:59 environ
lrwxrwxrwx 1 root root 0 Dec 25 22:59 exe -> /usr/bin/mozilla*
dr-x------ 2 root root 0 Dec 25 22:59 fd/
-r--r--r-- 1 root root 0 Dec 25 22:59 maps
-rw------- 1 root root 0 Dec 25 22:59 mem
-r--r--r-- 1 root root 0 Dec 25 22:59 mounts
lrwxrwxrwx 1 root root 0 Dec 25 22:59 root -> //
-r--r--r-- 1 root root 0 Dec 25 22:59 stat
-r--r--r-- 1 root root 0 Dec 25 22:59 statm
-r--r--r-- 1 root root 0 Dec 25 22:59 status
The "cmdline" file contains the command line called when the process is started. The environment of the "envir" process is changed to two. "Status" indicates the status of a process, including the user ID (UID), group ID (GID), parent process ID (ppid), and current status of the process, for example, "sleelping" and "running ". Each process directory has several symbolic links. "CWD" is a symbolic link pointing to the current working directory of the process. "EXE" points to the executable program of the running process, "root" points to the directory (usually "/") of the root directory "/"). The directory "FD" contains a link to the file descriptor used by the process. "CPU" only appears when running the SMP kernel, which is the process time by CPU.
/Proc/selfIs an interesting sub-Directory, which allows the program to conveniently use/proc to find information about the local process. /Proc/self is a symbolic link to the PID directory of the process accessing/proc in/proc.
Interaction with the kernel through/proc
Most of the/proc files discussed above are read-only. In fact, the/proc file system provides an interaction mechanism for the kernel through/proc files that can be read and written. Writing these files can change the state of the kernel. Therefore, you must modify these files with caution. The/proc/sys directory stores all the readable and writable files and can be used to change kernel behavior.
/Proc/sys/kernel-This directory contains information about General kernel behavior. /Proc/sys/kernel/{domainname, hostname} stores the domain name and Host Name of the machine/network. These files can be used to modify these names.
$ hostname
machinename.domainname.com
$ cat /proc/sys/kernel/domainname
domainname.com
$ cat /proc/sys/kernel/hostname
machinename
$ echo "new-machinename" > /proc/sys/kernel/hostname
$ hostname
new-machinename.domainname.com
In this way, you can modify the host name by modifying the files in the/proc file system. Many of its configurable files exist in/proc/sys/kernel /. It is not possible to list all these files here. Readers can view them in this directory to obtain more details.
Another configurable directory is/Proc/sys/Net. Files in this directory can be used to modify Network Properties of machines/networks. For example, simply modifying a file allows you to become addicted to computers hiding in the Internet.
$ echo 1 > /proc/sys/net/ipv4/icmp_echo_ignore_all
This will hide your machine in the Network addiction because it does not respond to ICMP_Echo. The host will not respond to ping queries from its host.
$ ping machinename.domainname.com
no answer from machinename.domainname.com
To change back to the default setting, as long
$ echo 0 > /proc/sys/net/ipv4/icmp_echo_ignore_all
There are many others in/proc/sys that can be used to change the kernel attributes. For more information, see references [1] and [2.
Conclusion
The/proc file system provides a file-based internal Linux interface. It can be used to determine the status of various devices and processes of the system. Configure them. Therefore, understanding and application knowledge about this file system is the key to understanding your Linux system.