Shell, system, and network tools

Linux commands: uname

Learn how uname prints the kernel name, release, version, hostname, and machine hardware on Unix systems, and when macOS users should use sw_vers instead.

uname tells you what system you’re on. I run it first thing when I SSH into a machine I don’t know. Without options it prints the kernel name:

uname

Terminal showing uname command output displaying Darwin as the operating system name

On macOS this prints Darwin, the name of the kernel under macOS. On a Linux system it prints Linux.

The -m option prints the machine hardware name:

uname -m

This is the option I use most. It tells you which binaries to download: x86_64 on an Intel machine, arm64 on an Apple silicon Mac, aarch64 on a 64-bit ARM Linux box such as a Raspberry Pi.

The -p option tries to print the processor type, but it is not portable and can print unknown. Prefer -m in scripts.

Terminal showing uname -mp output displaying arm64 arm for the machine hardware and processor type

Use -s for the kernel name, -r for its release, and -v for its version:

uname -srv

Terminal showing uname -srv output with the Darwin kernel name, release 27.2.0, and the kernel version and build details

The -n option prints the network node name, which is normally the hostname:

Terminal showing uname -n output displaying mac as the network node name

The -a option prints all the fields at once:

Terminal showing uname -a output with all the fields: Darwin, the hostname mac, release 27.2.0, the kernel version and arm64

Here’s the same command on an Ubuntu server, so you can compare:

uname -a
# Linux web1 6.8.0-45-generic #45-Ubuntu SMP PREEMPT_DYNAMIC Fri Aug 30 12:02:04 UTC 2024 x86_64 x86_64 x86_64 GNU/Linux

Kernel name, hostname, release, version, then the machine name. When someone asks “which kernel is that server running?”, uname -a is the answer to paste.

In shell scripts, uname -s is the standard way to branch on the operating system:

case "$(uname -s)" in
  Darwin) echo 'running on macOS' ;;
  Linux) echo 'running on Linux' ;;
esac

Notice what uname doesn’t tell you: the macOS version. Darwin 27.2.0 is a kernel release, not a product version, and it doesn’t always match the macOS version (this Mac runs macOS 27.2.1). On macOS, use sw_vers when you need the product name, version, and build number:

Terminal showing sw_vers output displaying ProductName macOS, ProductVersion 27.2.1 and BuildVersion 26B131

The same goes for Linux distributions. uname says Linux for Ubuntu, Debian and Fedora alike. To find out which one you’re on, read /etc/os-release with cat.

The uname command works on Linux, macOS, WSL, and other Unix-like environments. Stick to -s, -n, -r, -v, and -m when portability matters.

Lesson completed