The Linux /proc filesystem
Introducing /proc
The /proc filesystem is a virtual filesystem (also called a pseudo or an in-memory filesystem) and is an interface to the internal data structures in the kernel. /proc serves as an image of the in-memory kernel data structures and provides file based communication with the kernel.
Typing ls -ld /proc shows that /proc is a directory of size zero, as can be seen below.
dr-xr-xr-x 133 root root 0 Mar 25 09:55 proc
However if you take a look inside the directory there are a lot of files and directories in there.
If I cd into /proc and type ls, I see the following
1 2625 30364 3737 3964 4528 9 filesystems partitions 1033 2633 30446 3754 3965 4529 904 fs scsi 1034 2641 30449 3784 4 4530 905 ide self 10902 2649 30450 3787 4019 4531 906 interrupts slabinfo 1110 2657 30490 3833 431 4532 907 iomem splash 12 2665 30491 3866 4342 4563 9810 ioports stat 13 2673 31123 3880 4352 4796 acpi irq swaps 14 2681 31335 3899 4392 4798 buddyinfo kallsyms sys 1645 2689 31351 3902 4442 5 bus kcore sysrq-trigger 1704 2697 31352 391 4469 6 cmdline kmsg sysvipc 1775 2705 31551 392 4471 7 config.gz loadavg tty 181 2713 31557 393 4489 791 cpuinfo locks uptime 182 2715 3159 3942 4504 792 crypto mdstat version 183 3 3163 3957 4513 799 devices meminfo vmstat 184 3020 3166 3958 4514 8 diskstats misc zoneinfo 185 30242 3173 3959 4516 800 dma modules 2 30254 3221 3960 4517 823 driver mounts 2279 30359 3320 3961 4518 829 execdomains mtrr 2617 30360 3721 3963 4527 893 fb net
Most of these files and directories are zero size, yet can hold a lot of information, this is because they are actually a window into the kernel running in memory.
Most of these files are dynamic and are constantly updated.
Viewing information about the running system
Each file in /proc can be viewed using utilities such as cat or more and can contain some useful information.
For instance on my server typing cat meminfo shows the following
# cat meminfo MemTotal: 970276 kB MemFree: 27752 kB Buffers: 217556 kB Cached: 308684 kB SwapCached: 0 kB Active: 412428 kB Inactive: 348748 kB HighTotal: 65472 kB HighFree: 408 kB LowTotal: 904804 kB LowFree: 27344 kB SwapTotal: 4194296 kB SwapFree: 4194096 kB Dirty: 32 kB Writeback: 0 kB Mapped: 261100 kB Slab: 162224 kB CommitLimit: 4679432 kB Committed_AS: 509840 kB PageTables: 1404 kB VmallocTotal: 114680 kB VmallocUsed: 20776 kB VmallocChunk: 93484 kB HugePages_Total: 0 HugePages_Free: 0 HugePages_Rsvd: 0 Hugepagesize: 4096 kB
This output shows how much memory my server has, how much is free, how much swap I have and so on.
Similarly we can look at the contents of cpuinfo by typing cat cpuinfo as follows.
#cat cpuinfo processor : 0 vendor_id : AuthenticAMD cpu family : 15 model : 43 model name : AMD Athlon(tm) 64 X2 Dual Core Processor 4200+ stepping : 1 cpu MHz : 2210.320 cache size : 512 KB physical id : 0 siblings : 2 core id : 0 cpu cores : 2 fdiv_bug : no hlt_bug : no f00f_bug : no coma_bug : no fpu : yes fpu_exception : yes cpuid level : 1 wp : yes flags : bogomips : 4427.26 processor : 1 vendor_id : AuthenticAMD cpu family : 15 model : 43 model name : AMD Athlon(tm) 64 X2 Dual Core Processor 4200+ stepping : 1 cpu MHz : 2210.320 cache size : 512 KB physical id : 0 siblings : 2 core id : 1 cpu cores : 2 fdiv_bug : no hlt_bug : no f00f_bug : no coma_bug : no fpu : yes fpu_exception : yes cpuid level : 1 wp : yes flags : bogomips : 4420.38
There are many more files that can be viewed in this way. I won't include them all here but have a look on your server.
We can also see from the listing of /proc that there are a many numbers, each one is a directory and the number of a currently running process on the system. We can verify this by typing ps ax to see all the running processes, compare this list with the numbered directories in /proc, they should be the same.
Have a look in one of these directories and you will see many files and some sub-directories, an example of the contents of an httpd2 process directory is shown below.
#ls -l /proc/4513 -r--r--r-- 1 root root 0 Apr 1 11:30 statm lrwxrwxrwx 1 root root 0 Apr 1 11:30 exe -> /usr/sbin/httpd2-prefork -r--r--r-- 1 root root 0 Apr 1 11:30 cmdline -r--r--r-- 1 root root 0 Apr 1 14:33 status -r--r--r-- 1 root root 0 Apr 1 14:33 stat -r--r--r-- 1 root root 0 Apr 1 16:00 wchan dr-xr-xr-x 3 root root 0 Apr 1 16:00 task -r--r--r-- 1 root root 0 Apr 1 16:00 smaps -rw------- 1 root root 0 Apr 1 16:00 seccomp lrwxrwxrwx 1 root root 0 Apr 1 16:00 root -> / -r--r--r-- 1 root root 0 Apr 1 16:00 oom_score -rw-r--r-- 1 root root 0 Apr 1 16:00 oom_adj -r--r--r-- 1 root root 0 Apr 1 16:00 mounts -rw------- 1 root root 0 Apr 1 16:00 mem -r--r--r-- 1 root root 0 Apr 1 16:00 maps -rw------- 1 root root 0 Apr 1 16:00 mapped_base -rw-r--r-- 1 root root 0 Apr 1 16:00 loginuid dr-x------ 2 root root 0 Apr 1 16:00 fd -r-------- 1 root root 0 Apr 1 16:00 environ lrwxrwxrwx 1 root root 0 Apr 1 16:00 cwd -> / -r--r--r-- 1 root root 0 Apr 1 16:00 cpuset -r-------- 1 root root 0 Apr 1 16:00 auxv dr-xr-xr-x 2 root root 0 Apr 1 16:00 attr
Again these files contain information that can be viewed. For instance take a look at the file maps, this will contain a list of the libraries in use by this process.
Adjusting kernel settings on the fly
Most of the files in /proc are read only, however some are writable allowing settings to be adjusted in the kernel. There are many such files in /proc/sys.
We can change the hostname by changing the contents of the file /proc/sys/kernel/hostname as follows.
echo newhostname > /proc/sys/kernel/hostname
Unless of course your server is already called newhostname.
In a similar way we can turn on or off the kernel's ability to forward network packets. Echoing a 1 to /proc/sys/net/ipv4/ip_forward will turn ip-forwarding on and a 0 to /proc/sys/net/ipv4/ip_forward will turn ip-forwarding off as follows.
echo 1 > /proc/sys/net/ipv4/ip_forward
echo 0 > /proc/sys/net/ipv4/ip_forward