Linux Commands

How To Use /proc File System for CPU Info

The best place to view comprehensive information about your kernel is the proc file system. It contains configuration parameters and other details about different processes, all structurally arranged inside the /proc directory. To view most of these files, you must have root privileges.

Most files in the /proc display information about the hardware and currently running processes, and you can change the permission of the files and modify them whenever necessary.

Understanding the /proc Directory

All the files in this directory are stored as virtual files and listed as zero bytes yet contain extensive information. The files get updated constantly, and their time stamps show the current time and date.

The files in the /proc directory get mounted at boot time and can be viewed using commands, such as less and cat.

The common files and their description are listed below:

  1. /proc/fb: it contains frame buffer devices
  2. /proc/devices: it contains drivers configured currently for the kernel
  3. /proc/cmdline: it contains the kernel command line details
  4. /proc/consoles: all details about current consoles, such as tty
  5. /proc/cpuinfo: all CPU information gets stored in the file

Viewing the /proc Files

The different files in the /proc directory represent various details. To view all the files contained, you can use the ls command:

$ ls /proc

In the previous output, you will note there are numbers and files. The numbers represent the PIDs for processes. As for the files, most have the name representing their purpose. For instance, cpuinfo contains the CPU information.

To view the contents of a specific file, list the contents using cat or less commands.

In the following example, we are viewing the memory file using cat.

$ cat /proc/meminfo

From the following output, you will note the file contains different information about the system’s memory, including free space.

Also, to get the CPU information, open the cpuinfo file using cat.

$ cat /proc/cpuinfo

Even though most of the contents of the file are viewable using root privileges, some are unreadable and require other commands, such as top, free, or lspci.

For instance, the top command lists all running processes we saw in the /proc directory.

Similarly, to view the PCI devices, you must run the lspci command as its file is not human-readable.

Editing the Files

Most of the /proc files are read-only. The few that are writable, especially those in the /proc/sys, can be changed to adjust different kernel settings.

The syntax to change the values of the writeable files is:

$ echo [value] > /proc/file

For instance, the hostname gets stored in the /proc/sys/kernel, and you can edit and use a new hostname using the following command. Here, replace the examplehost1 with your preferred hostname:

$ echo examplehost1 > /proc/sys/kernel/hostname

Running the commands with root privileges edits the kernel details as in the previous example. Besides, you can check the values of given variables using the cat command. For instance, you can run the following command to check if ipv4 packet forwarding is enabled.

$ cat /proc/sys/net/ipv4/ip_forward

The output here is binary with zero representing false, meaning port forwarding is not enabled.

Other Directories in the /proc

For different processes, their directories are numbered in blue, and each name represents the process ID and contains details of the specific process. Inside each process directory, you will get different files, such as maps, fd, exe, and cmdline.

Still, you will note other unnumbered directories, which contain other files. One such directory is the /proc/sys, which contains information about the system and kernel features.

Conclusion

This guide covered the /proc file system in Linux that contains different virtual files. We’ve covered how to list the available files and display the contents of a specific file in the directory.

Furthermore, we have seen how you can change the values for the writable files, provided you have root privileges. Hopefully, you now understand working with the /proc file system in Linux.

About the author

Denis Kariuki

Denis is a Computer Scientist with a passion for Networking and Cyber Security. I love the terminal, and using Linux is a hobby. I am passionate about sharing tips and ideas about Linux and computing.