uname Command

Display system information including kernel name, version, and architecture

Syntax: uname [OPTION]...

Description

The uname command displays system information about the current machine and operating system. It provides details such as the kernel name, network node hostname, kernel release, kernel version, machine hardware name, processor type, hardware platform, and operating system name.

Note: Without any options, uname displays the kernel name (equivalent to uname -s). This command is commonly used in scripts to determine system characteristics.

Common Options

Option Description
-a, --all Print all information (equivalent to -snrvmpio)
-s, --kernel-name Print the kernel name
-n, --nodename Print the network node hostname
-r, --kernel-release Print the kernel release
-v, --kernel-version Print the kernel version
-m, --machine Print the machine hardware name
-p, --processor Print the processor type
-i, --hardware-platform Print the hardware platform
-o, --operating-system Print the operating system
--help Display help message and exit
--version Output version information and exit

Examples

Basic usage (kernel name):
uname
# Output: Linux

uname -s
# Output: Linux (same as above)
Display all system information:
uname -a
# Output: Linux hostname 5.15.0-56-generic #62-Ubuntu SMP Tue Nov 22 19:54:14 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux
Display kernel release:
uname -r
# Output: 5.15.0-56-generic

uname --kernel-release
# Output: 5.15.0-56-generic
Display machine architecture:
uname -m
# Output: x86_64

uname --machine
# Output: x86_64
Display hostname:
uname -n
# Output: myserver

uname --nodename
# Output: myserver
Display kernel version:
uname -v
# Output: #62-Ubuntu SMP Tue Nov 22 19:54:14 UTC 2022
Display operating system:
uname -o
# Output: GNU/Linux

uname --operating-system
# Output: GNU/Linux
Display processor information:
uname -p
# Output: x86_64 (or unknown on some systems)

uname -i
# Output: x86_64 (hardware platform)
Combine multiple options:
uname -sr
# Output: Linux 5.15.0-56-generic

uname -nm
# Output: myserver x86_64

uname -rmo
# Output: 5.15.0-56-generic x86_64 GNU/Linux

Output Format Explanation

When using uname -a, the output format is:

Sample output breakdown:
Linux hostname 5.15.0-56-generic #62-Ubuntu SMP Tue Nov 22 19:54:14 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux

Fields in order:

  1. Kernel name: Linux
  2. Hostname: hostname
  3. Kernel release: 5.15.0-56-generic
  4. Kernel version: #62-Ubuntu SMP Tue Nov 22 19:54:14 UTC 2022
  5. Machine: x86_64
  6. Processor: x86_64
  7. Hardware platform: x86_64
  8. Operating system: GNU/Linux

Common Architecture Values

Architecture Description
x86_64 64-bit Intel/AMD processors
i386, i686 32-bit Intel processors
aarch64 64-bit ARM processors
armv7l 32-bit ARM processors
ppc64le 64-bit PowerPC (little endian)
s390x IBM System z (64-bit)
mips64 64-bit MIPS processors

Practical Usage in Scripts

Check architecture in scripts:
#!/bin/bash
ARCH=$(uname -m)
if [ "$ARCH" = "x86_64" ]; then
    echo "64-bit system detected"
    # Download 64-bit version
elif [ "$ARCH" = "i386" ] || [ "$ARCH" = "i686" ]; then
    echo "32-bit system detected"
    # Download 32-bit version
else
    echo "Unsupported architecture: $ARCH"
fi
Check operating system:
#!/bin/bash
OS=$(uname -s)
case $OS in
    Linux)
        echo "Running on Linux"
        ;;
    Darwin)
        echo "Running on macOS"
        ;;
    FreeBSD)
        echo "Running on FreeBSD"
        ;;
    *)
        echo "Unknown operating system: $OS"
        ;;
esac
Create system information report:
#!/bin/bash
echo "System Information Report"
echo "========================"
echo "Hostname: $(uname -n)"
echo "Operating System: $(uname -o)"
echo "Kernel: $(uname -s) $(uname -r)"
echo "Architecture: $(uname -m)"
echo "Kernel Version: $(uname -v)"
echo "Full Info: $(uname -a)"
Conditional compilation based on architecture:
#!/bin/bash
ARCH=$(uname -m)
KERNEL=$(uname -r)

echo "Compiling for architecture: $ARCH"
echo "Kernel version: $KERNEL"

if [[ $ARCH == "x86_64" ]]; then
    CFLAGS="-m64 -O2"
elif [[ $ARCH == "i"*"86" ]]; then
    CFLAGS="-m32 -O2"
else
    CFLAGS="-O2"
fi

echo "Using CFLAGS: $CFLAGS"

Advanced Usage

Format output for specific needs:
# Get just the major kernel version
uname -r | cut -d. -f1-2
# Output: 5.15

# Check if running specific kernel version
if [[ $(uname -r) == *"generic"* ]]; then
    echo "Running Ubuntu generic kernel"
fi

# Get hostname without domain
uname -n | cut -d. -f1
Compare with other system info commands:
# uname vs other commands
echo "uname -n: $(uname -n)"
echo "hostname: $(hostname)"
echo "uname -r: $(uname -r)"
echo "uname -m: $(uname -m)"
echo "arch: $(arch)"  # equivalent to uname -m
Use in Makefiles:
# Makefile example
UNAME_S := $(shell uname -s)
UNAME_M := $(shell uname -m)

ifeq ($(UNAME_S),Linux)
    CCFLAGS += -D LINUX
endif
ifeq ($(UNAME_S),Darwin)
    CCFLAGS += -D OSX
endif
ifeq ($(UNAME_M),x86_64)
    CCFLAGS += -D AMD64
endif

Common Use Cases

  • System identification: Determining the operating system and architecture
  • Script portability: Writing scripts that work across different systems
  • Software installation: Selecting appropriate binaries for the system
  • System monitoring: Including system info in logs and reports
  • Debugging: Identifying system characteristics when troubleshooting
  • Conditional compilation: Setting compiler flags based on architecture
  • Documentation: Recording system specifications
  • Automation: Making deployment scripts system-aware

Tips and Best Practices

  • Use uname -a for comprehensive system information
  • Store uname output in variables for multiple uses in scripts
  • Use uname -m to detect 32-bit vs 64-bit systems
  • Combine with other commands like lsb_release for complete OS info
  • Be aware that some fields may show "unknown" on certain systems
  • Use case statements for handling multiple OS types
  • Consider using arch command as alternative to uname -m
  • Test scripts on different architectures when using uname for conditionals

Related Information

Command Purpose
hostname Display or set system hostname
arch Display machine architecture (same as uname -m)
lsb_release Display Linux distribution information
cat /proc/version Display detailed kernel version information
hostnamectl Control system hostname and related settings
Related Commands: hostname, lscpu, arch, hostnamectl, lsb_release