Getting System Information in Linux | Bash

Retrieving system information in Bash on Linux can be accomplished using various commands and utilities that provide details about hardware, software, and system configurations.

Basic System Information

The uname command in Linux is used to print system information. It stands for "Unix Name." When used without any options, it typically displays the operating system name. However, it can also provide various other details such as kernel version, hostname, and hardware architecture. Let's break down its usage and provide examples:

Display Operating System Name:

When you simply run uname without any options, it prints the operating system name.

$ uname Linux

Display Kernel Version:

The -r option is used to display the kernel release.

$ uname -r 5.4.0-96-generic

Display All Information:

The -a option displays all information available.

$ uname -a

Linux myhostname 5.4.0-96-generic #109-Ubuntu SMP Fri Jan 8 18:02:24 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux

This provides:
Kernel name Node name (hostname) Kernel release Kernel version Machine hardware name Processor type Hardware platform Operating system

Display Hardware Architecture:

The -m option is used to print the hardware architecture.

$ uname -m x86_64

Display Processor Type:

The -p option displays the processor type.

$ uname -p x86_64

Display Operating System Information:

The -o option prints the operating system name.

$ uname -o GNU/Linux

Display Node Name (Hostname):

The -n option is used to print the network node hostname.

$ uname -n myhostname

Display Hardware Platform:

The -i option prints the hardware platform.

$ uname -i x86_64

Hardware Information

lscpu:

The lscpu command provides detailed information about the CPU (Central Processing Unit), including the number of cores, threads, model, architecture, and cache size.

Example:
$ lscpu Architecture: x86_64 CPU op-mode(s): 32-bit, 64-bit Byte Order: Little Endian CPU(s): 8 On-line CPU(s) list: 0-7 Thread(s) per core: 2 Core(s) per socket: 4 Socket(s): 1 ...

lspci:

The lspci command lists information about PCI (Peripheral Component Interconnect) devices, such as network cards, graphics cards, USB controllers, etc.

Example:
$ lspci 00:00.0 Host bridge: Intel Corporation Xeon E3-1200 v6/7th Gen Core Processor Host Bridge/DRAM Registers (rev 08) 00:02.0 VGA compatible controller: Intel Corporation UHD Graphics 620 (rev 07) ...

lshw:

The lshw command provides a comprehensive overview of hardware components on the system, including detailed specifications like CPU, memory, motherboard, network adapters, storage devices, etc.

Example:
$ sudo lshw -short H/W path Device Class Description ======================================================= system Laptop /0 bus X570 Phantom Gaming-ITX/TB3 ... /0/0 memory 64KiB BIOS ...

dmidecode:

The dmidecode command decodes and displays data from the DMI (Desktop Management Interface) BIOS, offering detailed hardware information such as system manufacturer, model, BIOS version, memory configuration, etc.

Example:
$ sudo dmidecode -t memory # dmidecode 3.2 Getting SMBIOS data from sysfs. SMBIOS 3.2.0 present. Handle 0x000E, DMI type 16, 23 bytes Physical Memory Array Location: System Board Or Motherboard Use: System Memory Error Correction Type: None Maximum Capacity: 64 GB Error Information Handle: Not Provided Number Of Devices: 2 ...

Memory Information

free:

The free command displays information about the system's memory usage, including total, used, and free memory, as well as swap space.

Example:
$ free -h total used free shared buff/cache available Mem: 15Gi 2.9Gi 8.5Gi 325Mi 4.2Gi 11Gi Swap: 2.0Gi 0B 2.0Gi total: Total amount of physical memory. used: Memory used by the system. free: Unused memory. shared: Memory used (shared) by multiple processes. buff/cache: Memory used for buffering and caching. available: Estimate of available memory for starting new applications.

The -h option is used to display the values in a human-readable format (e.g., GiB, MiB).

top:

The top command provides a dynamic real-time view of system resource usage, including memory. It shows a list of processes and their resource consumption, with memory statistics displayed at the top.

Example:
$ top top - 08:55:14 up 1 day, 2:18, 2 users, load average: 0.05, 0.06, 0.05 Tasks: 258 total, 1 running, 184 sleeping, 0 stopped, 0 zombie %Cpu(s): 2.3 us, 0.4 sy, 0.0 ni, 97.3 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st KiB Mem : 16443772 total, 168644 free, 2877928 used, 13597200 buff/cache KiB Swap: 2097148 total, 2097148 free, 0 used. 12906356 avail Mem
In the memory section:
  1. KiB Mem: Total, free, used, and buffer/cache memory.
  2. KiB Swap: Total, free, and used swap memory.
  3. avail Mem: Estimate of available memory for new processes.

The top command continuously updates the display by default. Pressing q quits the top program.

Storage Information:

df:

The df command stands for "disk free" and is used to display information about the disk space usage of file systems. It shows the total size, used space, available space, and filesystem type for each mounted filesystem.

Example:
$ df -h Filesystem Size Used Avail Use% Mounted on /dev/sda1 49G 13G 33G 28% / Size: Total size of the filesystem. Used: Amount of space used. Avail: Available space. Use%: Percentage of used space. Mounted on: The mount point of the filesystem.

The -h option is used to display the sizes in a human-readable format (e.g., GiB, MiB).

du:

The du command stands for "disk usage" and is used to estimate the disk space usage of files and directories. It recursively scans directories to calculate the total size of files within them.

Example:
$ du -h /home/user/Documents 10M /home/user/Documents/images 5.5G /home/user/Documents/videos 20K /home/user/Documents/notes.txt 5.5G /home/user/Documents -h: Displays sizes in a human-readable format. /home/user/Documents: Specifies the directory for which disk usage is calculated.

The output shows the size of each subdirectory and file within the specified directory.

You can also use du with other options to control its behavior, such as sorting the output by size or excluding certain directories from the calculation.

Network Information

ip addr:

The ip addr command is part of the iproute2 package and is used to display detailed information about network interfaces, including IP addresses, subnet masks, MAC addresses, and more.

Example:
$ ip addr 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 inet 192.168.1.100/24 brd 192.168.1.255 scope global dynamic eth0 valid_lft 86077sec preferred_lft 86077sec lo: Loopback interface. eth0: Ethernet interface. inet: IPv4 address. inet6: IPv6 address. mtu: Maximum Transmission Unit. brd: Broadcast address.

netstat:

The netstat command displays information about network connections, routing tables, interface statistics, masquerade connections, and multicast memberships. However, it is considered somewhat deprecated in favor of the ss command.

Example:
$ netstat -tuln Proto Recv-Q Send-Q Local Address Foreign Address State tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN -t: TCP connections. -u: UDP connections. -l: Show only listening sockets. -n: Show numerical addresses instead of resolving hosts.

ping:

The ping command is used to check network connectivity by sending ICMP echo request packets to a specified host and waiting for ICMP echo reply packets.

Example:
$ ping -c 4 google.com PING google.com (172.217.7.174) 56(84) bytes of data. 64 bytes from maa05s10-in-f14.1e100.net (172.217.7.174): icmp_seq=1 ttl=55 time=18.3 ms 64 bytes from maa05s10-in-f14.1e100.net (172.217.7.174): icmp_seq=2 ttl=55 time=17.9 ms 64 bytes from maa05s10-in-f14.1e100.net (172.217.7.174): icmp_seq=3 ttl=55 time=17.9 ms 64 bytes from maa05s10-in-f14.1e100.net (172.217.7.174): icmp_seq=4 ttl=55 time=17.8 ms --- google.com ping statistics --- 4 packets transmitted, 4 received, 0% packet loss, time 3003ms rtt min/avg/max/mdev = 17.828/18.002/18.331/0.191 ms -c 4: Send 4 ICMP echo request packets. google.com: Target hostname or IP address. 64 bytes from ...: Response from the target host. --- google.com ping statistics ---: Summary of ping statistics.

Additional Tools

inxi:

inxi is a versatile command-line tool that provides a concise summary of various system information in an easy-to-read format. It gathers information about hardware, CPU, memory, storage, network, and more.

Example:
$ inxi -F System: Host: myhost Kernel: 5.4.0-97-generic x86_64 bits: 64 Desktop: GNOME 3.36.9 Distro: Ubuntu 20.04.3 LTS Machine: Type: Laptop System: Dell product: Latitude 5490 v: N/A serial: <superuser required> CPU: Topology: Quad Core model: Intel Core i7-8650U bits: 64 type: MT MCP L2 cache: 8192 KiB Speed: 800 MHz min/max: 400/4200 MHz Core speeds (MHz): 1: 800 2: 800 3: 800 4: 800 5: 800 6: 800 7: 800 8: 800 ... Graphics: Device-1: Intel UHD Graphics 620 driver: i915 v: kernel Display: x11 server: X.Org 1.20.11 driver: modesetting unloaded: fbdev,vesa resolution: 1920x1080~60Hz ... -F: Display full information about the system. System: Information about the operating system, kernel version, and desktop environment. Machine: Details about the hardware, including manufacturer and product information. CPU: Information about the processor, including model, cores, and speeds. Graphics: Details about the graphics hardware and drivers.

hwinfo:

hwinfo is a command-line tool that provides detailed hardware information, including CPU, memory, storage, graphics, network, and more. It supports various output formats, including XML and machine-readable options, making it suitable for scripting and automation.

Example:
$ sudo hwinfo --short cpu: Intel(R) Core(TM) i7-8650U CPU @ 1.90GHz, 4200 MHz ... network: wlp2s0 Intel Dual Band Wireless-AC 8265 enp0s31f6 Intel Ethernet Connection (4) I219-LM ... storage: HGST HTS541010B7E610 --short: Display brief information about hardware components. cpu: Information about the CPU, including model and speed. network: Details about network interfaces. storage: Information about storage devices.

Additionally, hwinfo offers various options for filtering and customizing the output, making it a powerful tool for gathering hardware information.

Additional Information:
  1. Use the man command with any of these tools to access detailed documentation and usage examples.
  2. Combine tools for more specific information. For example, use lspci | grep Network to list only network-related PCI devices.
  3. Be aware that some tools like dmidecode might require root privileges.

Conclusion

Retrieving system information in Bash on Linux involves using commands like uname, lscpu, lsblk, and free to gather details about the operating system, hardware configuration, memory usage, and storage availability. These commands provide essential insights into the system's architecture, CPU specifications, disk partitions, and resource utilization, empowering users to monitor system health, diagnose issues, and make informed decisions regarding system management and optimization.