{"id":3771,"date":"2013-09-15T17:06:53","date_gmt":"2013-09-15T21:06:53","guid":{"rendered":"http:\/\/www.databook.bz\/?page_id=3771"},"modified":"2013-09-18T12:43:38","modified_gmt":"2013-09-18T16:43:38","slug":"the-proc-filesystem","status":"publish","type":"page","link":"http:\/\/www.linux-databook.info\/?page_id=3771","title":{"rendered":"The \/proc filesystem"},"content":{"rendered":"<p>The \/proc filesystem is one of the most important filesystems on any Linux computer. It is also a very powerful tool for the system administrator. It is both a view into the instantaneous status of the running kernel and a way to modify the operational parameters of the kernel.<\/p>\n<p>The \/proc filesystem is a virtual filesystem. It exists in RAM only and is not stored or recorded on the hard drive. It therefore exists only when the computer is turned on and running.<\/p>\n<p>The \/proc filesystem contains many different types of data. All of this data is observable by any user or process, whether root or non-root. This means that users, programs and scripts can all access any of the data in the \/proc filesystem without the necessity of using protected mode system calls and their attendant CPU overhead. The kernel has been designed specifically to store performance data there and to use the configuration variables stored there for configuration and tuning.<\/p>\n<p>For example, each process has a subdirectory in the \/proc filesystem, with the name of the directory being the process ID (PID) of the process. That subdirectory contains all of the information about a process including private and shared memory usage, various statistics, the environment variables, current status, stack contents, and a great deal more.<\/p>\n<p>The \/proc directory itself contains many system statistics including memory and CPU data, load averages, disk statistics, device information, a count of interrupts and much more.<\/p>\n<p>The \/proc\/sys directory and its subdirectories contains files that contain kernel configuration parameters.<\/p>\n<p>Data stored in \/proc\/sys\/kernel includes information such as the hostname, OS information (the stuff you get from the uname command), the maximum PID number that the kernel can assign, and again, much much more. The difference with these parameters is that they can be changed. But you should only do so if you know exactly what you are doing.<\/p>\n<p>Changes to kernel configuration parameters is instantaneous and no reboot of the system is required for them to take effect.<\/p>\n<h1>Viewing system data<\/h1>\n<p>There are a large number of Linux commands that are used in the process of analyzing system performance and problem determination. Most of these commands obtain their information from various files in the \/proc filesystem.<\/p>\n<p>For example. the <strong><span style=\"font-family: andale mono,times;\">free<\/span><\/strong> command displays the amount of free memory as well as the memory and swap space in use.<\/p>\n<p style=\"padding-left: 30px;\"><span style=\"font-family: andale mono,times;\">[root@voyager ~]# <strong>free<\/strong><\/span><br \/>\n<span style=\"font-family: andale mono,times;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 total\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 used\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 free\u00a0\u00a0\u00a0\u00a0 shared\u00a0\u00a0\u00a0 buffers\u00a0\u00a0\u00a0\u00a0 cached<\/span><br \/>\n<span style=\"font-family: andale mono,times;\">Mem:\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 8107216\u00a0\u00a0\u00a0 4583236\u00a0\u00a0\u00a0 3523980\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 0\u00a0\u00a0\u00a0\u00a0\u00a0 69524\u00a0\u00a0\u00a0 2460124<\/span><br \/>\n<span style=\"font-family: andale mono,times;\">-\/+ buffers\/cache:\u00a0\u00a0\u00a0 2053588\u00a0\u00a0\u00a0 6053628<\/span><br \/>\n<span style=\"font-family: andale mono,times;\">Swap:\u00a0\u00a0\u00a0\u00a0\u00a0 4194300\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 0\u00a0\u00a0\u00a0 4194300<\/span><\/p>\n<p>Other commands like top, htop, w, and many more use the \/proc filesystem as the source for their data. Thus there is no need for simple utilities to have privileged access to the kernel. The kernel stores all of its performance data in a virtual filesystem that is freely accessible to all users.<\/p>\n<p>All of the data displayed by these commands and many others are stored by the kernel in the \/proc filesystem. Because the kernel already stores this data in an easily accessible location and format, it is possible for other programs to access it with no impact upon the performance of the kernel.<\/p>\n<p>Each of the following commands allow you to view some of the raw data from the \/proc filesystem.<\/p>\n<p class=\"western\" style=\"margin-left: 0.5in; margin-bottom: 0in; font-weight: normal;\"><span style=\"font-family: andale mono,times;\"><span style=\"color: #000000;\"><span style=\"font-size: small;\"><b><span style=\"background: none repeat scroll 0% 0% transparent;\">cat \/proc\/meminfo<br \/>\ncat \/proc\/cpuinfo<br \/>\ncat \/proc\/loadavg<\/span><\/b><\/span><\/span><\/span><\/p>\n<p>Cat each of the above files multiple times to observe the changes in the various data as the kernel changes.<\/p>\n<p>These are just a few of the files in \/proc that contain incredibly useful information.<\/p>\n<h1>Modifying the running kernel<\/h1>\n<p>It is possible to modify the configuration parameters of the running kernel. You may wish to do this to tune the performance parameters of the system, or to alter its functionality. Note that only root user can make changes to the \/proc filesystem.<\/p>\n<p>For example, you may wish to configure the host as a router. This is a fairly trivial thing to do and requires only adding one or two entries to your firewall and a change to the kernel parameter, ip_forward.<\/p>\n<p>If you cat the contents of \/proc\/sys\/net\/ipv4\/ip_forward it is zero (0) by default. To turn your computer into a router use the command below.<\/p>\n<p style=\"padding-left: 30px;\"><strong><span style=\"font-family: andale mono,times;\">echo 1 &gt; \/proc\/sys\/net\/ipv4\/ip_forward<\/span><\/strong><\/p>\n<p>This command turns your Linux computer into a router by turning on IP packet forwarding. Instantly. No reboot required. See the page &#8220;<a href=\"http:\/\/www.linux-databook.info\/?page_id=697\">Making your Linux Box Into a Router&#8221;<\/a> for the complete procedure required to perform this task.<\/p>\n<p>To test a more visible change, you can alter the hostname in \/proc\/sys\/kernel\/hostname. If you <strong><span style=\"font-family: andale mono,times;\">cat<\/span><\/strong> that file you can see the current value of the hostname. Type the\u00a0<strong>hostname<\/strong>\u00a0 command to see the current hostname as the system sees it. Use the command below to alter the value in that file.<\/p>\n<p style=\"padding-left: 30px;\"><strong><span style=\"font-family: andale mono,times;\">echo test.example.com &gt; \/proc\/sys\/kernel\/hostname<\/span><\/strong><\/p>\n<p>Now use the <strong>hostname<\/strong>\u00a0 command again to see that the hostname has changed. Note that the hostname that is part of your command prompt does not change. The reason is that each program inherits its environment variables when it is started and the environment is not changed unless it is overtly changed by the user. One of those environment variables is $HOSTNAME. If you use the command <span style=\"font-family: andale mono,times;\"><strong>echo<\/strong><\/span><strong> $HOSTNAME<\/strong> you will see that it contains the old value for the hostname.<\/p>\n<p>You do need to be aware that any kernel configuration change made in the \/proc\/sys directory is only temporary until the next reboot. All changes you make in \/proc\/sys are non-persistent and will be replaced by the default values at each reboot.<\/p>\n<p>The point here is that the configuration parameters of the live, running kernel can be changed instantaneously using the \/proc filesystem, and that no ugly reboots are required.<\/p>\n<h2>Making changes permanent<\/h2>\n<p>To make permanent any kernel configuration changes there are configuration files that are parsed at boot time and which can be used to make any changes persistent across reboots.<\/p>\n<p>For CentOS and fedora up through Fedora 18, change the value of following line in the \/etc\/sysctl.conf file from 0 (zero) to 1 (one).<\/p>\n<p style=\"padding-left: 30px;\"><span style=\"font-family: andale mono,times;\"># Controls IP packet forwarding<\/span><br \/>\n<span style=\"font-family: andale mono,times;\"> net.ipv4.ip_forward = 1<\/span><\/p>\n<p>For Fedora 19 and above the default entries in the \/etc\/sysctl.conf file have been moved to a series of files in the \/usr\/lib\/sysctl.d\/ directory. However the remaining stub of the \/etc\/sysctl.conf file can be used to add non-default entries for sysctl. So add the following lines to the \/etc\/sysctl.conf file.<\/p>\n<p style=\"padding-left: 30px;\"><span style=\"font-family: andale mono,times;\"># Controls IP packet forwarding<\/span><br \/>\n<span style=\"font-family: andale mono,times;\"> net.ipv4.ip_forward = 1<\/span><\/p>\n<p>Note that the hostname is not set permanently in the \/etc\/sysctl.conf file, but in the \/etc\/sysconfig\/hostname file. The hostname command can be used to permanently change the hostname as shown in the sample command below.<\/p>\n<p style=\"padding-left: 30px;\"><strong><span style=\"font-family: andale mono,times;\">hostname test.example.com<\/span><\/strong><\/p>\n<p>You can\u00a0 also change the hostname in the \/etc\/sysconfig\/hostname file and then reboot the system for the name change to take effect.<\/p>\n<p>On systems that use systemd for startup, the hostname command will set the hostname temporarily but this change is not persistent through a reboot.<\/p>\n<p>To change the hostname on systems using systemd, change the contents of the \/etc\/hostname file.<\/p>\n<p>See &#8220;<a title=\"Setting the host name\" href=\"http:\/\/www.linux-databook.info\/?page_id=3669\" target=\"_blank\">Setting the host name<\/a>&#8221; for more details on changing the hostname.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The \/proc filesystem is one of the most important filesystems on any Linux computer. It is also a very powerful tool for the system administrator. It is both a view into the instantaneous status of the running kernel and a&hellip;<\/p>\n<p class=\"more-link-p\"><a class=\"more-link\" href=\"http:\/\/www.linux-databook.info\/?page_id=3771\">Read more &rarr;<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"parent":1623,"menu_order":5,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-3771","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"http:\/\/www.linux-databook.info\/index.php?rest_route=\/wp\/v2\/pages\/3771","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.linux-databook.info\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"http:\/\/www.linux-databook.info\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"http:\/\/www.linux-databook.info\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.linux-databook.info\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3771"}],"version-history":[{"count":23,"href":"http:\/\/www.linux-databook.info\/index.php?rest_route=\/wp\/v2\/pages\/3771\/revisions"}],"predecessor-version":[{"id":3815,"href":"http:\/\/www.linux-databook.info\/index.php?rest_route=\/wp\/v2\/pages\/3771\/revisions\/3815"}],"up":[{"embeddable":true,"href":"http:\/\/www.linux-databook.info\/index.php?rest_route=\/wp\/v2\/pages\/1623"}],"wp:attachment":[{"href":"http:\/\/www.linux-databook.info\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3771"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}