Baeldung Pro – Linux – NPI EA (cat = Baeldung on Linux)
announcement - icon

Learn through the super-clean Baeldung Pro experience:

>> Membership and Baeldung Pro.

No ads, dark-mode and 6 months free of IntelliJ Idea Ultimate to start with.

1. Introduction

Most users switching to Linux from Windows/Mac OS or booting up a Linux machine for the first time wonder where they can see all the system properties, such as CPUs, RAM, disk usage, etc. On most Linux systems, the usual way to view and manage these properties is through separate command-line tools. Some distributions bundle these tools into a neat graphical interface, but these are updated very frequently and change with each distribution.

In this tutorial, we’ll look at some commands to view system properties and manage the device via the Linux command line.

2. CPU Information

The CPU is a key component of the computer that we’ll be looking at first. In this section, we’ll see different methods to obtain information about the CPUs on our current system.

2.1. Reading /proc/cpuinfo

/proc is a virtual filesystem that’s created during system boot and dissolved on shutdown. It acts as a medium for accessing various system and kernel parameters via a file and directory interface. To get the CPU information of our device, we’ll simply read the /proc/cpuinfo file using the cat command:

$ cat /proc/cpuinfo

This gives us the following output:

processor	: 0
vendor_id	: GenuineIntel
cpu family	: 6
model		: 61
model name	: Intel(R) Core(TM) i5-5257U CPU @ 2.70GHz
stepping	: 4
microcode	: 0x2f
cpu MHz		: 1889.830
cache size	: 3072 KB
...truncated...

From the above output, we observe that it’s comprehensive and contains all the relevant info about our CPU. Some of the information is presented as flags, which tell us all of the individual, vendor-specific features of our system’s processor.

2.2. Using the lscpu Command

The lscpu command provides a useful interface that combines the output of /proc/cpuinfo and other files to provide a more readable CPU information output:

$ lscpu
Architecture:             x86_64
  CPU op-mode(s):         32-bit, 64-bit
  Address sizes:          39 bits physical, 48 bits virtual
  Byte Order:             Little Endian
CPU(s):                   4
  On-line CPU(s) list:    0-3
Vendor ID:                GenuineIntel
  Model name:             Intel(R) Core(TM) i5-5257U CPU @ 2.70GHz
    CPU family:           6
    Model:                61
...truncated...

The above output is noticeably more readable, with key information such as CPU architecture and number of cores right at the beginning of the output.

3. RAM Information

After CPU, the next key component that we’d like to view and monitor in a system is the RAM (Random Access Memory). Here, we’ll look at ways to get the total memory in the system and the amount that’s currently free.

3.1. Reading /proc/meminfo

Analogous to getting CPU information from the /proc filesystem, we can get the memory information by reading the /proc/meminfo file:

$ cat /proc/meminfo 
MemTotal:        8032536 kB
MemFree:         1097772 kB
MemAvailable:    4797760 kB
Buffers:          129936 kB
Cached:          3839160 kB
SwapCached:        30452 kB
...truncated...

Reading the /proc/meminfo file gives us comprehensive information about the total, free, and used memory, including swap space. However, the units are in kB, and we’d probably want this to be more readable.

3.2. Using the free Command

We can use the free command with the -h (human-readable) option to retrieve RAM information in a cleaner way:

$ free -h
               total        used        free      shared  buff/cache   available
Mem:           7.7Gi       3.1Gi       1.1Gi       273Mi       4.1Gi       4.6Gi
Swap:          4.0Gi       377Mi       3.6Gi

Now, we have cleaner output in Gi instead of kB. We can easily see that the machine has around 8 GB of RAM, and around 3 GB is in use.

4. Disk Capacity and Usage

After CPU and RAM, the next thing we’ll do is check the hard disk capacity and usage. Here, we’ll see how to check the available disks, partitions, and how much of each partition is used up.

4.1. Checking Disks and Partitions Using /proc/partitions and lsblk

The /proc file we need for disk info is /proc/partitions:

$ cat /proc/partitions 
major minor  #blocks  name

   8        0  118489088 sda
   8        1    1100800 sda1
   8        2  117386240 sda2

We can see from the output that we have one disk labeled sda, and it has two partitions: sda1 and sda2. However, the partition sizes aren’t very “human-readable”. We can fix this using the lsblk command:

$ lsblk
NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINTS
sda      8:0    0   113G  0 disk 
├─sda1   8:1    0     1G  0 part /boot/efi
└─sda2   8:2    0 111.9G  0 part /

