Building a Simple System Information Script in Bash

Ever needed a quick way to check your system’s vital stats without memorizing a dozen different commands?
I created a simple bash script that displays all the essential system information in one clean output.

What It Does

The script displays:

  • Hostname and current date
  • System uptime
  • Linux distribution and kernel version
  • CPU model and core count
  • Memory usage (total, used, free)
  • Disk space utilization
  • Network information (IPv4, MAC address, gateway, DNS)

The Script

Here’s the complete script:

#!/bin/bash
# Description: Display system information
echo "=== System Info ==="
echo "Hostname: $(hostname)"
echo "Date: $(date)"
echo "Uptime: $(uptime -p | sed 's/up //')"
echo "Distro: $(lsb_release -ds 2>/dev/null || grep PRETTY_NAME /etc/os-release | cut -d'"' -f2)"
echo "Kernel: $(uname -r)"
# CPU information
echo "CPU: $(lscpu | grep 'Model name' | cut -d':' -f2 | xargs)"
# Memory information
echo "Mem: $(free -h | awk '/^Mem:/ {print $2 " total, " $3 " used, " $4 " free"}')"
# Disk information
echo "Disk: $(df -h / | awk 'NR==2 {print $2 " total, " $3 " used, " $4 " free (" $5 " used)"}')"
# IP address (primary interface)
echo "IPv4: $(hostname -I | awk '{print $1}')"
# MAC address (primary interface)
echo "MAC: $(ip link show | awk '/ether/ {print $2; exit}')"
# Default Gateway
echo "Gateway: $(ip route | grep default | awk '{print $3}')"
# DNS Server (current)
echo "DNS: $(resolvectl status | grep 'Current DNS Server:' | awk '{print $4}')"

Sample Output

=== System Info ===
Hostname: cato
Date: Mon Feb  2 18:33:38 CET 2026
Uptime: 2 days, 2 hours, 55 minutes
Distro: Ubuntu 24.04.3 LTS
Kernel: 6.8.0-94-generic
CPU: Intel(R) Xeon(R) CPU D-1541 @ 2.10GHz
Mem: 31Gi total, 739Mi used, 29Gi free
Disk: 100G total, 45G used, 50G free (45% used)
IPv4: 10.0.39.1
MAC: 00:50:56:9c:cd:b1
Gateway: 10.0.39.254
DNS: 1.1.1.1

How to Use

Save the script to a file (e.g., sysinfo), make it executable, and run it:

chmod +x sysinfo
./sysinfo

Key Commands Explained

  • uptime -p - Human-readable uptime format
  • lscpu - Detailed CPU information
  • free -h - Memory usage in human-readable format
  • df -h - Disk space usage
  • ip route - Network routing information
  • resolvectl status - DNS resolver configuration

This script has been validated with shellcheck to follow bash best practices, so you can be confident it’s clean and efficient.

my DevOps Odyssey

“Σα βγεις στον πηγαιμό για την Ιθάκη, να εύχεσαι να ‘ναι μακρύς ο δρόμος, γεμάτος περιπέτειες, γεμάτος γνώσεις.” - Kavafis’ Ithaka.



Building a Simple System Information Script in Bash

2026-02-02

Series:lab

Categories:Linux

Tags:#bash, #linux, #scripts, #sysadmin


Building a Simple System Information Script in Bash: