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
| Command | Shows |
|---|---|
uname -r | Kernel release, e.g. 4.16.13 |
uname -m | Machine architecture: i686, x86_64, aarch64 |
uname -n | The hostname, same as hostname |
uptime | Time since boot and load averages |
nproc | Number of CPUs the kernel can use |
Commands in this lesson
| Command | What it does |
|---|---|
uname -a | Print everything uname knows, in one line. |
uname -r | Kernel release only. |
uname -m | Machine architecture. |
uptime | Time since boot and the 1/5/15-minute load averages. |
hostname | Show the machine's name. |
hostname NAME | Set the machine's name (root only). |
nproc | Count the available processors. |
Quiz
Which option makes uname print only the kernel release?
- -a
- -r
- -m
- -n
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.
Where does the kernel keep the live hostname?
- /etc/passwd
- /proc/sys/kernel/hostname
- /var/log/hostname
- ~/.hostname
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
Save just the kernel release (nothing else) into `/root/lab/l61/kernel.txt`.
Change the machine's hostname to `labbox`.
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.