Now, we see that our machine has around 113G of storage in one storage device, with about 1G allocated to the boot partitions and the rest mounted at /. We still don’t see other information, such as the type and model of the storage device.

4.2. Disk Information Using the fdisk Command

The fdisk utility is a Linux command that’s commonly used to view and perform operations on disks and partitions. We can use this command with the -l option to get the information we need:

$ sudo fdisk -l
Disk /dev/sda: 113 GiB, 121332826112 bytes, 236978176 sectors
Disk model: APPLE SSD SM0128
...truncated...
Device       Start       End   Sectors   Size Type
/dev/sda1     2048   2203647   2201600     1G EFI System
/dev/sda2  2203648 236976127 234772480 111.9G Linux filesystem

The fdisk output is more comprehensive. Here, we can see the details of the disk capacity, disk model, and so on.

4.3. Checking Disk Usage With df

The tools we’ve covered till now only showed us the total disk capacity. But how do we then check the used and available space? We can do this using the df command, which shows us the disk usage information for each filesystem/partition. We’ll run this command with the -h option to get human-readable output:

$ df -h
Filesystem      Size  Used Avail Use% Mounted on
tmpfs           785M  1.7M  783M   1% /run
/dev/sda2       110G   43G   62G  42% /
tmpfs           3.9G   80M  3.8G   3% /dev/shm
tmpfs           5.0M   12K  5.0M   1% /run/lock
/dev/sda1       1.1G  6.2M  1.1G   1% /boot/efi
tmpfs           785M  100K  785M   1% /run/user/1000

The above output shows us all the available filesystems with size, used space (absolute and percentage units), and available space in a neat, tabular format.

5. Checking Other Hardware Information

So far, we’ve checked the three key system properties: CPU, RAM, and Storage Disks. Next, let’s see how we can get information about other hardware components.

5.1. Listing Hardware Using lshw

The lshw command is commonly used to list all the hardware devices connected to a Linux system:

$ sudo lshw
banana                      
    description: Laptop
    product: MacBookPro12,1
    vendor: Apple Inc.
    ...truncated...
  *-core
       description: Motherboard
       product: Mac-E43C1C25D4880AD6
       vendor: Apple Inc.
       physical id: 0
       version: MacBookPro12,1
       serial: C02639406M8GDVQ1N
  ...truncated...

The command produces a long list of hardware components in a tree format, with details such as description, product, and vendor for each component. To view just the descriptions, we can pipe the output of this to grep to filter the lines containing ‘description’:

$ sudo lshw | grep description
    description: Laptop
       description: Motherboard
          description: L1 cache
          description: L1 cache
          description: L2 cache
          description: L3 cache
          description: CPU
          description: BIOS
          description: System Memory
             description: SODIMM DDR3 Synchronous 1867 MHz (0.5 ns)
             description: SODIMM DDR3 Synchronous 1867 MHz (0.5 ns)
          ...truncated...

The output above shows us the full hierarchy of devices connected to the system.

5.2. Displaying PCI and USB Devices Separately

While the lshw command displays all the hardware devices connected to the system, we can use the lspci and lsusb commands to separately show the attached PCI and USB devices respectively. Let’s first see what the lspci command gives us:

$ lspci
00:00.0 Host bridge: Intel Corporation Broadwell-U Host Bridge -OPI (rev 09)
00:02.0 VGA compatible controller: Intel Corporation Iris Graphics 6100 (rev 09)
00:03.0 Audio device: Intel Corporation Broadwell-U Audio Controller (rev 09)
00:14.0 USB controller: Intel Corporation Wildcat Point-LP USB xHCI Controller (rev 03)
00:15.0 DMA controller: Intel Corporation Wildcat Point-LP Serial IO DMA Controller (rev 03)
...truncated...

Next, let’s see the output for lsusb:

$ lsusb
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 001 Device 002: ID 05ac:8290 Apple, Inc. Bluetooth Host Controller
Bus 001 Device 003: ID 05ac:0273 Apple, Inc. Internal Keyboard/Trackpad (ISO)
Bus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub

We see neat, concise outputs in both cases. However, if we need, we can get more verbose outputs using the -v-vv, and -vvv options with both lspci and lsusb commands. Moreover, the verbosity increases with the number of v‘s in the option.

5.3. Wireless Network Information

Another important piece of hardware that we’d usually like to keep track of is the wireless network adapter. We can see the current status of this device using the iwconfig command:

$ iwconfig
lo        no wireless extensions.

