A comprehensive understanding of the proc file system in Linux

Source: Internet
Author: User

Proc is a virtual file system. In Linux, it is mounted on the/proc directory. Proc has multiple features, including the ability for users to access kernel information or to troubleshoot issues. One of these features is very useful, it is also a more special function for Linux to access process information in the form of text streams. Linux commands (such as PS and toppstree) need to use the information of this file system.

This article describes how to access this information. It should be noted that the content described in this article does not necessarily apply to all kernel versions, and some operations only apply to the 2.6 kernel.
I. Process Information
In the/proc file system, each process has a corresponding file. Below are some important files in the/proc directory:
/Proc/Pid/cmdline contains commands used to start processes;
/Proc/Pid/CWD contains a link to the working directory of the current process;
/Proc/Pid/environ contains a list of available process environment variables;
/Proc/Pid/EXE contains links to programs running in the process;
The/proc/Pid/FD/directory contains links to each file opened by the process;
/Proc/Pid/MEM contains the content of the process in the memory;
/Proc/Pid/STAT contains the Process status information;
/Proc/Pid/statm contains the memory usage information of the process.
The following are examples of obtaining process information:
◆ Assume that you want to check whether all processes will perform memory cleanup. Each process identifier (8th attributes) in the/proc/Pid/STAT file provides many "personal" messages about the process. This information can be used to identify each process.
0x00000002 process being created
0x00000004 exiting
0x00000008 dead
0x00000040 process using superuser privflage
0x00000200 p rocess dumping Core
0x00000400 prooess initialized ed some signal
0x00000800 process allocating memory
0x00001000 killed due to out-Of -- Memory Condition
This information is extracted from/usr/src/Linux/include/Linux/sched. h.
◆ The/proc/Pid/FD/directory provides information about opening a file. To view the input file used by the process, run the following command:
# Ls-L/proc/Pid/FD/0
◆ View the socket used by the process, and run the following command:
# Ls-L/proc/Pid/FD | sed-n'/socket/{S/. * \ [//; S/\] // P }'
You can use the following command to obtain information about these sockets:
# Netstat -- AE
◆ To pass the command line parameters to any process, run the following command:
~~ CAT/proc/Pid/cmdline
◆ To obtain the ID of the parent process of a process, run the following command:
# Grep ppid/proc/Pid/status
2. Common System Information
Proc contains a lot of system information, including CPU load, file system, and network configuration. The following are some examples of viewing and changing the system through proc:
◆ Obtain available system memory space:
# Grep free/proc/meminfo
◆ System statistics after the last system startup can be obtained from the/proc/STAT file:
# Grep processes/proc/STAT
◆ Obtain the average system load of 1st, 5, and 15 minutes:
# Awk '{print "1 min: \ t" $1 "\ N5 min: \ t" $2 "\ N15 min: \ t" $3}'/proc/loadavg
◆/Proc/partitions can be used to obtain the partition information of the system.
◆/Proc/net and/proc/sys/NET can be used to view and change important network information. For example, to disable the ping command, run the following command as root:
# Echo 1>/proc/sys/NET/IPv4/icmp_echo_ignore_all
To enable IP Forwarding/IP spoofing, run the following command:
# Echo 1>/proc/sys/NET/IPv4/ip_forward
◆ Information about mounted file systems can be retrieved from/proc/mounts.
◆ To change the host name in use, run the following command:
# Echo www. ABC. Corn>/proc/sys/kernel/hostname
◆ To obtain CPU information, run the following command:
# Cat/proc/cpuinfo
◆ To know the usage of swap space, run the following command:
# Cat/proc/SWAPs
◆ Obtain the normal running time of the system:
# Cat/proc/uptime
◆ List file systems shared by NFS:
# Cat/proc/fs/nfsd/exports
Iii. Related kernel information
This is a very broad topic. Here we will only introduce a small part of it:
◆ Obtain the kernel version from the running kernel:
# Cat/proc/version
◆ The/proc/kmsg file is used as the kernel log information source and can be used as an interface for calling system information.
◆ The/proc/kcore file allows you to access the physical memory of the system in the form of an internal core file, and GDB can be used to check the current status of any kernel data structure. If the source code is installed, you can view the/usr/src/Linux/documentation/filesystems/proc.txt file to obtain more information.
It can be seen that proc is a very good channel for obtaining system information. If it is well utilized, it can greatly help the maintenance and management of the Linux system.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.