Read

Module 7 · The system, disks and the environment

uname, uptime and hostname

Three small commands that answer the first questions you ask any machine: which kernel is this, how long has it been up, and what is it called.

What you will learn

  • Read kernel version and architecture with uname.
  • Interpret uptime and the three load averages.
  • Show and change the machine's hostname.

When you land on a machine you have never seen, the first minute is always the same: what is this thing, is it healthy, and what should I call it? Linux answers each of those with a one-word command. None of them need options to be useful, which is why they are usually the first things typed into a fresh terminal.

uname: what kernel am I running?

uname means *Unix name*. Alone it prints just Linux, which is not much help. With -a (all) you get the whole line: kernel name, hostname, kernel release, build info, machine architecture and operating system. The two fields you will look up most often are the release (-r), which tells you which kernel features and drivers exist, and the machine (-m), which tells you whether binaries must be 32-bit or 64-bit.

~% uname
Linux
~% uname -a
Linux (none) 4.16.13 #13 SMP Sat Jul 21 13:56:18 CST 2018 i686 GNU/Linux
~% uname -r
4.16.13
~% uname -m
i686

In the practice VM the architecture is i686: a 32-bit x86 processor, because the emulator behaves like a Pentium III. The (none) in the second field is the hostname, and we will fix that in a moment. SMP means the kernel can use several CPUs, even though this machine has one; nproc confirms it.

uptime: how long, and how busy

uptime prints one line: the current time, how long the system has been running since boot, and three load averages. The load average is the mean number of processes that were running or waiting to run over the last 1, 5 and 15 minutes. On a one-CPU machine a load of 1.00 means the processor was exactly fully used; 0.50 means half idle; 4.00 means work is queuing. On a machine with four CPUs, divide by four before you worry.

~% uptime
 18:51:15 up 0 min,  load average: 0.00, 0.00, 0.00
~% cat /proc/uptime
2.96 0.73
~% cat /proc/loadavg
0.00 0.00 0.00 1/46 887

The numbers come from the kernel's /proc files you met in lesson 60: /proc/uptime holds seconds since boot and seconds spent idle, and /proc/loadavg holds the three averages plus the running/total process count and the last PID used. uptime is simply a friendlier view of the same data.

hostname: the machine's name

Every Linux system has a short name, the hostname, which shows up in prompts, logs, and network tools. hostname prints it, and as root hostname NEWNAME changes it immediately. The kernel keeps the live value in /proc/sys/kernel/hostname; on a normal distribution the file /etc/hostname stores the name to apply at boot, and hostname -F /etc/hostname loads it from there.

~% hostname
(none)
~% hostname labbox
~% hostname
labbox
~% cat /proc/sys/kernel/hostname
labbox
~% uname -n
labbox
CommandShows
uname -rKernel release, e.g. 4.16.13
uname -mMachine architecture: i686, x86_64, aarch64
uname -nThe hostname, same as hostname
uptimeTime since boot and load averages
nprocNumber of CPUs the kernel can use

Commands in this lesson

CommandWhat it does
uname -aPrint everything uname knows, in one line.
uname -rKernel release only.
uname -mMachine architecture.
uptimeTime since boot and the 1/5/15-minute load averages.
hostnameShow the machine's name.
hostname NAMESet the machine's name (root only).
nprocCount the available processors.

Quiz

  1. Which option makes uname print only the kernel release?

    • -a
    • -r
    • -m
    • -n
  2. A one-CPU machine shows load averages of 2.10, 1.80, 0.40. What does that tell you?

    • It has been overloaded for hours.
    • Load has risen recently: it was quiet 15 minutes ago and is now queuing work.
    • Two CPUs are broken.
    • Memory is 210% full.
  3. Where does the kernel keep the live hostname?

    • /etc/passwd
    • /proc/sys/kernel/hostname
    • /var/log/hostname
    • ~/.hostname
  4. What does `i686` in the uname output mean?

    • The kernel was built in 1986.
    • A 32-bit x86 processor.
    • There are 686 processes.
    • The hostname.

Practice

  1. Save just the kernel release (nothing else) into `/root/lab/l61/kernel.txt`.

  2. Change the machine's hostname to `labbox`.

  3. Record the uptime line (with its load averages) into `/root/lab/l61/up.txt`.

Open this lesson in the app to do the tasks in a real Linux machine and have them checked.