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.
Display Kernel Version:
The -r option is used to display the kernel release.
Display All Information:
The -a option displays all information available.
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:Display Hardware Architecture:
The -m option is used to print the hardware architecture.
Display Processor Type:
The -p option displays the processor type.
Display Operating System Information:
The -o option prints the operating system name.
Display Node Name (Hostname):
The -n option is used to print the network node hostname.
Display Hardware Platform:
The -i option prints the hardware platform.
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:lspci:
The lspci command lists information about PCI (Peripheral Component Interconnect) devices, such as network cards, graphics cards, USB controllers, etc.
Example: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: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: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: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:- KiB Mem: Total, free, used, and buffer/cache memory.
- KiB Swap: Total, free, and used swap memory.
- 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: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: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: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: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: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: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:Additionally, hwinfo offers various options for filtering and customizing the output, making it a powerful tool for gathering hardware information.
Additional Information:- Use the man command with any of these tools to access detailed documentation and usage examples.
- Combine tools for more specific information. For example, use lspci | grep Network to list only network-related PCI devices.
- 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.