wlp3s0    IEEE 802.11  ESSID:"Wifi Network Name"  
          Mode:Managed  Frequency:2.462 GHz  Access Point: 1C:5F:2B:96:E2:67   
          Bit Rate=26 Mb/s   Tx-Power=31 dBm   
          Retry short limit:7   RTS thr:off   Fragment thr:off
          Power Management:on
          Link Quality=49/70  Signal level=-61 dBm  
          Rx invalid nwid:0  Rx invalid crypt:0  Rx invalid frag:0
          Tx excessive retries:1744  Invalid misc:0   Missed beacon:0

The above output shows the network we’re currently connected to. Additionally, we can also see the access point address, bit rate, signal level, and other useful information.

6. OS and Distribution Information

Lastly, let’s look at how we can get information about the software running our system. Here, we’ll check details about the Linux kernel, distribution, and architecture.

6.1. Using the uname Command

The uname command is an important Linux command that gives us information about the Linux kernel. We’ll run this with the -a option, which stands for “all”:

$ uname -a
Linux banana 6.8.0-51-generic #52-Ubuntu SMP PREEMPT_DYNAMIC Thu Dec  5 13:09:44 UTC 2024 x86_64 x86_64 x86_64 GNU/Linux

The uname command prints import information such as OS name and machine name. Further, it shows us that our machine is a 64-bit x86_64 type and also prints the kernel version.

6.2. Using the lsb_release Command

The lsb_release command provides us with information about the Linux distribution we’re running:

$ lsb_release -a
No LSB modules are available.
Distributor ID:	Ubuntu
Description:	Ubuntu 24.04.1 LTS
Release:	24.04
Codename:	noble

From the above output, we can see that we’re running Ubuntu 24.04, codenamed ‘noble’. Analogous to the uname command, here too we used the -a option to display all the output the command can give us.

6.3. Eyecandy Output: Using Neofetch

Finally, we can use the neofetch command to print all the OS and system information, with some cute ASCII art as a bonus. This package is not pre-installed on most distributions. Therefore, we’ll have to install it using the package manager. For Ubuntu, we’ll use the apt command:

$ sudo apt install neofetch

Once neofetch is installed, let’s run it:

$ neofetch
           `-/osyhddddhyso/-`
        .+yddddddddddddddddddy+.           kd@banana 
      :yddddddddddddddddddddddddy:         --------- 
    -yddddddddddddddddddddhdddddddy-       OS: Xubuntu 24.04.1 LTS x86_64 
   odddddddddddyshdddddddh`dddd+ydddo      Host: MacBookPro12,1 1.0 
 `yddddddhshdd-   ydddddd+`ddh.:dddddy`    Kernel: 6.8.0-51-generic 
 sddddddy   /d.   :dddddd-:dy`-ddddddds    Uptime: 1 day, 22 hours, 54 mins 
:ddddddds    /+   .dddddd`yy`:ddddddddd:   Packages: 2178 (dpkg) 
sdddddddd`    .    .-:/+ssdyodddddddddds   Shell: bash 5.2.21 
ddddddddy                  `:ohddddddddd   Resolution: 1280x800 
dddddddd.                      +dddddddd   DE: Xfce 4.18 
sddddddy                        ydddddds   WM: Xfwm4 
:dddddd+                      .oddddddd:   WM Theme: Nordic 
 sdddddo                   ./ydddddddds    Theme: Nordic [GTK2/3] 
 `yddddd.              `:ohddddddddddy`    Icons: Zafiro-Dark [GTK2/3] 
   oddddh/`      `.:+shdddddddddddddo      Terminal: terminator 
    -ydddddhyssyhdddddddddddddddddy-       CPU: Intel i5-5257U (4) @ 3.100GHz 
      :yddddddddddddddddddddddddy:         GPU: Intel Iris Graphics 6100 
        .+yddddddddddddddddddy+.     ​_      Memory: 3027MiB / 7844MiB 
           `-/osyhddddhyso/-`
                                                                   
                                                                   

The above snippet shows the neofetch output for a system running Xubuntu 24.04. We can see that both hardware and OS details are included. Significantly, we also get a cute ASCII representation of our distribution’s logo.

7. Conclusion

In this article, we looked at how we can see information about various system devices and OS details using the Linux command line. Although GUIs exist to accomplish this, the command line tools are more robust and uniform across various Linux distributions. Markedly, most tools we saw are very specific in what they do, in line with the Unix philosophy of “do one thing, and do it well.”