GNU/Linux, Open Source, Cloud Computing, DevOps and more...

How to know how many cores and processors has a Linux box

1 comment

The simplest and shortest method to measure the number of processors present on a Linux box, which is also widely extended as it’s part of coreutils is:

~$ nproc --all
8

Another way to get the same result which also allows us to obtain additional information from our processor are the lscpu command:

~$ lscpu | grep 'CPU(s)'
CPU(s):                8
On-line CPU(s) list:   0-7
NUMA node0 CPU(s):     0-7

Or we can examine the cpuinfo file from /proc filesystem:

~$ grep processor /proc/cpuinfo | wc -l
8

There are many additional details about these processors in /proc/cpuinfo file, including the CPU model and number of cores:

~$ cat /proc/cpuinfo | grep "model name"
model name	: Intel(R) Xeon(R) CPU           E5520  @ 2.27GHz
model name	: Intel(R) Xeon(R) CPU           E5520  @ 2.27GHz
~$ cat /proc/cpuinfo | grep "cpu cores"
cpu cores	: 4
cpu cores	: 4

As you can see, the machine in our example has 2 processors with 4 cores each.

Recommended reading:
  Differences between physical CPU vs logical CPU vs Core vs Thread vs Socket


Error: Your Requested widget " ai_widget-6" is not in the widget list.
  • [do_widget_area above-nav-left]
    • [do_widget_area above-nav-right]
      • [do_widget_area footer-1]
        • [do_widget id="wpp-4"]
      • [do_widget_area footer-2]
        • [do_widget id="recent-posts-4"]
      • [do_widget_area footer-3]
        • [do_widget id="recent-comments-3"]
      • [do_widget_area footer-4]
        • [do_widget id="archives-4"]
      • [do_widget_area logo-bar]
        • [do_widget id="oxywidgetwpml-3"]
      • [do_widget_area menu-bar]
        • [do_widget id="search-3"]
      • [do_widget_area sidebar]
        • [do_widget id="search-4"]
        • [do_widget id="ai_widget-2"]
        • [do_widget id="categories-5"]
        • [do_widget id="ai_widget-3"]
        • [do_widget id="ai_widget-4"]
        • [do_widget id="ai_widget-5"]
      • [do_widget_area sub-footer-1]
        • [do_widget id="text-4"]
      • [do_widget_area sub-footer-2]
        • [do_widget_area sub-footer-3]
          • [do_widget_area sub-footer-4]
            • [do_widget_area upper-footer-1]
              • [do_widget id="search-2"]
              • [do_widget id="recent-posts-2"]
              • [do_widget id="recent-comments-2"]
              • [do_widget id="archives-2"]
              • [do_widget id="categories-2"]
              • [do_widget id="meta-2"]
            • [do_widget_area upper-footer-2]
              • [do_widget_area upper-footer-3]
                • [do_widget_area upper-footer-4]
                  • [do_widget_area widgets_for_shortcodes]
                    • [do_widget id="search-5"]
                    • [do_widget id="ai_widget-6"]
                  • [do_widget_area wp_inactive_widgets]
                    • [do_widget id="wpp-2"]
                    • [do_widget id="text-1"]
                    • [do_widget id="recent-posts-3"]
                    • [do_widget id="categories-3"]
                    • [do_widget id="archives-3"]
                    • [do_widget id="icl_lang_sel_widget-3"]

                  Due to HyperThreading technology and other processor enhancements, we might need to differentiate between physical and logical CPUs:

                  # Get number of physical cpus/cores:
                  ~$ lscpu -p | egrep -v '^#' | wc -l
                  4
                  # Get number of logical cpus (including those reported by hyperthreading cores):
                  ~$ lscpu -p | egrep -v '^#' | sort -u -t, -k 2,4 | wc -l
                  8
                  

                  The equivalent of the above for Mac OS X:

                  # Get number of physical cpus/cores:
                  ~$ sysctl -n hw.physicalcpu_max
                  4
                  # Get number of logical cpus (including those reported by hyperthreading cores):
                  ~$ sysctl -n hw.logicalcpu_max
                  8
                  

                  Another portable method that works on old boxes, both Linux and Mac OS X, which don’t have nproc or lscpu commands available is using the getconf command:

                  ~$ getconf _NPROCESSORS_ONLN
                  8
                  

                  We also can use alternative tools to get hardware information like dmidecode, but we’ll need root privileges:

                  ~# dmidecode -t 4 | egrep 'Socket Designation|Count'
                          Socket Designation: CPUSocket
                          Core Count: 8
                          Thread Count: 8
                  

                  If running the above commands you find out that there are multiple CPUs or multiple cores present in your system, you’ll want to also make sure that your current Linux kernel has multiprocessor support (SMP, Symmetric MultiProcessing):

                  ~# uname -a
                  Linux sasser 2.6.18-028stab092.1 #1 SMP Wed Jul 20 19:47:12 MSD 2011 i686 GNU/Linux
                  
                   

                  About the author

                  Daniel López Azaña
                  Freelance AWS Cloud Solution Architect & Linux Sysadmin

                  Entrepreneur, a generator of ideas and restless mind. Passionate about new technologies, especially Linux systems and Open Source Software. I also like to write about Technology News, Cloud Computing, AWS, DevOps, DevSecOps, System Security, Web Development and Programming, SEO, Science, Innovation, Entrepreneurship, etc.

                  DanielHow to know how many cores and processors has a Linux box

                  Related Posts

                  1 comment

                  Join the conversation
                  • ronald - 28/10/2012 reply

                    Or, in my case:
                    grep -i processor /proc/cpuinfo | wc -l

                  Leave a Reply

                  Your email address will not be published.